Java Array Copying Methods: Loop, System.arraycopy, Arrays.copyOf, and clone
This article explains four Java array copying techniques—manual loop assignment, System.arraycopy, Arrays.copyOf (or copyOfRange), and clone—detailing their shallow or deep copy behavior, performance characteristics, and providing complete code examples with expected outputs.
Java provides four primary ways to copy arrays: manual loop assignment, System.arraycopy() , Arrays.copyOf() (or Arrays.copyOfRange ), and the clone() method.
Loop Copy
Loop copying uses a for loop to copy each element individually, resulting in a shallow copy and relatively slow performance.
Example code:
import java.util.Arrays;
public class KaoBei2 {
public static void main(String[] args) {
int[] array1 = {1,2,3,4,5,6,7};
int[] array2 = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
array2[i] = array1[i];
}
System.out.println(Arrays.toString(array2));
// Two‑dimensional array copy
int[][] array3 = {{1,2,3},{4,5,6}};
int[][] array4 = new int[2][3];
for (int i = 0; i < array3.length; i++) {
for (int j = 0; j < array3[i].length; j++) {
array4[i][j] = array3[i][j];
}
}
System.out.println(Arrays.deepToString(array4));
}
}Running this prints the copied one‑dimensional and two‑dimensional arrays.
System.arraycopy (shallow copy)
System.arraycopy is a native method implemented in C/C++ that performs a shallow copy; for non‑primitive types it copies references only, making it faster than a loop.
Signature:
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);Example code:
import java.util.Arrays;
public class kaoBei1 {
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
int[] array2 = new int[array.length];
System.arraycopy(array, 0, array2, 0, array.length);
System.out.println(Arrays.toString(array2));
// Two‑dimensional copy
int[][] array1 = {{1,2,3,5,9},{2,3,36,5,7}};
int[][] array3 = new int[2][5];
for (int i = 0; i < array1.length; i++) {
System.arraycopy(array1[i], 0, array3[i], 0, array1[i].length);
}
System.out.println(Arrays.deepToString(array3));
}
}The output shows both the one‑dimensional and two‑dimensional copies.
Arrays.copyOf (shallow copy)
Arrays.copyOf also performs a shallow copy because it internally calls System.arraycopy .
Relevant source snippet:
public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0) throw new IllegalArgumentException(from + " > " + to);
byte[] copy = new byte[newLength];
System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength));
return copy;
}Example code:
import java.util.Arrays;
public class KaoBei4 {
public static void main(String[] args) {
int[] array = new int[4];
System.out.println(Arrays.toString(array));
int[] array1 = new int[4];
array1 = Arrays.copyOf(array, array.length);
System.out.println(Arrays.toString(array1));
int[][] array2 = {{1,2,3,4},{2,6,7,5}};
int[][] array3 = new int[2][4];
array3 = Arrays.copyOf(array2, array2.length);
System.out.println(Arrays.deepToString(array3));
}
}The program prints the copied arrays, confirming the shallow‑copy behavior.
Object.clone
The clone() method is special: for ordinary objects it performs a deep copy, but for arrays it behaves as a shallow copy.
Example code:
import java.util.Arrays;
public class KaoBei3 {
public static void main(String[] args) {
int[] array1 = {1,2,3,5,9,8,7};
int[] array2 = new int[array1.length];
array2 = array1.clone();
System.out.println(Arrays.toString(array2));
int[][] array3 = {{1,2,3,4},{5,6,7}};
int[][] array4 = new int[2][];
for (int i = 0; i < array3.length; i++) {
array4[i] = array3[i].clone();
}
for (int i = 0; i < array3.length; i++) {
System.out.print(Arrays.toString(array4[i]));
}
}
}The output demonstrates that cloning an array of primitives yields a shallow copy, while cloning an array of objects requires each element to be cloned individually for a deep copy.
Object Copy and Array Copy Details
Cloning an object uses the protected native method protected native Object clone() throws CloneNotSupportedException; . To make it accessible, a class must implement Cloneable and expose a public clone() method.
When copying arrays that contain object references, the new array holds the same references (shallow copy). The article provides a demonstration with a custom class Aby implementing Cloneable to show how changes to the original affect the cloned array.
Overall, the article compares the performance and copy depth of the four techniques, offering practical code samples and execution results for developers to choose the appropriate method.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.