Java

How to Clone Arrays in Java

The content of the array is required to be copied into another array if the content of the main array needs to remain unchanged. This type of task is called the clone of an array. An array can be cloned in Java without using any built-in function or using a particular built-in function.  Java has many built-in functions to clone an array. Different ways to clone an array in Java are shown in this tutorial.

Example 1: Copy an Array Using the Assignment (=) Operator

In Java, when an array variable is assigned to another array variable, both array variables will refer to the same location of the memory. So, if the value of any index of one array is updated, the value of the same index of another array is modified. Create a Java file with the following code to know how the array works when it is assigned to another array variable. 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[] args)
      {
       
         //Declare the main array
         String [] mainArray = { "Book", "Pen", "Rular", "Paper" };
       
         System.out.println("The values of the main array: ");
            for (String val : mainArray)
            {
               System.out.print(val + " ");
            }
   
         //Count the size of the main array
         int len = mainArray.length;
            //Declare the second array
            String [] copiedArray = new String [len];
   
            //Refer to the first array by the second array
            copiedArray = mainArray;
   
            //Change the second value of both arrays
            copiedArray[1] = "Pencil";
   
            System.out.println("\n\nThe values of the main array after update: ");
            for (String val : mainArray)
            {
                  System.out.print(val + " ");
            }
            System.out.println("\n\nThe values of the second array after update: ");
            for (String val : copiedArray)
            {
                  System.out.print(val + " ");
            }
    }
}

 
Output:

The following output shows that the second index of both arrays contains the same value after the update:

Example 2: Using the Clone() Method

If you want to make a clone of an array that will be stored in different locations of the memory, you have to use a built-in method of Java. The clone() is one of the methods that make a copy of an array and the main array will not be affected by the copied array if any index is updated or vice versa. 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 copied array are printed after updating the fourth element of the copied array.

public class CloneArray2
{
    public static void main(String[] args)
{
         //Declare the main array  
         String [] mainArray = { "Book", "Pen", "Rular", "Paper" };
   
         //Count the size of the main array
         int len = mainArray.length;
            //Declare the second array
            String [] copiedArray = new String [len];
   
            //Make a copy of the first array
            copiedArray = mainArray.clone();
   
            //Change the second value of second array
            copiedArray[3] = "Pencil";
   
            System.out.println("The values of the main array after update: ");
            for (String val : mainArray)
            {
                  System.out.print(val + " ");
            }
            System.out.println("\n\nThe values of the second array after update: ");
            for (String val : copiedArray)
            {
                  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 the Arraycopy() Method

The arraycopy() method is another built-in method of Java to make a clone of an array variable. This method has four arguments. 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 uses of the arraycopy() method to make a clone of an array:

public class CloneArray3
{
    public static void main(String[] args)
{  
         //Declare the main array
         int [] mainArray = { 23, 67, 45, 90, 49 };
   
         //Count the size of the main array
         int len = mainArray.length;
           //Declare the second array
           int [] copiedArray = new int [len];
   
           //Make a copy of the first array
           System.arraycopy(mainArray, 0, copiedArray, 0, len);
   
           //Change the last value of the second array
           copiedArray[len-1] = 80;
   
           System.out.println("The values of the main array after update: ");
           for (int val : mainArray)
           {
                System.out.print(val + " ");
           }
           System.out.println("\n\nThe values of the second array after update: ");
           for (int val : copiedArray)
           {
               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 uses of the clone() and arraycopy() methods to make a clone of an array in Java are shown in this tutorial using multiple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.