In Java, System.arraycopy(src, srcPos, dest, destPos, length) copies length elements from src, starting at index srcPos, into dest, starting at index destPos. The destination must already exist and be big enough.
It is the fastest standard way to copy part of an array: to grow it, take a slice, or shift elements inside the same array.
A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.arraycopy(...). No import is needed, because classes in java.lang are always visible.
Syntax
Read it as "from src at srcPos, to dest at destPos, this many":
Java
int[] src = {1, 2, 3, 4, 5};
int[] dest = new int[5];
System.arraycopy(src, 0, dest, 0, src.length); // full copy
System.arraycopy(src, 1, dest, 0, 3); // dest starts 2, 3, 4
System.arraycopy(src, 0, src, 1, 4); // shift right inside src
Call
What it does
System.arraycopy(a, 0, b, 0, a.length)
Copies all of a to the start of b.
System.arraycopy(a, i + 1, a, i, n - i - 1)
Shifts the tail left by one: removes element i.
System.arraycopy(a, i, a, i + 1, n - i - 1)
Shifts the tail right by one: makes room at index i.
The parameters are typed Object, so array types are only checked when the program runs.
Library declarations
Java
// java.lang.System
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Parameters
Parameter
Description
src
The source array.
srcPos
First index to copy in src.
dest
The destination array; it is never created or resized.
destPos
Index in dest for the first copied element.
length
Number of elements to copy (0 copies nothing).
Return Value
Return Value
Description
void
Nothing. Only the target range of dest is overwritten.
Room for more.new int[6] starts as six zeros; copying the four scores to index 0 fills the first four places.
One new value.bigger[4] = 70 uses the first free place; index 5 keeps its default 0.
A slice by position.arraycopy(scores, 1, middle, 0, 2) starts at index 1 of scores and copies two elements, 75 and 60. The original array is unchanged.
Example 2 — Remove and Insert by Shifting Elements in the Same Array
Source and destination can be the same array. arraycopy handles the overlap correctly; a naive forward loop does not.
Close the gap. Copying the three elements from index 2 (30, 40, 50) to index 1 overwrites 20; the last slot still holds a stale 50, so it is reset to 0.
Why the loop fails. Copying forward, the loop writes 30 into index 2, then copies that same 30 again and again across the tail.
As if through a temporary array. An overlapping copy behaves as if the source range were first copied to a temporary array, so shifting 30, 40, 50 right keeps all three and frees index 1 for 99.
Example 3 — Too-Long Ranges, Wrong Types, and a Partial Copy
arraycopy checks ranges and types when it runs. Each problem is caught and its exception class name printed.
Java
import java.util.Arrays;
public class SystemArraycopyErrors {
public static void main(String[] args) {
int[] small = {1, 2, 3, 4};
int[] target = new int[10];
try {
System.arraycopy(small, 2, target, 0, 5);
} catch (IndexOutOfBoundsException e) {
System.out.println("Too long: " + e.getClass().getSimpleName());
}
long[] longs = new long[4];
try {
System.arraycopy(small, 0, longs, 0, 4);
} catch (ArrayStoreException e) {
System.out.println("int[] to long[]: " + e.getClass().getSimpleName());
}
Object[] mixed = {1, 2, "three", 4};
Integer[] numbers = new Integer[4];
try {
System.arraycopy(mixed, 0, numbers, 0, 4);
} catch (ArrayStoreException e) {
System.out.println("String into Integer[]: " + e.getClass().getSimpleName());
}
System.out.println("Copied before the error: " + Arrays.toString(numbers));
}
}
Output
Too long: ArrayIndexOutOfBoundsException
int[] to long[]: ArrayStoreException
String into Integer[]: ArrayStoreException
Copied before the error: [1, 2, null, null]
How It Works
Range past the end. Index 2 of a 4-element array leaves only 2 elements, not 5. Nothing is copied; an ArrayIndexOutOfBoundsException (a subclass of IndexOutOfBoundsException) is thrown.
No widening between primitive arrays. An int fits into a long variable, but copying between two different primitive array types throws ArrayStoreException before anything is copied.
Checked element by element. 1 and 2 are Integers and land in numbers; "three" is a String, so the copy stops there and the last two slots stay null.
Frequently Asked Questions
It copies a number of elements from a source index in one array to a destination index in another, existing array.
arraycopy copies into an array you already have. Arrays.copyOf creates and returns a new array of the length you ask for.
Yes. Overlapping ranges are copied as if through a temporary array, so shifting elements left or right works correctly.
No. For object arrays it copies the references, so both arrays point to the same objects.
IndexOutOfBoundsException (in practice ArrayIndexOutOfBoundsException) for bad ranges, ArrayStoreException for incompatible types, and NullPointerException for a null array.