In this tutorial, we will learn about how to copy an array in Java. Throughout this tutorial, we will explore various methods to efficiently copy one-dimensional in Java. We will enhance our understanding of these techniques through illustrative examples.
Java provides multiple approaches for array copying. In Java, you have the flexibility to copy arrays through two distinct approaches: utilizing the direct copy method from java.util package
or employing the System
class. Furthermore, Java encompasses a clone method that enables the cloning of entire arrays.
Copy an array in JAVA using the following ways
- Manual Copying using for loop
- By Using System.arraycopy()
- By Using Arrays.copyOf()
- By Using Arrays.copyOfRange()
- By Using Object.clone()
Related Post: How to convert char array to String
1.) Manual copying using for loop
Manual copying of an array using a for loop in Java refers to the process of duplicating the contents of one array into another array element by element, by iterating over each element of the source array and assigning it to the corresponding index of the destination array.
Example:
public class main{
public static void main(String args[])
{
int[] sourceArray = {1, 2, 3, 4, 5};
int[] destinationArray = new int[sourceArray.length];
for (int i = 0; i < sourceArray.length; i++) {
destinationArray[i] = sourceArray[i];
}
// Optional: Print the contents of the arrays for verification
System.out.println("Source Array:");
for (int num : sourceArray) {
System.out.print(num + " ");
}
System.out.println("\nDestination Array (Copied Array):");
for (int num : destinationArray) {
System.out.print(num + " ");
}
}
}
Output:
Source Array: 1 2 3 4 5
Destination Array (Copied Array): 1 2 3 4 5
2. Copy an Array using System.arraycopy()
The Java language provides a method called System.arraycopy()
that facilitates efficient and concise array copying. By employing this method, you can effortlessly duplicate either a specific section or the entire source array into a destination array. The copied elements from the source array are seamlessly inserted into the destination array, commencing from a designated index. Importantly, this method guarantees the preservation of the original values in the destination array beyond the copied elements, ensuring their integrity remains unaffected.
Example:
import java.util.Arrays;
public class CopyArrayExample {
public static void main(String[] args) {
int[] sourceArray = { 1, 2, 3, 4, 5 };
int[] destinationArray = new int[sourceArray.length];
// Copy the elements from sourceArray to destinationArray
System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length);
// Print the original and copied arrays
System.out.println("Original Array: " + Arrays.toString(sourceArray));
System.out.println("Copied Array: " + Arrays.toString(destinationArray));
}
}
Output
Original Array: [1, 2, 3, 4, 5]
Copied Array: [1, 2, 3, 4, 5]
3. Copy an Array Using Arrays.copyOf()
In Java, when it comes to copying arrays, an excellent option is to utilize the Arrays.copyOf()
method. This method offers a convenient and straightforward approach that simplifies the array copying process. By specifying the desired length, Arrays.copyOf() seamlessly handles the copying process, effortlessly generating a new array that contains the desired elements from the original array.
Example:
import java.util.Arrays;
public class CopyArrayExample {
public static void main(String[] args) {
// Create the original array
int[] originalArray = {1, 2, 3, 4, 5};
// Copy the array using Arrays.copyOf()
int[] copiedArray = Arrays.copyOf(originalArray, originalArray.length);
// Print the original and copied arrays
System.out.println("Original Array: " + Arrays.toString(originalArray));
System.out.println("Copied Array: " + Arrays.toString(copiedArray));
}
}
Output
Original Array: [1, 2, 3, 4, 5]
Copied Array: [1, 2, 3, 4, 5]
4. Copy an Array Using Arrays.copyOfRange()
Arrays.copyOfRange()
method offers several advantages. First, it simplifies the array copying process by handling the creation of a new array and copying the elements in a single step. Additionally, it ensures that the resulting copied array is of the correct size, eliminating the need for manual resizing. This method allows you to create a new array that contains a specified range of elements from the original array.
Example:
import java.util.Arrays;
public class CopyArrayExample {
public static void main(String[] args) {
int[] sourceArray = {1, 2, 3, 4, 5};
// Copy the entire sourceArray to a new array
int[] destinationArray = Arrays.copyOfRange(sourceArray, 0, sourceArray.length);
// Modify the sourceArray
sourceArray[0] = 100;
// Print the sourceArray and destinationArray
System.out.println("Source Array: " + Arrays.toString(sourceArray));
System.out.println("Destination Array: " + Arrays.toString(destinationArray));
}
}
Output
Source Array: [100, 2, 3, 4, 5]
Destination Array: [1, 2, 3, 4, 5]
5. Copy an Array Using Object.clone()
The Object.clone()
method in Java is a built-in method provided by the java.lang.Object
class. It allows the creation of a new copy, known as a clone, of an existing object. When applied to an array, Object.clone()
enables the duplication of the array’s contents into a new array, preserving the original data structure while providing a separate and independent copy.
Example:
public class ArrayCloneExample {
public static void main(String[] args) {
int[] originalArray = { 1, 2, 3, 4, 5 };
int[] copiedArray = copyArray(originalArray);
// Modify the original array
originalArray[0] = 10;
System.out.println("Original Array: ");
printArray(originalArray);
System.out.println("\nCopied Array: ");
printArray(copiedArray);
}
public static int[] copyArray(int[] array) {
return array.clone();
}
public static void printArray(int[] array) {
for (int num : array) {
System.out.print(num + " ");
}
System.out.println();
}
}
Output
Original Array:
10 2 3 4 5
Copied Array:
1 2 3 4 5