Java ArrayList clone

This section demonstrates how to use the Java Arraylist clone Method.

 
 

Share on Google+Share on Google+

Java ArrayList clone

Ads

This section demonstrates how to use the Java Arraylist clone Method.

  • It returns the copy of the given ArrayList object as the Object class.
  • So it must be casted to the ArrayList for further use.


Java Arraylist Clone Example
import java.util.*;

public class List1 {

public static void main(String[] args) {
String  ar[]={"india","pakistan","United Kingdom","Japan"};
ArrayList list=new ArrayList();
list.add(ar[0]);
list.add(ar[1]);
list.add(ar[2]);
list.add(ar[3]);
ArrayList list1=(ArrayList)list.clone();

System.out.println(list+"   ");
System.out.println(list1+"   ");
}
}
Output
[india, pakistan, United Kingdom, Japan] 
[india, pakistan, United Kingdom, Japan]

Advertisements

Posted on: October 23, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles