Java数组克隆奇怪的行为

发布于 2024-12-15 17:13:07 字数 279 浏览 2 评论 0原文

我正在尝试克隆我创建的名为 CSP 的自定义类的实例。我有一个名为 csp 的实例,我想创建一个名为 cspclone 的 csp 克隆。这是我用来执行此操作的方法:

CSP cspclone = new CSP((csp.x).clone(), (csp.d).clone(), (csp.c).clone()) ;

由于某种原因,虽然当我将 cspclone 传递给修改它的方法时,csp 也会被修改,就好像我忘记了 .clone() 函数一样,但我没有!为什么会出现这种情况?!

I'm trying to clone an instance of a custom class I made called CSP. I have one instance called csp and I want to make a clone of csp called cspclone. Here is what I'm using to do that:

CSP cspclone = new CSP((csp.x).clone(), (csp.d).clone(), (csp.c).clone());

For some reason though when I pass cspclone to a method that modifies it csp gets modified also as if I forgot the .clone() functions but I didn't! Why is this happening?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

︶ ̄淡然 2024-12-22 17:13:07

重写CSP中的clone方法:

public class CSP {
    private String aField;
    private int[] array;
    private int[][] twoDArr;
    private List<ALContent> list; //here ALContent also needs to override clone properly

    @Override
    public Object clone() {
        CSP clone = new CSP();
        clone.aField = this.aField;
        clone.array = new int[this.array.length];
        System.arraycopy(this.array, 0, clone.array, 0, this.array.length);

        clone.list = new ArrayList<ALContent>();
        for(ALContent content : this.list) {
            clone.list.add(content.clone()); //make sure you add the clone of the content
        }

        clone.twoDArr = new int[this.twoDArr.length][];
        for(int i=0; i<this.twoDArr.length; i++) {
            clone.twoDArr[i] = new int[this.twoDArr[i].length];
            System.arraycopy(this.twoDArr[i], 0, clone.twoDArr[i], 0, this.twoDArr[i].length);
        }

        return clone;
    }
}

然后你可以这样做:

CSP csp = new CSP();
CSP cspClone = (CSP) csp.clone();

Override the clone method in CSP:

public class CSP {
    private String aField;
    private int[] array;
    private int[][] twoDArr;
    private List<ALContent> list; //here ALContent also needs to override clone properly

    @Override
    public Object clone() {
        CSP clone = new CSP();
        clone.aField = this.aField;
        clone.array = new int[this.array.length];
        System.arraycopy(this.array, 0, clone.array, 0, this.array.length);

        clone.list = new ArrayList<ALContent>();
        for(ALContent content : this.list) {
            clone.list.add(content.clone()); //make sure you add the clone of the content
        }

        clone.twoDArr = new int[this.twoDArr.length][];
        for(int i=0; i<this.twoDArr.length; i++) {
            clone.twoDArr[i] = new int[this.twoDArr[i].length];
            System.arraycopy(this.twoDArr[i], 0, clone.twoDArr[i], 0, this.twoDArr[i].length);
        }

        return clone;
    }
}

Then you can do:

CSP csp = new CSP();
CSP cspClone = (CSP) csp.clone();
赴月观长安 2024-12-22 17:13:07

如果您的数组类型属性使用 System.arraycopy

If your properties of array type use System.arraycopy

太阳哥哥 2024-12-22 17:13:07

根据 http://download .oracle.com/javase/1.3/docs/api/java/lang/Object.html#clone%28%29

此方法创建该对象的类的一个新实例,并使用该对象相应字段的内容初始化其所有字段,就像通过赋值一样;字段的内容本身并不是克隆的。因此,该方法执行该对象的“浅复制”,而不是“深复制”操作。

您可能必须覆盖对象中的clone方法和clone()一个引用类型属性(即执行深复制操作)。

According to http://download.oracle.com/javase/1.3/docs/api/java/lang/Object.html#clone%28%29

this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

You might have to override the clone method and clone() a reference type attribute within the object (i.e., perform deep copy operation).

天生の放荡 2024-12-22 17:13:07

要解决您的问题,您需要深度克隆。默认的克隆方法执行浅复制。请参阅 Object.clone()

这里有一些方法。都有优点和缺点。

  • 重写:重写clone()方法。
  • 序列化:将对象写入流并从流中读回。
  • 反射:这是反射教程
  • 使用深度克隆库,例如 Java 深度克隆库

以下是有关克隆的其他几个 stackoverflow 讨论。

To solve your problem you need deep cloning. The default clone method does a shallow copy. See Object.clone().

Here are some approaches. All have advantages and drawbacks.

Here are several other stackoverflow discussions of cloning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文