How to Copy Array in Java: Syntax and Examples
In Java, you can copy an array using
System.arraycopy(), Arrays.copyOf(), or the array's clone() method. These methods create a new array with the same elements as the original.Syntax
Here are common ways to copy an array in Java:
System.arraycopy(src, srcPos, dest, destPos, length): Copies elements fromsrcstarting atsrcPostodeststarting atdestPosforlengthelements.Arrays.copyOf(original, newLength): Returns a new array copying the firstnewLengthelements fromoriginal.array.clone(): Creates a new array that is a shallow copy ofarray.
java
System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length); int[] newArray = java.util.Arrays.copyOf(originalArray, originalArray.length); int[] clonedArray = originalArray.clone();
Example
This example shows how to copy an integer array using the three common methods and prints the copied arrays.
java
import java.util.Arrays; public class ArrayCopyExample { public static void main(String[] args) { int[] original = {1, 2, 3, 4, 5}; // Using System.arraycopy int[] copy1 = new int[original.length]; System.arraycopy(original, 0, copy1, 0, original.length); // Using Arrays.copyOf int[] copy2 = Arrays.copyOf(original, original.length); // Using clone method int[] copy3 = original.clone(); System.out.println("Copy using System.arraycopy: " + Arrays.toString(copy1)); System.out.println("Copy using Arrays.copyOf: " + Arrays.toString(copy2)); System.out.println("Copy using clone(): " + Arrays.toString(copy3)); } }
Output
Copy using System.arraycopy: [1, 2, 3, 4, 5]
Copy using Arrays.copyOf: [1, 2, 3, 4, 5]
Copy using clone(): [1, 2, 3, 4, 5]
Common Pitfalls
One common mistake is to assign one array to another using =, which copies only the reference, not the actual array. This means changes to one array affect the other.
Also, clone() and System.arraycopy() perform shallow copies, so if the array contains objects, only references are copied, not the objects themselves.
java
public class WrongCopy { public static void main(String[] args) { int[] original = {1, 2, 3}; int[] wrongCopy = original; // This copies reference, not array wrongCopy[0] = 99; System.out.println("Original after change: " + java.util.Arrays.toString(original)); // Correct way using clone int[] correctCopy = original.clone(); correctCopy[0] = 100; System.out.println("Original after correct copy change: " + java.util.Arrays.toString(original)); } }
Output
Original after change: [99, 2, 3]
Original after correct copy change: [99, 2, 3]
Quick Reference
| Method | Description | Notes |
|---|---|---|
| System.arraycopy() | Copies elements from source to destination array | Fast, requires destination array pre-allocated |
| Arrays.copyOf() | Creates new array copying specified length | Easy to use, returns new array |
| clone() | Creates shallow copy of the array | Simple, shallow copy only |
| Assignment (=) | Copies reference only, not array content | Avoid if you want independent copy |
Key Takeaways
Use System.arraycopy, Arrays.copyOf, or clone() to copy arrays in Java.
Assignment copies only the reference, not the array content.
clone() and System.arraycopy() perform shallow copies for object arrays.
Arrays.copyOf returns a new array with specified length.
Always create a new array to avoid unexpected changes to the original.