Array is a frequently utilized data structure in Java that declares multiple values in a single variable instead of declaring each value using a separate variable. To manipulate the arrays’ data, java provides a built-in Array class. This Array class helps users to solve real-world scenarios that we encounter while coding. One such scenario is to copy/duplicate one array to another one.
This article will demonstrate simple yet effective techniques to copy one array to another in Java.
How to Copy an Array in Java?
The array copy in Java implements certain methods and functions that help to copy one array to another. The need for array copy arises when the contents of the array need to be modified without reflecting any change in the original array. Moreover, an array can be copied to another array when a small change is required in the array and we do not want to disturb the original array.
There are different methods to copy an array in Java that are depicted below.
- Approach 1: Copy Array Using the for Loop in Java
- Approach 2: Copy Array Using the copyOfRange() Method in Java
- Approach 3: Copy Array Using arraycopy() in Java
- Approach 4: Copy Array Using copyOf() Method
- Approach 5: Copy Array Using the object.clone() Method
- Approach 6: Copy Array Using the Assignment(=) Operator
Let’s start with the for loop.
Approach 1: Copy Array Using the for Loop in Java
The for loop in Java can be used to iterate over the elements in the array according to the specified condition. The code below implements “for loop” to iterate and then copy the elements from the first array to the second array.
import java.util.Arrays;
//Declare the Class
class CopyExample1 {
public static void main(String[] args) {
//Declare the integer array
int [] src = {22,15,1,8,25,26,4};
//Declare the length
int [] dest = new int[7];
System.out.println("The Array is: ");
for (int x = 0; x < src.length; ++x) {
dest[x] = src[x];
}
//Convert the array to strings and then print
System.out.println(Arrays.toString(dest));
}
}
In the above code block:
- In the initial step, the required package is imported and a class named “ CopyExample1” is created.
- The integer array is declared as “src” and an empty array named “des” is created.
- The length of the destination(des) array is declared as “7”.
- The for loop iterates over the integers in the “src” array up to the specified length. Within the for loop, the assignment operator is used to copy the elements of the “src” array to the “des” array.
- The output is printed using the println() method along with the toString() method of Java.
Output
The output below shows that the array is copied from source to destination.

Approach 2: Copy Array Using the copyOfRange() Method in Java
The copyOfRange() method in Java copies the selected range of elements from one array to the other one without changing the contents of the original array as depicted in the syntax below.
copyOfRange(originalArr, startIndex, endIndex);
In the above syntax:
- The originalArr represents the array whose elements need to be copied.
- startIndex represents the starting range of elements to be copied.
- endIndex represents the last index of the range to be duplicated/copied. The endIndex is exclusive, which means it will not be copied to the selected array.
Here is a practical demonstration of the copyOfRange() method:
// Import the package required
import java.util.Arrays;
//Create a class
class CopyExample1 {
//The main() method of Java
public static void main(String[] args) {
//Declare the integer array
int[] array1 ={3,5,7,9,11,15,2,0};
// copy the entire array1 to array 2 from start to end
int[] array2 = Arrays.copyOfRange(array1, 0, array1.length);
//Print the array
System.out.println("The Array is " + Arrays.toString(array2));
// copy from index 1 to 4
int[] finalarr = Arrays.copyOfRange(array1, 1 , 4);
//Print the array after changes
System.out.println("Final Array is " + Arrays.toString(finalarr));
}
}
In the above code block:
- The required package is imported and the class is declared as “CopyExample1”.
- The two integer arrays are declared as “array1” and “array2”.
- The array2 implements the copyOfRange() method to copy the integers from array1 to array2 starting from the 0 index.
- The toString() function retrieves the integer values in the string format.
- The final array(finalarr) utilizes the copyOfRange() method to fetch the integer value starting from index one to the third index and then the println() method prints the output.
Output
The output below shows that the values located at index 1 to index 3 are printed. It is important to keep in mind that the fourth index is not included when the results are printed.

Approach 3: Copy Array Using arraycopy() in Java
The arraycopy() in Java copies a specific range of elements from the declared source array to the destination array. Additionally, it accepts a length parameter that specifies the total elements to copy. The code below implements the arraycopy method in Java.
arraycopy(Object src, int srcIndx, Object dest, int destIndx, int length)
In the above syntax:
- The src stands for the source array whose elements need to be copied.
- The srcIndx is the starting index of the source array. It shows the starting index for copying array elements.
- The des represents the destination array in which the original array’s elements will be copied.
- The destIndex refers to the starting index of the destination array where the elements of the original array will be copied.
- Length indicates the total elements to be copied.
Let us look at the code implementation below:
//Import the required packageimport java.util.Arrays;
//Declare the class in Java
class CopyExample3 {
public static void main(String[] args) {
//declare the first array
int[] Arr1 = {11,12,13,14,15,16,17};
int[] Arr3 = new int[7];
// Create an array as Arr2 of the length of array Arr1
int[] Arr2 = new int[Arr1.length];
// copy the complete array from index 0
System.arraycopy(Arr1, 0, Arr2, 0, Arr1.length);
System.out.println("Array2 = " + Arrays.toString(Arr2));
// copy element from index 3 on Arr1 array, copy element to index 2 of Arr3 array
// The length is 3
System.arraycopy(Arr1, 3, Arr3, 2, 3);
//Print the array
System.out.println("Array3 = " + Arrays.toString(Arr3));
}
}
In the above code block:
- The required package is imported to implement the arraycopy() method.
- A class is declared as “CopyExample3”.
- There are three integer arrays declared in the next step as Arr1, Arr2, and Arr3 respectively.
- Using array.copy() method, the first array “Arr1” is copied to the second array “Arr2”.
- The toString() method converts the integer object to the string object to print the output.
- The array.copy() method copies the value present at the third index of Arr1 to the second index of Arr3 along with the total length of elements as “3”.
- The output is printed utilizing the println() method.
Output
The output below shows that the elements of Arr1 from index positions 3 to 5 have been copied to Arr2.

