Having trouble deep cloning an object in a linked list
Hello guys I'm having trouble with cloning. I am trying to clone an object within a list. I succeeded in cloning my list and the node in the list. But i am having trouble cloning an object that is stored in the node. Below is my sample code: I succeeded in cloning the class Node and class B. But not succeeding in cloning class A from class B as i am trying to modify class A with the clone Linkedlist and verify its a deep copy by printing out the original contents and the cloned contents and being able to modify the cloned contents without modifying the original contents. Basically my program read from a file and store a country, the statistical cell data from year 1960 to 2012. I am able to read from a file successfully but now I'm trying to make a deep copy of the list. I am having trouble making a deep copy for class A as i tried to modify A but it was affecting the original list. At first it gave me array out of bounds but now when i modify the a year and its data it also modifies the original list and not only the cloned list. What am i doing wrong. I know its a lot of code but i made it simple as short and didn't want to leave out any important details please if someone could help. I am trying to understand deep copy.
Class A implements Cloneable{
private int year;
private double data;
A(int double, double data)
{
setInt(year);
setDouble(data);
}
public void setInt(int year)
{
this.year = year;
}
public void setDouble(double data)
{
this.data = data;
}
public int getYear()
{
return year;
}
public double getData()
{
return data;
}
public Object clone()
{
A clonedA = new A(this.getYear(), this.getData());
}}
class B implements Cloneable{
private A[] a;
private String name;
private int arraylength;
private int index;
public B(String name, int length)
{
this.name = name;
this.arraylength = length;
a = new A[array.length];
index = 0;
}
public void addToA(int year, double data)
{
a[index] = new A(year, data);
index++;
}
public String getName(){
return name; }
public int getLength(){
return array length;}
public void setName(String name)
{
this.name= name
}
public Object clone()
{
B clonedB = new B(this.getName(), this.getLength());
for(A clonedArray: a)
{
clonedB.addToA(clonedArray.getYear(), clonedArray.getData());
}
return clonedB;
}
my successful cloned in the List Below
class Node implements Cloneable{
B objectB;
Node next;
public Node(B objectB)
{
this.objectB = objectB;
this.next = null;
}
public void setNode(B node)
{
this.next = node;
}
public Node getNext()
{
this.next;
}
public B getObjectB()
{
return objectB;
}
public Object clone()
{
Node newNode = new Node(this.getObjectB);
newNode.objectB = (B)this.getB().clone
return newNode;
} }
class LinkedList implements Cloneable{
private Node head;
public LinkedList()
{
head = null
}
public boolean isEmpty()
{
return (head==null);
}
public void addEndOfList(B b)
{
Node newNode = new Node(b);
if(this.isEmpty())
{
newNode.set(head);
head = newNode;
}
else {
Node current = head;
while(current.getNext!=null)
{
current = current.getNext();
}
current.set(newNode);
}
public void setName(String name)
{
if(this.isEmpty()
{
System.out.println("Cannot modify an empty list" );
}
else{
first.getObjectB().setName(name);
}
}
public void modifyA(int year, double stat)
{
if(this.isEmpty()
{
System.out.println("Cannot modify an empty list" );
}
else{
first.getObjectB().addTOA(year, stat);
}
}
public Object clone()
{
LinkedList newList = new LinkedList();
Node current = first;
while(current!=null)
{
Node clonedCurrent = (Node)current.clone();
newList.add(clonedCurrent.getObjectB())
current = current.getNext();
}
returned newList;
}
I was able to successful to clone the list and class Node
In my main method i was able to add a random set of countries and data into the list and was able to clone the list and modify the name in B with no problem. But i am having trouble deep cloning class A as the clonedList is trying to modify class A with no success. I have checked my clone method in class and something its not cloning class A. i am having trouble debugging it. Please cld some help me out.
private LinkedList createCloneList(B []b, int selectSize)
{
Random rand = new Random()
LinkedList selectB = new LinkedList();
for(int i = 0; i<selectSize;i++)
{
int index = random.nextInt(b.length);
selectB.addEndOfList(b[index]);
}
return selectB;
}
private LinkedList testCloneable(LinkedList listOfB)
{
LinkedList clonedList = (LinkedList)listofB.clone();
clonedList.setName("NewName");
clonedList.modifyA(0, 12.56);//0 being the first index position of the stat 12.56
return clonedList;
}
when you have a class lets say you have class A, class B , and Class Node if you make an object from class A that contains an array of objects from class B and each element of this Array contains some nodes you should clone a copy of A then in the same clone function of class A, you clone the array of type B, also and this is very important, you should clone every element in the array by it self all this was in class A clone method
in B class method you should clone copy of B, here the copy is our array of type B, or it is the object element of type B in the array (B[0],B[1]....etc) and also every each node in any entry should be cloned too.
here an example:
//class A
public class A implements Cloneable{
B [] array;
.
.
.
public Object clone() throws CloneNotSupportedException {
A copyA = null;
try {
copyA = (A) super.clone();
}
catch (CloneNotSupportedException e) {
// this should never happen
throw new InternalError(e.toString());
}
copyA.array = array.clone();
for (int i=0;i<array.length;i++)
copyA.array[i] = (B) array[i].clone();
return copyA;
}
}
//class B
public class B implements Cloneable{
Node topnode;
Node bottom;
.
.
.
public Object clone() throws CloneNotSupportedException {
B copyB = null;
try {
copyB= (B) super.clone();
}
catch (CloneNotSupportedException e) {
// this should never happen
throw new InternalError(e.toString());
}
return copyB;
}
}
//class Node
public class Node implements Cloneable{
.
.
.
.
public Object clone() throws CloneNotSupportedException {
Node copyNode = null;
try {
copyNode = (Node) super.clone();
}
catch (CloneNotSupportedException e) {
// this should never happen
throw new InternalError(e.toString());
}
return copyNode;
}
}
/*in your main class
if you already have you original object of class A lets say original
and you want to clone it to copy you just do */
A copy =(A) original.clone();
and that is it :)
链接地址: http://www.djcxy.com/p/40790.html下一篇: 无法深入克隆链接列表中的对象