System.arraycopy() is a built-in Java method used to copy elements from one array to another array efficiently. It can copy the entire array or only a selected range of elements.
- It works with both primitive arrays and object arrays.
- It allows us to specify the source position, destination position, and number of elements to copy.
public class Main {
public static void main(String[] args) {
int[] source = {10, 20, 30, 40, 50};
int[] destination = new int[5];
System.arraycopy(source, 0, destination, 0, 5);
for (int num : destination) {
System.out.print(num + " ");
}
}
}
Output
10 20 30 40 50
Here, all five elements from the source array are copied into the destination array.
Syntax
System.arraycopy(source, sourcePosition, destination, destinationPosition, length);
The method accepts five parameters:
- source: Array from which elements are copied.
- sourcePosition: Starting index in the source array.
- destination: Array where elements are copied.
- destinationPosition: Starting index in the destination array.
- length: Number of elements to copy.
How System.arraycopy() Works?
Suppose we have the following arrays:
int[] source = {10, 20, 30, 40, 50};
int[] destination = {0, 0, 0, 0, 0};
Now execute:
System.arraycopy(source, 1, destination, 2, 3);
The copying process is:
Source Array
[10, 20, 30, 40, 50]
└───────┘
Copy 3
elements
↓
Destination Array
[0, 0, 20, 30, 40]
↑
Start at index 2
Therefore, elements 20, 30, and 40, starting from index 1 of the source array, are copied starting at index 2 of the destination array.
Example: Copy Specific Elements of an Array
public class Main {
public static void main(String[] args) {
int[] source = {10, 20, 30, 40, 50};
int[] destination = {1, 2, 3, 4, 5};
System.arraycopy(source, 1, destination, 2, 3);
for (int num : destination) {
System.out.print(num + " ");
}
}
}
Output
1 2 20 30 40
In this example, three elements starting from index 1 of source are copied to destination starting at index 2.
Copying Object Arrays
System.arraycopy() can also copy elements between compatible object arrays.
public class Main {
public static void main(String[] args) {
String[] source = {"Java", "Python", "C++"};
String[] destination = new String[3];
System.arraycopy(source, 0, destination, 0, 3);
for (String language : destination) {
System.out.println(language);
}
}
}
Output
Java Python C++
For object arrays, the references are copied, not independent copies of the objects themselves.
Copying Elements Within the Same Array
System.arraycopy() can also copy elements within the same array, including overlapping ranges.
public class Main{
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
System.arraycopy(arr, 0, arr, 1, 4);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Output
10 10 20 30 40
Java handles the overlapping source and destination ranges correctly.
Java handles the overlapping source and destination ranges correctly.
Exceptions in System.arraycopy()
System.arraycopy() can throw exceptions when invalid arrays, indexes, or types are provided.
- NullPointerException: If the source or destination array is null.
- ArrayIndexOutOfBoundsException: If the specified positions or length go outside the valid array range.
- ArrayStoreException: If an element cannot be stored in the destination array because of an incompatible type