Skip to content

Deep / Shallow Cloning

January 4, 2012

public class PersonAge  {

private int age=0;

public PersonAge(int age) {
super();
this.age = age;
}

@Override
public String toString() {
return “CloningObj [age=” + age + “]”;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public void increment() {
age++;
}

}

 

import java.util.ArrayList;
import java.util.Iterator;

public class ShallowCloningTest {

public static void main(String[] args) {

ArrayList originalList = new ArrayList();

for (int i = 0; i < 10; i++) {
PersonAge age = new PersonAge(i);
originalList.add(age);

}

ArrayList clonedList = (ArrayList) originalList.clone();

PersonAge cObj = new PersonAge(250);
PersonAge cO = (PersonAge) clonedList.get(5);
cO.setAge(300);
clonedList.remove(5);
clonedList.add(5, cO);

System.out.println(“Original List”);
for (Iterator origListElement = originalList.iterator(); origListElement
.hasNext();)
System.out.println(origListElement.next());
System.out.println(“\n\nCloned List”);
for (Iterator clonedListElement = clonedList.iterator(); clonedListElement
.hasNext();)
System.out.println(clonedListElement.next());

}

}

 

Output

Original List
PersonAge [age=0]
PersonAge [age=1]
PersonAge [age=2]
PersonAge [age=3]
PersonAge [age=4]
PersonAge [age=300]
PersonAge [age=6]
PersonAge [age=7]
PersonAge [age=8]
PersonAge [age=9]

Cloned List
PersonAge [age=0]
PersonAge [age=1]
PersonAge [age=2]
PersonAge [age=3]
PersonAge [age=4]
PersonAge [age=300]
PersonAge [age=6]
PersonAge [age=7]
PersonAge [age=8]
PersonAge [age=9]

 

 

import java.util.ArrayList;
import java.util.Iterator;

public class DeepCloningTest {

public static void main(String[] args) {

ArrayList originalCollection = new ArrayList();

for (int i = 0; i < 10; i++) {
// CloningObj cObj = new CloningObj(i);
originalCollection.add(i);

}

ArrayList clonedCollection = (ArrayList) originalCollection.clone();

int retVal = Integer.valueOf((Integer) clonedCollection.get(5))
.intValue();

clonedCollection.remove(5);
clonedCollection.add(5, 400);

System.out.println(“Original List”);

for (Iterator origCollectionElement = originalCollection.iterator(); origCollectionElement
.hasNext();)
System.out.println(origCollectionElement.next());

System.out.println(“\n\nCloned List”);
for (Iterator clonedCollectionElement = clonedCollection.iterator(); clonedCollectionElement
.hasNext();)
System.out.println(clonedCollectionElement.next());

}

}

Output

Original List
0
1
2
3
4
5
6
7
8
9

Cloned List
0
1
2
3
4
400
6
7
8
9

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: