Array Copy in Java
In Java, copying an array can be done in several ways, depending on our needs such as shallow copy or deep copy. In this article, we will learn different methods to copy arrays in Java.
Copying Each Element Individually
Iterating each element of the given original array and copy one element at a time. With the usage of this method, it guarantees that any modifications to "b", will not alter the original array "a", as shown in below example.
// Java program to demonstrate copying
// copying an array by assigning
// elements one by one
public class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
int b[] = new int[a.length];
// Copying elements of a[] to b[]
for (int i = 0; i < a.length; i++)
b[i] = a[i];
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
// Java program to demonstrate copying
// copying an array by assigning
// elements one by one
public class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
int b[] = new int[a.length];
// Copying elements of a[] to b[]
for (int i = 0; i < a.length; i++)
b[i] = a[i];
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
Output
1 8 3 2 8 3
Explanation: In this example, after copying, the first element of b
(b[0]++
) is incremented, so b[0]
becomes 2
, but a[0]
remains 1
.
Using clone() method
In the previous method, we had to iterate over the entire array to make a copy. A more efficient approach is to use the clone() method in Java. This method provides a quick way to create a shallow copy of an array without manual iteration.
1. Cloning of Single-Dimensional Array
When you clone a single-dimensional array, such as Object[], a shallow copy is performed.

class Geeks {
public static void main(String args[])
{
int intArray[] = { 1, 2, 3 };
int cloneArray[] = intArray.clone();
// will print false as shallow copy is created
System.out.println(intArray == cloneArray);
for (int i = 0; i < cloneArray.length; i++) {
System.out.print(cloneArray[i] + " ");
}
}
}
class Geeks {
public static void main(String args[])
{
int intArray[] = { 1, 2, 3 };
int cloneArray[] = intArray.clone();
// will print false as shallow copy is created
System.out.println(intArray == cloneArray);
for (int i = 0; i < cloneArray.length; i++) {
System.out.print(cloneArray[i] + " ");
}
}
}
Output
false 1 2 3
Explanation:
- This program demonstrates cloning a one-dimensional array using the clone() method, which creates a shallow copy.
- The original and cloned arrays have the same contents but are different objects in memory (intArray == cloneArray returns false).
2. Cloning Multidimensional Array
A clone of a multi-dimensional array (like Object[][]) is a "shallow copy,"

// Java program to demonstrate
// cloning of multi-dimensional arrays
class Geeks {
public static void main(String args[])
{
int intArray[][] = { { 1, 2, 3 }, { 4, 5 } };
int cloneArray[][] = intArray.clone();
// will print false
System.out.println(intArray == cloneArray);
// will print true as shallow copy is created
// i.e. sub-arrays are shared
System.out.println(intArray[0] == cloneArray[0]);
System.out.println(intArray[1] == cloneArray[1]);
}
}
// Java program to demonstrate
// cloning of multi-dimensional arrays
class Geeks {
public static void main(String args[])
{
int intArray[][] = { { 1, 2, 3 }, { 4, 5 } };
int cloneArray[][] = intArray.clone();
// will print false
System.out.println(intArray == cloneArray);
// will print true as shallow copy is created
// i.e. sub-arrays are shared
System.out.println(intArray[0] == cloneArray[0]);
System.out.println(intArray[1] == cloneArray[1]);
}
}
Output
false true true
Explanation:
This program shows that cloning a multi-dimensional array creates a shallow copy—the top-level array is duplicated, but inner arrays are still shared references.
Using arraycopy() method
We can also use System.arraycopy() Method. The system is present in java.lang package. Its signature is as follows:
// Java program to demonstrate array
// copy using System.arraycopy()
public class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3 };
// Creating an array b[] of same size as a[]
int b[] = new int[a.length];
// Copying elements of a[] to b[]
System.arraycopy(a, 0, b, 0, 3);
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
// Java program to demonstrate array
// copy using System.arraycopy()
public class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3 };
// Creating an array b[] of same size as a[]
int b[] = new int[a.length];
// Copying elements of a[] to b[]
System.arraycopy(a, 0, b, 0, 3);
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
Output
1 8 3 2 8 3
Explanation: After copying with System.arraycopy()
, the first element of "b"
is incremented, so b[0]
becomes 2
, but a[0]
remains 1
.
Using copyOf() method of Arrays class
We can use Arrays.copyOf() method, if we want to copy the first few elements of an array or make a full copy of the array, we can use this method.
// Java program to demonstrate array
// copy using Arrays.copyOf()
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
// Copy elements of a[] to b[]
int b[] = Arrays.copyOf(a, 3);
// Change b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("");
// Iterating over array a[]
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
// Iterating over array b[]
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
// Java program to demonstrate array
// copy using Arrays.copyOf()
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
// Copy elements of a[] to b[]
int b[] = Arrays.copyOf(a, 3);
// Change b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("");
// Iterating over array a[]
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
// Iterating over array b[]
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
Output
1 8 3 2 8 3
Explanation: The Arrays.copyOf()
method copies the elements of "a"
to "b". A
fter modifying b[0]
, we see that only "b"
is affected.
Using copyOfRange() method of Arrays class
The Arrays.copyOfRange() method copies the specified range of the specified array into a new array.
Note: The range is specified by the indices [from, to)
, meaning the element at the from
index is included, but the element at the to
index is not.
Example:
// Java program to demonstrate array
// copy using Arrays.copyOfRange()
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3, 5, 9, 10 };
// Creating an array b[] and
// copying elements of a[] to b[]
int b[] = Arrays.copyOfRange(a, 2, 6);
// Changing b[] to verify that
// b[] is different from a[]
b[0]++; // Modify b[0] to check if it affects a[]
// Iterating over array a[]
System.out.println("");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
// Iterating over array b[]
System.out.println("");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
// Java program to demonstrate array
// copy using Arrays.copyOfRange()
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int a[] = { 1, 8, 3, 5, 9, 10 };
// Creating an array b[] and
// copying elements of a[] to b[]
int b[] = Arrays.copyOfRange(a, 2, 6);
// Changing b[] to verify that
// b[] is different from a[]
b[0]++; // Modify b[0] to check if it affects a[]
// Iterating over array a[]
System.out.println("");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
// Iterating over array b[]
System.out.println("");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
Output
1 8 3 5 9 10 4 5 9 10
Explanation:
b[0]++
increments the first element of arrayb[]
.- Since
b[]
is a separate copy of the specified range froma[]
, modifyingb[]
does not affecta[]
. - The output will confirm that changes in
b[]
do not alter the original arraya[]
.
Shallow Copy Vs Deep Copy
- Shallow Copy: It copies only the reference of nested objects, not the actual data. Making changes to nested objects affect both the original and the copy.
- Deep Copy: It creates a full, independent copy of the object, including nested objects. Making any changes to the copy do not affect the original object.