Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- import java.util.ArrayList;
- import java.util.Iterator;
- //Java clone ArrayList example
- public class JavaCloneArrayListExample {
- public static void main(String args[]){
- //create original ArrayList object
- ArrayList<Employee> aListOriginal = new ArrayList<Employee>();
- aListOriginal.add(new Employee("Mark", 10000));
- aListOriginal.add(new Employee("Steve", 20000));
- aListOriginal.add(new Employee("Alex", 30000));
- //to clone ArrayList use clone method
- ArrayList<Employee> aListCloned = (ArrayList<Employee>)aListOriginal.clone();
- /*
- * NOTE: clone method only shallow copies the ArrayList. That means that
- * only ArrayList object reference is cloned not the objects within the
- * original ArrayList.
- *
- * Cloned ArrayList still refers to the same original objects added to the
- * original ArrayList. That also means that if we change any of the object
- * contained within cloned ArrayList, object within original ArrayList also
- * changes!!
- */
- //Change the object in original ArrayList
- aListCloned.get(0).setName("Name Updated");
- System.out.println("Original ArrayList " + aListOriginal);
- System.out.println("Cloned ArrayList " + aListCloned);
- /*
- * As you can see from the output, clone method only copies the ArrayList
- * reference and not the objects contained in the ArrayList.
- *
- * To deep copy ArrayList we also need to clone the objects contained in
- * the ArrayList using iterator as given below.
- */
- ArrayList<Employee> aListNew = new ArrayList<Employee>();
- Iterator<Employee> itr = aListOriginal.iterator();
- while(itr.hasNext()){
- //add cloned Employee object to cloned ArrayList instead of original object.
- //In order for this to work, Class Employee must have clone method.
- aListNew.add((Employee)itr.next().clone());
- }
- System.out.println("ArrayList cloned - Deep copied");
- //now change again object of Original ArrayList
- aListOriginal.get(0).setName("Mark");
- System.out.println("Original ArrayList " + aListOriginal);
- System.out.println("New ArrayList " + aListNew);
- /*
- * As you can see from the output now, changing object of original
- * ArrayList does not affect the object in the cloned ArrayList.
- */
- }
- }
- class Employee{
- String name;
- int salary;
- public Employee(String name, int salary){
- this.name = name;
- this.salary = salary;
- }
- public String toString(){
- return "Name: " + this.name + ", salary: " + this.salary;
- }
- public String getName(){
- return this.name;
- }
- public void setName(String name){
- this.name = name;
- }
- public Object clone(){
- Employee temp = new Employee(this.name, this.salary);
- return temp;
- }
- }
- /*
- Output of above given Java ArrayList clone example would be
- Original ArrayList [Name: Name Updated, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
- Cloned ArrayList [Name: Name Updated, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
- ArrayList cloned - Deep copied
- Original ArrayList [Name: Mark, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
- New ArrayList [Name: Name Updated, salary: 10000, Name: Steve, salary: 20000, Name: Alex, salary: 30000]
- */

Post new comment