Java System.arraycopy() – Examples
In this tutorial, we will learn about the Java System.arraycopy() function, and learn how to use this function to copy a subarray from the source array to destination array, with the help of examples.
arraycopy(src, srcPos, dest, destPos, length)
System.arraycopy() copies a sub-array from the specified source array, beginning at the specified index, to the specified index of the destination array.
Syntax
The syntax of arraycopy() function is
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
where
| Parameter | Description |
|---|---|
| src | Source array. |
| srcPos | Index in source array from which copy should happen. |
| dest | Destination array. |
| destPos | Index in destination array to which copy should happen. |
| length | Number of elements (length of subarray) to copy. |
Returns
The function returns static void.
Example 1 – arraycopy()
In this example, we will take two integer arrays: src and dest. We will copy src array from index srcPos = 1 to dest array from destPos = 2. The length of subarray we copy is length = 4.
Java Program
public class Example {
public static void main(String args[]) {
int src[] = {1, 2, 3, 4, 5, 6, 7};
int dest[] = {11, 12, 13, 14, 15, 16, 17};
int srcPos = 1;
int destPos = 2;
int length = 4;
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
System.arraycopy(src, srcPos, dest, destPos, length);
System.out.println("\nAfter copy\n");
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
}
public static void printArray(String name, int[] arr) {
System.out.print(name + '[');
for(int i: arr) {
System.out.print(i+", ");
}
System.out.println("]");
}
}
Output
Source Array : [1, 2, 3, 4, 5, 6, 7, ]
Destination Array : [11, 12, 13, 14, 15, 16, 17, ]
After copy
Source Array : [1, 2, 3, 4, 5, 6, 7, ]
Destination Array : [11, 12, 2, 3, 4, 5, 17, ]
Example 2 – arraycopy() – destPos+length > dest.length
In this example, we will choose destination array dest and length, such that destPos+length > dest.length.
Since, the index would go out of bounds for dest array while copying, arraycopy() should throw java.lang.ArrayIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String args[]) {
int src[] = {1, 2, 3, 4, 5, 6, 7};
int dest[] = {11, 12, 13, 14};
int srcPos = 1;
int destPos = 2;
int length = 4;
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
System.arraycopy(src, srcPos, dest, destPos, length);
System.out.println("\nAfter copy\n");
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
}
public static void printArray(String name, int[] arr) {
System.out.print(name + '[');
for(int i: arr) {
System.out.print(i+", ");
}
System.out.println("]");
}
}
Output
Source Array : [1, 2, 3, 4, 5, 6, 7, ]
Destination Array : [11, 12, 13, 14, ]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.base/java.lang.System.arraycopy(Native Method)
at Example.main(Example.java:12)
Example 3 – arraycopy()- srcPos+length > dest.length
In this example, we will choose source array src and length, such that srcPos+length > src.length.
Since, the index would go out of bounds for src array while copying, arraycopy() should throw java.lang.ArrayIndexOutOfBoundsException.
Java Program
public class Example {
public static void main(String args[]) {
int src[] = {1, 2, 3, 4};
int dest[] = {11, 12, 13, 14, 15, 16, 17};
int srcPos = 1;
int destPos = 2;
int length = 4;
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
System.arraycopy(src, srcPos, dest, destPos, length);
System.out.println("\nAfter copy\n");
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
}
public static void printArray(String name, int[] arr) {
System.out.print(name + '[');
for(int i: arr) {
System.out.print(i+", ");
}
System.out.println("]");
}
}
Output
Source Array : [1, 2, 3, 4, ]
Destination Array : [11, 12, 13, 14, 15, 16, 17, ]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at java.base/java.lang.System.arraycopy(Native Method)
at Example.main(Example.java:12)
Example 4 – arraycopy() – Copy Whole src to dest
In this example, we will take equal length integer arrays, and copy whole src to dest using arraycopy(). The starting positions that copy has to be done will be zero, which is the starting index for an array and the length will be the length of source array src.
Java Program
public class Example {
public static void main(String args[]) {
int src[] = {1, 2, 3, 4};
int dest[] = new int[4];
int srcPos = 0;
int destPos = 0;
int length = 4;
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
System.arraycopy(src, srcPos, dest, destPos, length);
System.out.println("\nAfter copy\n");
printArray("Source Array : ", src);
printArray("Destination Array : ", dest);
}
public static void printArray(String name, int[] arr) {
System.out.print(name + '[');
for(int i: arr) {
System.out.print(i+", ");
}
System.out.println("]");
}
}
Output
Source Array : [1, 2, 3, 4, ]
Destination Array : [0, 0, 0, 0, ]
After copy
Source Array : [1, 2, 3, 4, ]
Destination Array : [1, 2, 3, 4, ]
Example 5 – arraycopy() – Null Destination
In this example, we will take a destination array dest as null object. arraycopy() throws java.lang.NullPointerException if any of the source or destination arrays is null.
Java Program
public class Example {
public static void main(String args[]) {
int src[] = {1, 2, 3, 4};
int dest[] = null;
int srcPos = 0;
int destPos = 0;
int length = 4;
System.arraycopy(src, srcPos, dest, destPos, length);
}
}
Output
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.System.arraycopy(Native Method)
at Example.main(Example.java:9)
Example 6 – arraycopy() – Null Source
In this example, we will take a source array src as null object. arraycopy() throws java.lang.NullPointerException if any of the source or destination arrays is null.
Java Program
public class Example {
public static void main(String args[]) {
int src[] = null;
int dest[] = new int[4];
int srcPos = 0;
int destPos = 0;
int length = 4;
System.arraycopy(src, srcPos, dest, destPos, length);
}
}
Output
Exception in thread "main" java.lang.NullPointerException
at java.base/java.lang.System.arraycopy(Native Method)
at Example.main(Example.java:9)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java System.arraycopy() function, and also learnt how to use this function with the help of examples.
