-1

您好我必须深度克隆一个包含数组列表的对象。

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

1 回答 1

1

重复: 如何克隆 ArrayList 并克隆其内容?

@zerocool 答案无效,因为未克隆 ArrayList 中包含的对象。

于 2013-07-02T14:30:23.923 回答