Clean • Professional
In Java, arrays are fixed in size, and sometimes you need to create a copy of an existing array — either fully or partially.
There are multiple ways to copy arrays in Java, ranging from manual loops to built-in methods like System.arraycopy(), Arrays.copyOf(), and Arrays.copyOfRange().
Java provides multiple ways to copy arrays — from simple loops to advanced built-in methods.

You can copy elements one by one using a for or for-each loop.
public class ManualCopyExample {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = new int[original.length];
for (int i = 0; i < original.length; i++) {
copy[i] = original[i];
}
System.out.print("Copied Array: ");
for (int n : copy)
System.out.print(n + " ");
}
}
Output:
Copied Array: 10 20 30 40 50
Points to remember:
System.arraycopy()The fastest and most efficient way to copy arrays in Java.
public class SystemArrayCopyExample {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] copy = new int[original.length];
System.arraycopy(original, 0, copy, 0, original.length);
System.out.print("Copied Array: ");
for (int n : copy)
System.out.print(n + " ");
}
}
Output:
Copied Array: 1 2 3 4 5
Points to remember:
Arrays.copyOf() and Arrays.copyOfRange()Java’s java.util.Arrays class provides two useful methods for copying arrays.
Example 1 – Arrays.copyOf()
import java.util.Arrays;
public class CopyOfExample {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] copy = Arrays.copyOf(original, original.length);
System.out.println("Copied Array: " + Arrays.toString(copy));
}
}
Output:
Copied Array: [10, 20, 30, 40, 50]
Example 2 – Arrays.copyOfRange()
import java.util.Arrays;
public class CopyOfRangeExample {
public static void main(String[] args) {
int[] original = {10, 20, 30, 40, 50};
int[] subArray = Arrays.copyOfRange(original, 1, 4);
System.out.println("Copied Range: " + Arrays.toString(subArray));
}
}
Output:
Copied Range: [20, 30, 40]
Points to remember:
clone() MethodEvery array in Java has a clone() method to make a copy.
public class CloneArrayExample {
public static void main(String[] args) {
int[] original = {5, 10, 15, 20};
int[] copy = original.clone();
System.out.println("Original Array: " + java.util.Arrays.toString(original));
System.out.println("Cloned Array: " + java.util.Arrays.toString(copy));
}
}
Output:
Original Array: [5, 10, 15, 20]
Cloned Array: [5, 10, 15, 20]
Points to remember:
Java 8 Streams offer a functional way to copy or filter arrays.
import java.util.Arrays;
public class StreamCopyExample {
public static void main(String[] args) {
int[] original = {3, 6, 9, 12, 15};
int[] copy = Arrays.stream(original).toArray();
System.out.println("Copied Array: " + Arrays.toString(copy));
}
}
Output:
Copied Array: [3, 6, 9, 12, 15]
Points to remember:
System.arraycopy() for large arrays.| Method | Best Use Case | Performance |
|---|---|---|
| Manual Loop | Small arrays or beginners | Slow |
System.arraycopy() | High-speed copying | Fastest |
Arrays.copyOf() / copyOfRange() | Resizing / range copy | Efficient |
clone() | Simple shallow copy | Moderate |
| Stream API | Functional programming | Slightly slower |