集册 Java实例教程 连接数组。

连接数组。

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

507
连接数组。
// from N o  w  J a v a . c o m - 时  代  Java

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

import java.lang.reflect.Array;

import static jodd.util.StringPool.NULL;


public class Main{

    /**

     * Joins arrays. Component type is resolved from the array argument.

     */

    @SuppressWarnings({ "unchecked" })

    public static <T> T[] join(T[]... arrays) {

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

                .getComponentType().getComponentType();

        return join(componentType, arrays);

    }

    /**

     * Joins arrays using provided component type.

     */

    @SuppressWarnings({ "unchecked" })

    public static <T> T[] join(Class<T> componentType, T[][] arrays) {

        if (arrays.length == 1) {//nowjava.com 提供

            return arrays[0];

        }

        int length = 0;

        for (T[] array : arrays) {

            length += array.length;

        }

        T[] result = (T[]) Array.newInstance(componentType, length);


        length = 0;

        for (T[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>String</code> arrays.

     */

    public static String[] join(String[]... arrays) {

        if (arrays.length == 0) {

            return new String[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (String[] array : arrays) {

            length += array.length;

        }

        String[] result = new String[length];

        length = 0;

        for (String[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>byte</code> arrays.

     */

    public static byte[] join(byte[]... arrays) {

        if (arrays.length == 0) {

            return new byte[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (byte[] array : arrays) {

            length += array.length;

        }

        byte[] result = new byte[length];

        length = 0;

        for (byte[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>char</code> arrays.

     */

    public static char[] join(char[]... arrays) {

        if (arrays.length == 0) {

            return new char[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (char[] array : arrays) {

            length += array.length;

        }

        char[] result = new char[length];

        length = 0;

        for (char[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>short</code> arrays.

     */

    public static short[] join(short[]... arrays) {

        if (arrays.length == 0) {

            return new short[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (short[] array : arrays) {

            length += array.length;

        }

        short[] result = new short[length];

        length = 0;

        for (short[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>int</code> arrays.

     */

    public static int[] join(int[]... arrays) {

        if (arrays.length == 0) {

            return new int[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (int[] array : arrays) {

            length += array.length;

        }

        int[] result = new int[length];

        length = 0;

        for (int[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>long</code> arrays.

     */

    public static long[] join(long[]... arrays) {

        if (arrays.length == 0) {

            return new long[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (long[] array : arrays) {

            length += array.length;

        }

        long[] result = new long[length];

        length = 0;

        for (long[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>float</code> arrays.

     */

    public static float[] join(float[]... arrays) {

        if (arrays.length == 0) {

            return new float[0];

        }

        if (arrays.length == 1) {

            return arrays[0];

        }

        int length = 0;

        for (float[] array : arrays) {

            length += array.length;

        }

        float[] result = new float[length];

        length = 0;

        for (float[] array : arrays) {

            System.arraycopy(array, 0, result, length, array.length);

            length += array.length;

        }

        return result;

    }

    /**

     * Join <code>double</code> arrays.

     */

    public static double[] join(double[]... arrays) {

        if (arrays.length == 0) {

            return new double[0];

        }

        if (arrays.length == 1) {

            
展开阅读全文