Java Reference ยท java.lang
Java System.arraycopy() method
Reference for System.arraycopy(): copy a range of array elements, overlapping ranges, exceptions, shallow copies of objects and when to prefer Arrays.copyOf.
System.arraycopy() copies a range of elements from one array into another (or into a different part of the same array). It is a native method and is usually the fastest way to move bulk data, which is why ArrayList, StringBuilder and Arrays.copyOf use it internally.
Both arrays must already exist, and the destination must be big enough. The method never allocates anything.
System.arraycopy(source, srcPos, dest, destPos, length);
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 native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
| Name | Description |
|---|---|
src |
Source array. |
srcPos |
Index of the first element to copy. |
dest |
Destination array. |
destPos |
Index in the destination where the first element lands. |
length |
Number of elements to copy. |
Returns: nothing (void).
Throws:
NullPointerExceptionifsrcordestisnull.ArrayStoreExceptionif an argument is not an array, the component types are incompatible (for exampleint[]intolong[]), or an element cannot be stored in the destination. With reference arrays, elements before the failing one have already been copied.IndexOutOfBoundsExceptionifsrcPos,destPosorlengthis negative or the range exceeds either array.
Usage notes
- Overlap is safe. When
srcanddestare the same array, the copy behaves as if the source range was first copied into a temporary array. That is what makes shifting elements for insert and remove work. - Shallow copy. For object arrays only references are copied; both arrays then point to the same objects.
- Primitive types must match exactly.
int[]tolong[]is an error even thoughintwidens tolong. Reference arrays follow assignment rules (String[]intoObject[]is fine). length == 0is legal, as long as the positions are within0..array.lengthinclusive.- Easier alternatives.
Arrays.copyOf(array, newLength)andArrays.copyOfRange(array, from, to)allocate the destination for you, andarray.clone()duplicates a whole array.arraycopyis for filling an existing array. - Performance. A memory move without a per-element loop; the JIT replaces it with intrinsic code. Copying a million
intvalues takes about a millisecond. - Not thread safe. Another thread modifying either array while it is copied can produce a mix of old and new values.
- Multi-dimensional arrays: copying
int[][]copies row references, so the rows are shared. Copy each row to get a deep copy.