Collections

how to use clone() method on ArrayList in collections
Previous Home Next

 

The following example demonstrates how can you create a clone of an ArrayList using collections in java.

In this example we have created a class CollectionExample. For using ArrayList we have
to import the java.util package.
 In this class we have created a main() method which contains the various operations for creating an ArrayList and thus creating the clone of the ArrayList created.

In the following example we have created an ArrayList which contains some elements. And then, we created another empty ArrayList, which we have created a clone of ArrayList with elements.

For creating the clone of the ArrayList we use the clone() on the ArrayList. The following statement of the following code does that,

ArrayList<String> al2=(ArrayList<String>)al.clone();

In the above statement we have created a ArrayList al2 in which we have stored the clone of ArrayList al.

 
package	r4r.co.in;
import java.util.ArrayList;
import java.util.Iterator;


public class CollectionExample {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub

ArrayList<String> al=new ArrayList<String>(); // creating an ArrayList named al
al.add("r4r"); // adding string objects to the arraylist
al.add("development");
al.add("tutorials");
al.add("shashi");
al.add("s/w engineer");
System.out.println("The size of the arraylist is: "+al.size()); // determining the length of the arraylist

System.out.println("The contents of the list are :"+al); // showing the contents of the arraylist
ArrayList<String> al2=(ArrayList<String>)al.clone(); // creating the clone of the arraylist al
System.out.println("The elements in the cloned arraylist are :"+al2); // displaying the contents of the cloned arraylist
System.out.println("The length of this cloned arraylist is :"+al.size()); // displaying the size of the cloned array
al2.remove(3); // removing the element at the index 3 from the cloned array
System.out.println("Now the contents of the cloned array after removal of the element is :"+al2);
Iterator<String> itr=al2.iterator(); // Invoking the Iterator on the cloned arraylist al2
itr.next(); // passing over the first element
itr.remove(); // removing the first element
System.out.println("The contents the cloned array al2 are :"+al2 );
System.out.println("The contents of the origianl array al is :"+al);

}

}
The output of the above program is as follows,

The size of the arraylist is: 5
The contents of the list are :[r4r, development, tutorials, shashi, s/w engineer]
The elements in the cloned arraylist are :[r4r, development, tutorials, shashi, s/w engineer]
The length of this cloned arraylist is :5
Now the contents of the cloned  array after removal of the element is :[r4r, development, tutorials, s/w engineer]
The contents the cloned array al2 are :[development, tutorials, s/w engineer]
The contents of the origianl array al is :[r4r, development, tutorials, shashi, s/w engineer]

Previous Home Next