Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, you may often need to copy the contents of one array to another. In this tutorial, we will cover different ways to copy arrays in Java.
System.arraycopy()
The System.arraycopy()
method is a native method that provides a fast way to copy elements from one array to another. This method takes five parameters: the source array, the starting position in the source array, the destination array, the starting position in the destination array, and the number of elements to be copied.
Example:
public class CopyArrayExample { public static void main(String[] args) { int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = new int[sourceArray.length]; System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length); for (int value : destinationArray) { System.out.print(value + " "); } // Output: 1 2 3 4 5 } }
Arrays.copyOf()
The Arrays.copyOf()
method is a utility method provided by the java.util.Arrays
class. It creates a new array, copies the specified number of elements from the original array to the new one, and returns the new array.
Example:
import java.util.Arrays; public class CopyArrayExample { public static void main(String[] args) { int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = Arrays.copyOf(sourceArray, sourceArray.length); for (int value : destinationArray) { System.out.print(value + " "); } // Output: 1 2 3 4 5 } }
You can use a simple for
loop to iterate through the source array and copy each element to the destination array.
Example:
public class CopyArrayExample { 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]; } for (int value : destinationArray) { System.out.print(value + " "); } // Output: 1 2 3 4 5 } }
clone()
The clone()
method creates a new array and copies the contents of the original array to the new one. Note that this method does a shallow copy, which means that if the array contains objects, only the references to the objects are copied.
Example:
public class CopyArrayExample { public static void main(String[] args) { int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = sourceArray.clone(); for (int value : destinationArray) { System.out.print(value + " "); } // Output: 1 2 3 4 5 } }
In summary, there are multiple ways to copy arrays in Java, including using System.arraycopy()
, Arrays.copyOf()
, a for
loop, and the clone()
method. Each method has its advantages and use cases, so choose the one that best suits your needs.
Copying arrays in Java using System.arraycopy()
The System.arraycopy()
method is a low-level mechanism to copy a range of elements from one array to another.
int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = new int[5]; System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length); // destinationArray now contains {1, 2, 3, 4, 5}
Using Arrays.copyOf() for array copying in Java
The Arrays.copyOf()
method is a convenient way to create a new array and copy elements from an existing array.
int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = Arrays.copyOf(sourceArray, sourceArray.length); // destinationArray now contains {1, 2, 3, 4, 5}
How to clone an array in Java
You can use the clone()
method to create a shallow copy of an array.
int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = sourceArray.clone(); // destinationArray now contains {1, 2, 3, 4, 5}
Copying multidimensional arrays in Java
When copying multidimensional arrays, you need to copy each dimension separately.
int[][] sourceArray = {{1, 2}, {3, 4}}; int[][] destinationArray = new int[sourceArray.length][]; for (int i = 0; i < sourceArray.length; i++) { destinationArray[i] = Arrays.copyOf(sourceArray[i], sourceArray[i].length); }
Java array copyOfRange() method
The Arrays.copyOfRange()
method allows you to copy a specified range of elements from an array.
int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = Arrays.copyOfRange(sourceArray, 1, 4); // destinationArray now contains {2, 3, 4}
Copying elements between arrays in Java
You can copy elements between arrays using a loop or methods like System.arraycopy()
.
int[] sourceArray = {1, 2, 3}; int[] destinationArray = new int[sourceArray.length]; for (int i = 0; i < sourceArray.length; i++) { destinationArray[i] = sourceArray[i]; }
Shallow copy vs. deep copy of arrays in Java
Shallow copy creates a new array but retains references to objects in the original array. Deep copy creates a new array and copies the contents of objects as well.
// Shallow copy int[] originalArray = {1, 2, 3}; int[] shallowCopy = originalArray.clone(); // Deep copy int[] deepCopy = Arrays.copyOf(originalArray, originalArray.length);
Copying array elements with Arrays.copyOfRange() in Java
The Arrays.copyOfRange()
method can be used to copy a range of elements between arrays.
int[] sourceArray = {1, 2, 3, 4, 5}; int[] destinationArray = new int[3]; System.arraycopy(sourceArray, 1, destinationArray, 0, 3); // destinationArray now contains {2, 3, 4}