Java Array Copying Techniques: A Comprehensive Guide
Java Array Copying Techniques
Learn how to duplicate one‑dimensional and two‑dimensional arrays in Java using assignment, loops, System.arraycopy, Arrays.copyOfRange, and more, with clear examples and best‑practice tips.
1. Copying with the Assignment Operator
Assigning one array variable to another is the simplest method. It copies the reference, not the data.
class Main {
public static void main(String[] args) {
int [] numbers = {1, 2, 3, 4, 5, 6};
int [] positiveNumbers = numbers; // reference copy
for (int number: positiveNumbers) {
System.out.print(number + ", ");
}
}
}
Output:
1, 2, 3, 4, 5, 6
Both numbers and positiveNumbers now point to the same array object. Changing one element affects the other:
class Main {
public static void main(String[] args) {
int [] numbers = {1, 2, 3, 4, 5, 6};
int [] positiveNumbers = numbers;
numbers[0] = -1;
for (int number: positiveNumbers) {
System.out.print(number + ", ");
}
}
}
Output:
-1, 2, 3, 4, 5, 6
This is a shallow copy. To avoid unintended side effects, use a deep copy technique.
2. Deep Copy with a Loop
Iterating over each element and assigning it to a new array creates an independent copy.
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int [] source = {1, 2, 3, 4, 5, 6};
int [] destination = new int[source.length];
for (int i = 0; i < source.length; ++i) {
destination[i] = source[i];
}
System.out.println(Arrays.toString(destination));
}
}
Output:
[1, 2, 3, 4, 5, 6]
Because source and destination are distinct objects, modifications to one do not affect the other.
3. Using System.arraycopy()
Java’s System.arraycopy method offers a fast, built‑in way to copy array segments.
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
src– source arraysrcPos– starting index in the sourcedest– destination arraydestPos– starting index in the destinationlength– number of elements to copy
Example:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] n1 = {2, 3, 12, 4, 12, -2};
int[] n2 = new int[n1.length];
System.arraycopy(n1, 0, n2, 0, n1.length);
System.out.println("n2 = " + Arrays.toString(n2));
int[] n3 = new int[5];
System.arraycopy(n1, 2, n3, 1, 2);
System.out.println("n3 = " + Arrays.toString(n3));
}
}
Output:
n2 = [2, 3, 12, 4, 12, -2] n3 = [0, 12, 4, 0, 0]
4. Copying with Arrays.copyOfRange()
When you need a new array that contains a specific slice, Arrays.copyOfRange is ideal.
import java.util.Arrays;
class ArraysCopy {
public static void main(String[] args) {
int[] source = {2, 3, 12, 4, 12, -2};
int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
System.out.println("destination1 = " + Arrays.toString(destination1));
int[] destination2 = Arrays.copyOfRange(source, 2, 5);
System.out.println("destination2 = " + Arrays.toString(destination2));
}
}
Output:
destination1 = [2, 3, 12, 4, 12, -2] destination2 = [12, 4, 12]
5. Copying 2D Arrays with a Loop
Deep copying a two‑dimensional array requires copying each row individually.
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new int[source.length][];
for (int i = 0; i < destination.length; ++i) {
destination[i] = new int[source[i].length];
for (int j = 0; j < destination[i].length; ++j) {
destination[i][j] = source[i][j];
}
}
System.out.println(Arrays.deepToString(destination));
}
}
Output:
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
6. 2D Array Copy with System.arraycopy()
Replace the inner loop with arraycopy for cleaner code.
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new int[source.length][];
for (int i = 0; i < source.length; ++i) {
destination[i] = new int[source[i].length];
System.arraycopy(source[i], 0, destination[i], 0, destination[i].length);
}
System.out.println(Arrays.deepToString(destination));
}
}
Output:
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
Java
- Mastering C# Arrays: Declaration, Initialization, and Operations
- Mastering C++ Arrays: Declaration, Initialization, and Practical Examples
- Mastering C Arrays: Declaration, Initialization, and Common Operations
- Mastering Java Arrays: Declaration, Initialization, and Manipulation
- Mastering Java Multidimensional Arrays: 2D & 3D Examples
- Java Arrays Tutorial: Declaration, Creation, and Initialization – Example Code
- Master Java Arrays: A Comprehensive Guide to Efficient Data Handling
- C Arrays Explained: Efficient Data Storage & Access
- MATLAB: Mastering Multidimensional and Special Arrays
- Mastering Arrays in C#: Efficient Data Storage & Access