Copy Array in Java
Problem
Copying an array means creating a brand-new array holding the same values, so changes to one array never affect the other.
Given an array of integers, create a copy of it.
Java Program
public class CopyArray {
public static void main(String[] args) {
int[] original = {3, 6, 9, 12};
int[] copy = new int[original.length];
for (int i = 0; i < original.length; i++) {
copy[i] = original[i]; // copies the value, not the array reference
}
System.out.println("Copy: " + java.util.Arrays.toString(copy));
}
}Output
Core Logic
Allocating a brand-new array of the same size, then copying each element across one at a time, produces an independent duplicate.
- 1
int[] copy = new int[original.length]allocates a new array sized to match the original. - 2A loop visits every index from
0tooriginal.length - 1. - 3
copy[i] = original[i]copies each value across into the new array. - 4The two arrays now hold identical values, but are separate objects in memory.
[3, 6, 9, 12], each of the four values is copied into copy at the same index it held in original.Key Point: Assigning copy = original directly would NOT create a copy — both variables would point to the same array, so changing one would change the other. Only copying element by element (or using a copying method) produces a truly independent array.
Why: Every element is visited once, and the new array holds its own copy of all n values.
Key Concepts
Approach 2: Using Arrays.copyOf()
import java.util.Arrays;
public class CopyArrayUsingCopyOf {
public static void main(String[] args) {
int[] original = {3, 6, 9, 12};
// copyOf() allocates a new array of the given length and copies into it
int[] copy = Arrays.copyOf(original, original.length);
System.out.println("Copy: " + Arrays.toString(copy));
}
}
Output
Core Logic
In real code, there's no reason to loop manually — Arrays.copyOf() already builds a correctly sized copy in one call.
- 1
Arrays.copyOf(original, original.length)takes the source array and the length of the new array to create. - 2It allocates a new array of that length and copies as many elements as will fit.
- 3Passing
original.lengthas the target length copies every element with none left out.
Arrays.copyOf(new int[]{3, 6, 9, 12}, 4) returns a brand-new array holding the same four values.Key Point: Passing a length shorter than the original truncates the copy, and passing a longer length pads the extra slots with 0 — the target length doesn't have to match the source exactly.
Why: Arrays.copyOf() still copies every element into a newly allocated array, the same underlying work as the manual loop.
Key Concepts
Approach 3: Using System.arraycopy()
public class CopyArrayUsingArraycopy {
public static void main(String[] args) {
int[] original = {3, 6, 9, 12};
int[] copy = new int[original.length];
// Copies original.length elements from original[0] into copy[0]
System.arraycopy(original, 0, copy, 0, original.length);
System.out.println("Copy: " + java.util.Arrays.toString(copy));
}
}
Output
Core Logic
System.arraycopy() is the low-level method Arrays.copyOf() itself uses internally — calling it directly skips one layer of indirection.
- 1A new array
copyis still allocated first, sized to matchoriginal. - 2
System.arraycopy(original, 0, copy, 0, original.length)copies elements fromoriginalstarting at index 0, intocopystarting at index 0, fororiginal.lengthelements. - 3This is a native method, implemented outside plain Java code for speed.
System.arraycopy({3, 6, 9, 12}, 0, copy, 0, 4) fills copy with the same four values.Key Point: System.arraycopy() is what both Arrays.copyOf() and Object.clone() on arrays ultimately call under the hood — it's the fastest way to copy array data in Java.
Why: The native copy still has to move every element into the newly allocated array, the same total work as the other two versions.