-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 を複製し、その内容も複製する方法は?

ArrayList に含まれるオブジェクトのクローンを作成していないため、@zerocool の回答は有効ではありません。

于 2013-07-02T14:30:23.923 に答える