Approach 4: Copy Array Using copyOf() Method
The copyOf() method in Java creates a copy of an array and pads of a value of 0 when the length of the copied array is more than the original array. Use the following syntax to implement the copyOf() method in Java:
copyOf(int[] originalArr, int newLen)
In the above syntax:
- originalArr represents the original array whose elements need to be copied.
- newLen represents the length of the new array to be created. The elements from the actual array will be copied to the newly created array.
The code below elaborates on the working of the copyOf() method in Java.
//import the required package
import java.util.Arrays;
//Declare the class
public class CopyExample4{
//main() method
public static void main(String args[])
{
// Declare an array
int[] Array1 = new int[] {3,5,15,8,10,12};
//Print the Original Array
System.out.println("The Original Array is:" + Arrays.toString(Array1));
// copy the array with the length declared as 7
int[] Array2 = Arrays.copyOf(Array1,7);
System.out.println("The New Array is:" + Arrays.toString(Array2));
//Instead of "0" at index 6 place 23
Array2[6] = 23;
System.out.println("The Copied Array is:" + Arrays.toString(Array2));
}
}
In the above code block:
- The integer array is declared and then printed using the Arrays.toString() method.
- The copyOf() method copies the array with a length of “7”.
- In the next step, the integer value of “23” is placed at the sixth index of the new array with a total length of 7.
- The modified array is printed accordingly.
Output
The output below shows that the original and new integer arrays are printed along with the copied array in which the 6th index from the new array is replaced with 23.

Approach 5: Copy of Array Using the object.clone() Method
The Java’s object.clone() method is utilized to make an exact copy of the object. Thus, it can be utilized with the below-stated syntax to copy arrays:
origianlArr.clone()
Let’s implement this syntax into a practical example to get a deep understanding of the clone() method.
class CopyExample5
{
//The main() method of Java
public static void main(String args[])
{
//Declare the integer array
int Array1[] = {15,20,33,87,25};
//Pass the array to the clone() method
int Array2[] = Array1.clone();
//Print the original array
System.out.println("Original Array:");
//The for loop to iterate over a sequence of integers in the array
for (int j = 0; j <Array1.length; j++) {
System.out.print(Array1[j] + " ");
}
System.out.println();
//Print the cloned array
System.out.println("Cloned Array:");
for (int j = 0; j <Array2.length; j++) {
System.out.print(Array2[j]+" ");
}
System.out.println("\n");
}
}
In the above code block:
- Two integer arrays are declared.
- The first array declared as “Array1” contains the integer values and the array declared as “Array2” contains the clone() method.
- In the next step, the for loop iterates over the original array and prints its elements to the output.
- Finally, the for loop is iterated over the cloned array to print its elements.
Output

Approach 6: Implementation Using the Assignment(=) Operator
The assignment operator helps to copy one array to another array in Java. The code below utilizes the assignment operator to copy an array to another array in Java.
class CopyExample {
public static void main(String[] args) {
//Declare an array of integer value
int [] numb = {11,15,17,20};
//Copy the array using the assignment operator
int [] posnumb = numb;
System.out.print("The Integers in the Array are: ");
//The 'for' loop to iterate the array and print the result
for (int number: posnumb) {
System.out.print(number + ",");
}
}
}
In the above code block:
- The class is declared as “CopyExample”.
- In the main method, two integer arrays are declared as “numb” and “posnumb”.
- The assignment (=) operator copies the first array “numb” to the second array “posnumb”.
- The for loop iterates over the integers in the array and prints the copied array.
Output
The output below shows that the Array is printed.

Problem With Assignment Operator While Using for Array Copy
The main issue while using the assignment operator is that if the values in the first array are changed, the values in the copied array get changed too. It is, therefore, not suggested to implement this operator.
class CopyExample {
public static void main(String[] args) {
//Declare an array of integer value
int [] numb = {11,15,17,20};
//At the first index the value is changed
numb[1] = 9;
//Copy the array using the assignment operator
int [] posnumb = numb;
System.out.print("The Integer Array is: ");
//The 'for' loop to iterate the array and print the result
for (int number: posnumb) {
System.out.print(number + ",");
}
}
}
In the above code block:
- The value “15” at the second index is changed to “9”.
- This change is also reflected in the second array “posnumb” which should be independent of the “numb” array.
Output
The output below shows that the change of value from 15 to 9 is reflected in the copied array using the assignment operator in Java.

This brings us to conclude the write-up on different methods to copy an array in Java.
Conclusion
Array copy in Java is a process of copying the elements of the actual array to a new array. For this purpose, different methods are used in Java, such as copyOfRange(), arraycopy(), copyOf(), and object.clone(). Moreover, the traditional for loop and assignment operator can also be utilized to perform the same task. All these approaches can effectively copy the selected array to another array in Java.