JAVA - ArrayList Cloning Technique
CyberCodes :: COMMUNITY :: Computer and Internet Zone :: Programming :: JAVA
Page 1 of 1
JAVA - ArrayList Cloning Technique
What is cloning?
Cloning is the ability to copy one object to another object.
What is the purpose of cloning?
Once object already copied to another object, the new object doesn't affect the attributes or values of the original object.
Can we clone ArrayList?
Yes. There are two types of cloning we can achieve in ArrayList. Shallow Clone and Deep Clone.
Shallow clone is the ability to copy its container while Deep clone is the ability to clone to its elements which is composed of another objects.
Shallow Clone:
ArrayList aListOriginal = new ArrayList();
aListOriginal.add("A");
aListOriginal.add("B");
aListOriginal.add("C");
ArrayList aListClone = new ArrayList();
aListClone = aListOriginal.clone(); //Replica of aListOriginal.
aListClone.set(1, "D"); // Now, first element of the cloned ArrayList will become "D", but it won't affect the first element of the aListOriginal which is "A".
Deep Clone:
ArrayList aListParent= new ArrayList();
ArrayList aListChild = new ArrayList();
aListChild.add("A");
aListChild.add("B");
aListChild.add("C");
aListParent.add(aListChild.clone()); // Shallow Clone.
ArrayList aListClone = new ArrayList();
aListClone = (ArrayList)((ArrayList)aListParent.get(1)).clone();
aListClone.set(1, "D");
Based on the above example, aListClone element is came from aListChild. aListParent is the container of aListChild.
Now, the first element of aListClone already changed, but, the first element of aListChild remain unchanged!
Cloning is the ability to copy one object to another object.
What is the purpose of cloning?
Once object already copied to another object, the new object doesn't affect the attributes or values of the original object.
Can we clone ArrayList?
Yes. There are two types of cloning we can achieve in ArrayList. Shallow Clone and Deep Clone.
Shallow clone is the ability to copy its container while Deep clone is the ability to clone to its elements which is composed of another objects.
Shallow Clone:
ArrayList aListOriginal = new ArrayList();
aListOriginal.add("A");
aListOriginal.add("B");
aListOriginal.add("C");
ArrayList aListClone = new ArrayList();
aListClone = aListOriginal.clone(); //Replica of aListOriginal.
aListClone.set(1, "D"); // Now, first element of the cloned ArrayList will become "D", but it won't affect the first element of the aListOriginal which is "A".
Deep Clone:
ArrayList aListParent= new ArrayList();
ArrayList aListChild = new ArrayList();
aListChild.add("A");
aListChild.add("B");
aListChild.add("C");
aListParent.add(aListChild.clone()); // Shallow Clone.
ArrayList aListClone = new ArrayList();
aListClone = (ArrayList)((ArrayList)aListParent.get(1)).clone();
aListClone.set(1, "D");
Based on the above example, aListClone element is came from aListChild. aListParent is the container of aListChild.
Now, the first element of aListClone already changed, but, the first element of aListChild remain unchanged!
princeterror- Universal Moderator
- Posts : 272
Credits : 11465
Fame : 81
Join date : 2012-06-07
Age : 33
Similar topics
» Java Swing Calculator
» Java ternary Operator
» [TUT] Create a Frame in Java with 2 Labels
» Java = Get public IP & Send report ot email
» Java ternary Operator
» [TUT] Create a Frame in Java with 2 Labels
» Java = Get public IP & Send report ot email
CyberCodes :: COMMUNITY :: Computer and Internet Zone :: Programming :: JAVA
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum