2013-07-02 23 views
-1

嗨,我必須深入克隆包含數組列表的對象。Java深度克隆包含ArrayList的對象

public class Objct1 implements Cloneable { 
    ArrayList<Objt> o = new ArrayList(); 

    public Object1() { 
    } 

    @Override 
    protected Object clone() { 
     try { 
      return super.clone(); 
     } catch (CloneNotSupportedException e) { 
      // This should never happen 
      throw new InternalError(e.toString()); 
     } 
    } 
} 

public class Objt implements Cloneable { 
    private int ca; 

    /** 
    * @return the x 
    */ 
    public int getCa() { 
     return x; 
    } 

    /** 
    * @param x the x to set 
    */ 
    public void setCa(int ca) { 
     this.ca = ca; 
    } 

    @Override 
    protected Object clone() { 
     try { 
      return super.clone(); 
     } catch (CloneNotSupportedException e) { 
      // This should never happen 
      throw new InternalError(e.toString()); 
     } 
    } 
} 

我試圖克隆Objct1在這個例子中

Objct1 a = new Objct1(); 

arryOfObjct1[count] = (Objct1) a.clone(); 
+0

您不是深度克隆,對於深度克隆,您必須遍歷所有數組列表,並且Objt應該在克隆方法中執行深度克隆,可能是您可以序列化它們並強制您獲得深層副本 – nachokk

回答