集册 Java实例教程 将元素追加到数组。

将元素追加到数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

405
将元素追加到数组。

// Copyright (c) 2003-present, Jodd Team (jodd.org). All Rights Reserved.

import java.lang.reflect.Array;

import static jodd.util.StringPool.NULL;
//from 时 代 J a v a

public class Main{

    /**

     * Appends an element to array.

     */

    public static <T> T[] append(T[] buffer, T newElement) {

        T[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>String</code> array.

     */

    public static String[] append(String buffer[], String newElement) {

        String[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>byte</code> array.

     */

    public static byte[] append(byte buffer[], byte newElement) {

        byte[] t = resize(buffer, buffer.length + 1);
        /*
        来 自*
         N o w  J a v a  .   c o m
        */

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>char</code> array.

     */

    public static char[] append(char buffer[], char newElement) {

        char[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>short</code> array.

     */

    public static short[] append(short buffer[], short newElement) {

        short[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>int</code> array.

     */

    public static int[] append(int buffer[], int newElement) {

        int[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>long</code> array.

     */

    public static long[] append(long buffer[], long newElement) {

        long[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>float</code> array.

     */

    public static float[] append(float buffer[], float newElement) {

        float[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>double</code> array.

     */

    public static double[] append(double buffer[], double newElement) {

        double[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Appends an element to <code>boolean</code> array.

     */

    public static boolean[] append(boolean buffer[], boolean newElement) {

        boolean[] t = resize(buffer, buffer.length + 1);

        t[buffer.length] = newElement;

        return t;

    }

    /**

     * Resizes an array.

     */

    public static <T> T[] resize(T[] buffer, int newSize) {

        Class<T> componentType = (Class<T>) buffer.getClass()

                .getComponentType();

        T[] temp = (T[]) Array.newInstance(componentType, newSize);

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>String</code> array.

     */

    public static String[] resize(String buffer[], int newSize) {

        String temp[] = new String[newSize];

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>byte</code> array.

     */

    public static byte[] resize(byte buffer[], int newSize) {

        byte temp[] = new byte[newSize];

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>char</code> array.

     */

    public static char[] resize(char buffer[], int newSize) {

        char temp[] = new char[newSize];

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>short</code> array.

     */

    public static short[] resize(short buffer[], int newSize) {

        short temp[] = new short[newSize];

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>int</code> array.

     */

    public static int[] resize(int buffer[], int newSize) {

        int temp[] = new int[newSize];

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>long</code> array.

     */

    public static long[] resize(long buffer[], int newSize) {

        long temp[] = new long[newSize];

        System.arraycopy(buffer, 0, temp, 0,

                buffer.length >= newSize ? newSize : buffer.length);

        return temp;

    }

    /**

     * Resizes a <code>float</code> array.

     */

    public 
展开阅读全文