Open In App

Array Copy in Java

Last Updated : 25 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Share
94 Likes
Like
Report

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.

Example:

Assigning one array to another is an incorrect approach to copying arrays. It only creates a reference to the original array. If any changes are made to one array, it will reflect in the other since they both point to the same memory location.


Output
Contents of a[] 
2 8 3 

Contents of b[] 
2 8 3 

Methods to Copy Array 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.


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.


Output
1 8 3 
2 8 3 

Explanation: This example uses the clone() method to copy "a" into "b". After incrementing b[0], we see that only "b" changes, while "a" stays the same.


Using arraycopy() method

We can also use System.arraycopy() Method. The system is present in java.lang package. Its signature is as follows: 


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.


Output
1 8 3 
2 8 3 

Explanation: The Arrays.copyOf() method copies the elements of "a" to "b". After 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:


Output
1 8 3 5 9 10 
4 5 9 10 

Explanation:

  • b[0]++ increments the first element of array b[].
  • Since b[] is a separate copy of the specified range from a[], modifying b[] does not affect a[].
  • The output will confirm that changes in b[] do not alter the original array a[].\

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.




Next Article
Article Tags :
Practice Tags :

Similar Reads