Loading
svg
Open

How to Clone Array in Java

January 1, 20245 min read

If the contents of the main array need to remain unchanged, it is necessary to copy the contents of the array to another array. This type of work is called cloning an array. An array can be cloned in Java without using any built-in functions or without using any special built-in functions. Java has several built-in functions to clone an array. This tutorial shows various ways to clone an array in Java.

Example 1: Copy an array using the assignment (=) operator

In Java, when one array variable is assigned to another array variable, both the array variables will refer to the same location of memory. Therefore, if the value of an index of one array is updated, the value of the same index of the other array is modified. To know how an array works when it is assigned to another array variable, create a Java file with the following code. The second index value of the second array is updated in the code after assigning the first array to the second array.

public class CloneArray1
,
public static void main,string[] logic,
,

,declare main array
string [] mainArray = , “Book”, “pen”, “Ruler”, “paper” ,,

System.out.println,“Values ​​of main array:”,,
For ,string val: mainArray,
,
system.out.print,val+ ,,,
,

,count out size of main array
int len ​​= mainArray.length;
,declare second array
string [] CopyArray = new String [len],

,View the first table from the second table
copyArray = mainArray;

,Change second value of both arrays
copied array[1] , “pencil”,

System.out.println,,\n\nValues ​​of main array after update: “,,
For ,string val: mainArray,
,
system.out.print,val+ ,,,
,
System.out.println,,\n\nValues ​​of second array after update: “,,
For ,string val:copyarray,
,
system.out.print,val+ ,,,
,
,
,

Output:

The following output shows that both arrays have the same value at the second index after the update:

Example 2: Using the clone() method

If you want to clone an array that will be stored in different locations in memory, you have to use Java's built-in method. Clone() is one of the methods that copies an array and if an index is updated or vice versa, the main array will not be affected by the copied array. Create a Java file with the following code to show the use of the clone() method to clone an array of strings. The main array and the copied array are printed after updating the fourth element of the copied array.

public class CloneArray2
,
public static void main,string[] logic,
,
,declare main array
string [] mainArray = , “Book”, “pen”, “Ruler”, “paper” ,,

,count out size of main array
int len ​​= mainArray.length;
,declare second array
string [] CopyArray = new String [len],

,copy first table
copyArray = mainArray.clone,,,

,Change second value of second array
copied array[3] , “pencil”,

System.out.println,“Values ​​of main array after update:”,,
For ,string val: mainArray,
,
system.out.print,val+ ,,,
,
System.out.println,,\n\nValues ​​of second array after update: “,,
For ,string val:copyarray,
,
system.out.print,val+ ,,,
,
,
,

Output:

The following output shows that the fourth index value of the main array has not changed after updating the fourth index of the copied array:

Example 3: Using ArrayCopy() Method

The arraycopy() method is another built-in method of Java to clone an array variable. There are four arguments in this method. The first argument contains the main array. The second argument contains the starting index. The third argument contains the copied array. The fourth argument contains the length of the array. Create a Java file with the following code that shows the use of the arraycopy() method to clone an array:

public class CloneArray3
,
public static void main,string[] logic,
,
,declare main array
int here [] mainArray = , 23, 67, 45, 90, 49 ,,

,count out size of main array
int len ​​= mainArray.length;
,declare second array
int here [] CopyArray = new int [len],

,copy first table
system.arecopy,main table, 0Copied array, 0lane,,

,replace the Last value of second array
copied array[len-1] , 80,

System.out.println,“Values ​​of main array after update:”,,
For ,int val:mainarray,
,
system.out.print,val+ ,,,
,
System.out.println,,\n\nValues ​​of second array after update: “,,
For ,int val:copyarray,
,
system.out.print,val+ ,,,
,
,
,

Output:

The following output shows that the last element of the main array has not changed after updating the last index of the copied array:

conclusion

The use of clone() and arraycopy() methods to clone an array in Java is shown using several examples in this tutorial.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg