public static Object[] copy (Object[] arr1, Object[] arr2) { if (arr1 == null) throw new IllegalArgumentException("arr1 is null"); if (arr2 == null) throw new IllegalArgumentException("arr2 is null"); Object[] result = new Object[arr1.length + arr2.length]; System.arraycopy(arr1, 0, result, 0, arr1.length); System.arraycopy(arr2, 0, result, arr1.length, arr2.length); return result; }
int a[] = new int[4]; int b[] = {1}; int c[] = {2,3,4}; System.arraycopy(b, 0, a, 0, b.length); System.arraycopy(c, 0, a, b.length, c.length); System.out.println(Arrays.toString(a));
更好用的 public static <T> T[] copy (T[] arr1, T[] arr2) { if (arr1 == null) throw new IllegalArgumentException("arr1 is null"); if (arr2 == null) throw new IllegalArgumentException("arr2 is null"); T[] result = (T[]) Array.newInstance(arr1.getClass().getComponentType(), arr1.length + arr2.length); System.arraycopy(arr1, 0, result, 0, arr1.length); System.arraycopy(arr2, 0, result, arr1.length, arr2.length); return result; }
if (arr1 == null)
throw new IllegalArgumentException("arr1 is null");
if (arr2 == null)
throw new IllegalArgumentException("arr2 is null"); Object[] result = new Object[arr1.length + arr2.length];
System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
return result;
}
int a[] = new int[4];
int b[] = {1};
int c[] = {2,3,4};
System.arraycopy(b, 0, a, 0, b.length);
System.arraycopy(c, 0, a, b.length, c.length);
System.out.println(Arrays.toString(a));
public static <T> T[] copy (T[] arr1, T[] arr2) {
if (arr1 == null)
throw new IllegalArgumentException("arr1 is null");
if (arr2 == null)
throw new IllegalArgumentException("arr2 is null"); T[] result = (T[]) Array.newInstance(arr1.getClass().getComponentType(), arr1.length + arr2.length);
System.arraycopy(arr1, 0, result, 0, arr1.length);
System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
return result;
}