Java examples for Collection Framework:Array Element
Joins arrays.
// 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{ /**/*from w ww . j a v a 2 s . c o m*/ * 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) { 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) { return arrays[0]; } int length = 0; for (double[] array : arrays) { length += array.length; } double[] result = new double[length]; length = 0; for (double[] array : arrays) { System.arraycopy(array, 0, result, length, array.length); length += array.length; } return result; } /** * Join <code>boolean</code> arrays. */ public static boolean[] join(boolean[]... arrays) { if (arrays.length == 0) { return new boolean[0]; } if (arrays.length == 1) { return arrays[0]; } int length = 0; for (boolean[] array : arrays) { length += array.length; } boolean[] result = new boolean[length]; length = 0; for (boolean[] array : arrays) { System.arraycopy(array, 0, result, length, array.length); length += array.length; } return result; } }