How to Copy One Array to Another in Java
Copying an array may seem straightforward, but Java offers several ways to do it, each suited to different situations. One important concept to understand is the difference between copying an array reference and creating an actual independent copy. This guide explains the most common techniques, including clone(), Arrays.copyOf(), System.arraycopy(), manual copying, and Arrays.copyOfRange(), along with the important distinction between shallow and deep copying.
Problem Statement
Given an array:
{1, 2, 3, 4, 5}
Create an independent copy so that modifying one array does not affect the other.
Why Simple Assignment Doesn't Copy an Array
int[] original = {1, 2, 3, 4, 5};
int[] copy = original; // NOT a copy
copy[0] = 99;
System.out.println(original[0]); // 99
This does not create a new array.
Instead, both original and copy point to the same array object in memory. Any modification through one reference is immediately visible through the other.
Method 1: Using clone()
int[] original = {1, 2, 3, 4, 5};
int[] copy = original.clone();
copy[0] = 99;
System.out.println(original[0]); // 1
Every array in Java supports the clone() method.
For one-dimensional primitive arrays, clone() creates a completely independent copy.
Method 2: Using Arrays.copyOf()
import java.util.Arrays;
int[] original = {1, 2, 3, 4, 5};
int[] copy = Arrays.copyOf(original, original.length);
Arrays.copyOf() works similarly to clone() but also allows you to resize the copied array.
int[] largerCopy = Arrays.copyOf(original, 10);
// [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
int[] smallerCopy = Arrays.copyOf(original, 3);
// [1, 2, 3]
If the new length is larger, the remaining positions are filled with default values.
Method 3: Using System.arraycopy()
int[] original = {1, 2, 3, 4, 5};
int[] copy = new int[original.length];
System.arraycopy(original, 0, copy, 0, original.length);
System.arraycopy() copies elements directly from one array into another.
Its syntax is:
System.arraycopy(source,
sourcePosition,
destination,
destinationPosition,
length);
This method is implemented natively inside the JVM and is highly optimized for copying large arrays.
Method 4: Using a Manual Loop
int[] original = {1, 2, 3, 4, 5};
int[] copy = new int[original.length];
for (int i = 0; i < original.length; i++) {
copy[i] = original[i];
}
This approach is easy to understand and demonstrates how array copying works internally, although built-in methods are generally preferred.
Method 5: Using Arrays.copyOfRange()
import java.util.Arrays;
int[] original = {1, 2, 3, 4, 5};
int[] partialCopy = Arrays.copyOfRange(original, 1, 4);
System.out.println(Arrays.toString(partialCopy));
// [2, 3, 4]
Arrays.copyOfRange() copies only a specific portion of an array.
The starting index is inclusive, while the ending index is exclusive.
Shallow Copy vs Deep Copy
Understanding shallow and deep copying is essential when working with multidimensional arrays or arrays of objects.
All of the methods above (clone(), Arrays.copyOf(), and System.arraycopy()) perform a shallow copy.
For one-dimensional primitive arrays, a shallow copy is completely sufficient because primitive values are copied directly.
However, for multidimensional arrays, only the outer array is copied while the inner arrays remain shared.
Shallow Copy Example
int[][] original = {
{1, 2},
{3, 4}
};
int[][] shallowCopy = original.clone();
shallowCopy[0][0] = 99;
System.out.println(original[0][0]); // 99
Both arrays still reference the same inner arrays.
Deep Copy Example
int[][] original = {
{1, 2},
{3, 4}
};
int[][] deepCopy = new int[original.length][];
for (int i = 0; i < original.length; i++) {
deepCopy[i] = original[i].clone();
}
deepCopy[0][0] = 99;
System.out.println(original[0][0]); // 1
Each row is cloned separately, creating a completely independent copy.
Step-by-Step Explanation
Why Assignment Doesn't Work
Assignment copies only the reference to the array, not its contents.
Both variables refer to the same memory location.
Why clone() and Arrays.copyOf() Work
For primitive arrays, these methods create a new array and copy every value into it.
The original and copied arrays become completely independent.
Why Shallow Copy Fails for 2D Arrays
A two-dimensional array is actually an array whose elements are references to other arrays.
A shallow copy duplicates only those references, not the inner arrays themselves.
Internal Working (Memory View)
Shallow Copy
original → [row0, row1]
copy → [row0, row1]
Both arrays reference the same rows.
Deep Copy
original → [row0A, row1A]
copy → [row0B, row1B]
Each row is independently cloned.
Real-Life Analogy
Imagine photocopying only the table of contents of a book while both copies still point to the same physical chapters.
If someone edits a chapter, both books appear to change.
A deep copy is like photocopying every page of the book so each copy becomes completely independent.
Best Practices
- Use
clone()for quick copies of one-dimensional primitive arrays. - Use
Arrays.copyOf()when you also need to resize the copied array. - Use
Arrays.copyOfRange()to copy only part of an array. - Use
System.arraycopy()when copying into an existing destination array or when maximum performance is required. - Perform a deep copy for multidimensional arrays or arrays containing mutable objects.
Common Mistakes
- Using
copy = originaland expecting an independent array. - Assuming
clone()performs a deep copy for 2D arrays. - Forgetting to deep copy mutable objects inside object arrays.
- Passing the wrong length to
Arrays.copyOf(), resulting in truncation or unwanted default values.
Expert Tips
Arrays.copyOf()is generally preferred overclone()because it also supports resizing.System.arraycopy()is the fastest option for copying large arrays because it is implemented as a native JVM method.- Deep copying arrays of custom objects requires copying each object individually, typically using a copy constructor or a custom cloning method.
Comparison Table
| Method | Supports Resizing? | Deep Copy for 2D Arrays? | Best Use Case |
|---|---|---|---|
clone() |
No | ❌ No | Quick copy of a 1D primitive array |
Arrays.copyOf() |
Yes | ❌ No | Copy with optional resizing |
System.arraycopy() |
Manual | ❌ No | Partial or high-performance copying |
| Manual Loop | Manual | ✅ Yes (if implemented manually) | Deep copying multidimensional arrays |
Frequently Asked Questions
Does copy = original actually copy an array?
No. It copies only the reference, so both variables point to the same array.
What is the difference between a shallow copy and a deep copy?
A shallow copy duplicates the outer array but shares nested objects or inner arrays. A deep copy duplicates every nested object, making the copy completely independent.
Is clone() enough for a one-dimensional int[]?
Yes. Since primitive values are copied directly, clone() creates a fully independent copy.
Is clone() enough for a 2D array?
No. It copies only the outer array. Each inner array must also be cloned to achieve a true deep copy.
What is the fastest way to copy a large array?
System.arraycopy() is generally the fastest because it uses native JVM memory-copy operations.
How do I copy only part of an array?
Use Arrays.copyOfRange(array, fromIndex, toIndex).
Can Arrays.copyOf() create a larger array?
Yes. Extra elements are filled with default values such as 0, false, or null.
Does the shallow/deep copy issue also apply to arrays of custom objects?
Yes. Arrays of custom objects contain references, so a shallow copy shares the same objects. A deep copy requires copying each object individually.