Java Reference ยท Collections
Java Arrays.copyOf() and copyOfRange()
Reference for Arrays.copyOf() and Arrays.copyOfRange(): copy, truncate or pad arrays, shallow copy behavior, and how they compare to clone and arraycopy.
Arrays.copyOf(array, newLength) returns a new array with the first newLength elements of the original. If the new length is bigger, the extra slots are filled with the default value (0, false, '\u0000' or null). If it is smaller, the copy is truncated.
Arrays.copyOfRange(array, from, to) copies a slice: from is inclusive, to is exclusive, and to may exceed the array length, in which case the end is padded with defaults.
int[] bigger = Arrays.copyOf(src, src.length * 2); int[] slice = Arrays.copyOfRange(src, 2, 5);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public static int[] copyOf(int[] original, int newLength) // overloads for all primitives public static <T> T[] copyOf(T[] original, int newLength) public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) public static int[] copyOfRange(int[] original, int from, int to) public static <T> T[] copyOfRange(T[] original, int from, int to)
| Name | Description |
|---|---|
original |
The array to copy from. |
newLength |
Length of the copy; may be smaller or larger than the original. |
from |
First index to copy, inclusive; must be in 0..original.length. |
to |
End index, exclusive; may be greater than original.length. |
newType |
Array class of the result, for converting element types. |
Returns: a new array.
Throws: NegativeArraySizeException if newLength is negative; ArrayIndexOutOfBoundsException if from is out of range; IllegalArgumentException if from > to; NullPointerException if original is null.
Usage notes
- Shallow copy: for object arrays, the references are copied, not the objects. Nested arrays are shared. Copy each row to get a deep copy.
- Padding: new slots hold the type's default, so a larger
String[]copy hasnulls. - Growing an array is the classic use.
ArrayListdoesArrays.copyOfinternally when it needs more capacity. - Alternatives:
array.clone()for a same-size copy;System.arraycopyto copy into an existing array (fastest, with manual bounds);list.toArray(new T[0])for collections. - Complexity:
O(n)in the length of the copy. - No slice views: lists have
subList, but arrays do not, so a slice always allocates a new array. - The three-argument
copyOfchanges the runtime component type and throwsArrayStoreExceptionif elements do not fit the new type.