Arrays.copyOf uses System.arraycopy , but first adds some fixes
public static int[] copyOf(int[] original, int newLength) { if (0 < = newLength) { return copyOfRange(original, 0, newLength); } throw new NegativeArraySizeException(); } public static int[] copyOfRange(int[] original, int start, int end) { if (start < = end) { if (original.length >= start && 0 < = start) { int length = end - start; int copyLength = Math.min(length, original.length - start); int[] copy = new int[length]; System.arraycopy(original, start, copy, 0, copyLength); return copy; } throw new ArrayIndexOutOfBoundsException(); } throw new IllegalArgumentException(); }
I got this from http://www.docjar.com/docs/api/java/util/Arrays.html , so it would be a little better to use this than Arrays.copyOf
int[] newone = new int[orig.length]; System.arraycopy(orig, 0, newone , 0, orig.length);
although it would actually be better to save arrays and reuse them after they are done with them.
source share