import java.lang.reflect.Array; public class ArrayUtils { @SuppressWarnings("unchecked") public static <T> T[] concatenate(T[] a, T[] b) { int len1 = a.length; int len2 = b.length; T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), len1 + len2); System.arraycopy(a, 0, c, 0, len1); System.arraycopy(b, 0, c, len1, len2); return c; } }
public class TestArrayUtils { public static void main(String args[]) { Integer a[] = new Integer[10]; Integer b[] = new Integer[10]; for (int i = 0, j = 10; i < 10; i++, j--) { a[i] = i; b[i] = j; } printArray(a); printArray(b); printArray(ArrayUtils.concatenate(a, b)); } static void printArray(Object obj[]) { for (Object tmp : obj) System.out.print(tmp + ","); System.out.println(); } }
Output
0,1,2,3,4,5,6,7,8,9, 10,9,8,7,6,5,4,3,2,1, 0,1,2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2,1,
public
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int
length)
Copies an array from the specified source
array, beginning at the specified position, to the specified position of the
destination array.
No comments:
Post a Comment