articles

Home / DeveloperSection / Articles / Arrays in Java: Cloning an Arrays (Part-7)

Arrays in Java: Cloning an Arrays (Part-7)

David Miller 2058 13-May-2016

To make a copy of an array, we call the clone method on the array object. This is illustrated in this implementation:

import java.util.Arrays;

public class MindStickCloneTest
 {
          public static void main(String[] args)
              {
                   float[] floatArray = { 5.0f, 3.0f, 2.0f, 1.5f };
                   float[] floatArrayCopy = floatArray.clone();
                   System.out.println(Arrays.toString(floatArray) + " - Original");
                   System.out.println(Arrays.toString(floatArrayCopy) + " - Copy");
                   System.out.println();
                   System.out.println("Modifying the second element of the original array");                  floatArray[1] = 20;                    System.out.println(Arrays.toString(floatArray)                        + " - Original after modification");
                   System.out.println(Arrays.toString(floatArrayCopy) + " - Copy");
                   System.out.println();
                   System.out.println("Modifying the third element of the copy array");
                   floatArrayCopy[2] = 30;
                   System.out.println(Arrays.toString(floatArray) + " - Original");
                   System.out.println(Arrays.toString(floatArrayCopy)
                                + " - Copy array after modification");

          }

}

 In the main method, we declare an array of floating-point numbers called floatArray with its elements initialized to some value. To make a copy of this array, we use the following statement:

float[] floatArrayCopy = floatArray.clone();

The clone method copies all the elements of the floatArray into a new array called floatArrayCopy. To confirm that a new copy is made, we will try modifying an element of each of the two arrays, printing both each time.

To print the contents of an array, we use the expression Arrays.toString(floatArray), where Arrays is a Java-supplied class available since Java 2.

The toString() method of this class takes an argument of the array type and converts its contents to a string. Another approach to print the elements of an array would be to use a traditional loop to iterate through all the array elements, printing each in the iteration cycle. The program first modifies the element at index 1 of the original array and then it modifies the element at index 2 of the copied array. Each time, both arrays are printed after modifications. The program output is shown here:


[5.0, 3.0, 2.0, 1.5] - Original
[5.0, 3.0, 2.0, 1.5] – Copy
Modifying the second element of the original array
[5.0, 20.0, 2.0, 1.5] - Original after modification
[5.0, 3.0, 2.0, 1.5] – Copy
Modifying the third element of the copy array
[5.0, 20.0, 2.0, 1.5] - Original
[5.0, 3.0, 30.0, 1.5] - Copy array after modification
 

I printed the legend after printing the array contents so that the elements of the two arrays appear one below the other for ease of comparison.

One more important thing I want to discuss here about the way of cloning. We can clone an object in java in two ways:

·      Deep Copy

·      Shallow Copy

 

The clone method performs a shallow copy and not a deep copy. A shallow copy copies only the “surface” portion of an object. The actual object consists of this “surface,” plus all the objects that the references are pointing to, plus all the objects those objects are pointing to, and so on. Copying this entire web of objects is called a deep copy.


Updated 16-Dec-2017

Leave Comment

Comments

Liked By