无法深入克隆链接列表中的对象

时间:2014-11-14 00:23:24

标签: java

大家好,我在克隆方面遇到了麻烦。我试图克隆列表中的对象。我成功地克隆了我的列表和列表中的节点。但是我无法克隆存储在节点中的对象。下面是我的示例代码:我成功克隆了类Node和类B.但是没有成功克隆类B中的类A,因为我试图用克隆Linkedlist修改类A并通过打印出原始文件来验证它的深层副本内容和克隆的内容,并能够修改克隆的内容而无需修改原始内容。基本上我的程序从文件中读取并存储一个国家,即1960年到2012年的统计单元数据。我能够成功读取文件,但现在我正在尝试对列表进行深层复制。我无法为A类制作深层副本,因为我试图修改A但它影响了原始列表。起初它给了我数组越界但现在当我修改一年及其数据时,它还修改了原始列表而不仅仅是克隆列表。我究竟做错了什么。我知道它的代码很多,但是我把它简单化了,并且如果有人可以提供帮助,我也不想遗漏任何重要的细节。我想了解深刻的副本。

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;

}

我的成功克隆在下面的列表中

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;
 }

我能够成功克隆列表和类Node

在我的主要方法中,我能够将一组随机的国家和数据添加到列表中,并且能够克隆列表并在B中修改名称而没有任何问题。但是我无法深度克隆类A,因为clonedList试图修改A类但没有成功。我在类中检查了我的clone方法,而不是克隆类A.我在调试它时遇到了问题。请给我一些帮助。

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;
 }

1 个答案:

答案 0 :(得分:1)

当你有一个类时,可以说你有A类,B类和Class Node 如果你从A类创建一个包含B类对象数组的对象 并且此Array的每个元素都包含一些节点 你应该克隆A的副本 然后在类A的同一个克隆函数中,你克隆了类型B的数组,这也非常重要,你应该通过它自己克隆数组中的每个元素 这一切都在A类克隆方法

在B类方法中你应该克隆B的副本,这里的副本是我们的B类数组,或者它是数组中B类型的对象元素(B [0],B [1] ....等等) 并且还应该克隆任何条目中的每个节点。

这里有一个例子:

//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();

就是这样:)