创建深拷贝方法,Java

发布于 2024-09-27 23:15:00 字数 765 浏览 5 评论 0原文

我想做一个深复制方法。前几天我在这里寻求了有关此问题的帮助,但那是针对复制构造函数的。现在我需要一个常规方法。我已经创建了代码(不起作用),但我只是不完全理解它。

public GhostList deepCopy(){
        int length=this.getLength();
        GhostList jadeed=new GhostList();
        Ghost[] data = new Ghost[length];
        for (int i=0;i<this.getLength();i++){
            data[i] = new Ghost();
            data[i].setX(this.ghosts[i].getX());
            data[i].setY(this.ghosts[i].getY());
            data[i].setColor(this.ghosts[i].getColor());
            data[i].setDirection(this.ghosts[i].getDirection());
        }

        return jadeed;
    }

现在,当我创建一个名为jadeed的新GhostList,然后在其下创建一个新的幽灵数据数组时,它知道数据属于jadeed GhostList吗?我不明白两者如何联系起来,尽管它们应该是联系在一起的。

另外,我没有得到与副本和 this.object 匹配的长度。我的问题是什么?

I want to make a deep copy method. I seeked help here the other day with this issue, but that was for a copy constructor. Now I need a regular method. I have the code created (nonworking), but I'm just not understanding it completely.

public GhostList deepCopy(){
        int length=this.getLength();
        GhostList jadeed=new GhostList();
        Ghost[] data = new Ghost[length];
        for (int i=0;i<this.getLength();i++){
            data[i] = new Ghost();
            data[i].setX(this.ghosts[i].getX());
            data[i].setY(this.ghosts[i].getY());
            data[i].setColor(this.ghosts[i].getColor());
            data[i].setDirection(this.ghosts[i].getDirection());
        }

        return jadeed;
    }

Now when I create a new GhostList named jadeed, and then under that I create a new data array of ghosts, does it know that data belongs to the jadeed GhostList? I dont see how the two can be associated, even though they should be.

Also, I'm not getting the lengths to match up for the copy and this.object. What is my problem?

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

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

发布评论

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

评论(4

俯瞰星空 2024-10-04 23:15:00

您创建了一个新的 GhostList 和一个新的 Ghost 数组。
您填充 Ghost 数组并返回 GhostList,但返回的 GhostListGhost 数组无关.
您应该将所有新幽灵添加到 GhostList

You created a new GhostList and a new Ghost array.
You fill in the Ghost array and return the GhostList but the returned GhostList has nothing to do with the Ghost array.
You should add all the new ghosts to the GhostList

野の 2024-10-04 23:15:00

首先,您提到了复制构造函数。如果您已经可以正常工作,那么您在 deepCopy 方法中需要做的就是:

return new GhostList(this);

让我们暂时忘记这一点,然后回到您发布的代码。您正在创建一个名为 data 的数组,但您从未在任何地方使用过它。您不应该将此数组分配给jadeed吗?类似于:

jadeed.ghosts = data;

最后,与其调用方法 deepCopy,不如调用它 克隆并实现Cloneable 接口。这样做可以让每个人都知道如何使用标准接口获取对象的副本。

First, you mentioned a copy constructor. If you already have that working, then all you need to do in your deepCopy method is:

return new GhostList(this);

Let's forget that for now and get back to the code you posted. You are creating an array named data but you never used it anywhere. Aren't you supposed to assign this array to jadeed? Something like:

jadeed.ghosts = data;

And finally, instead of calling the method deepCopy, it would be better to call it clone and implement the Cloneable interface. Doing this allows everyone to know how to get a copy of your object using a standard interface.

2024-10-04 23:15:00

您的 GhostList 类将以对 Ghost 数组的引用作为其数据成员。您还没有向我们展示类定义,因此假设该成员名为 foo。现在您需要做的就是使新创建的jadeed对象的foo引用引用您创建的Ghost数组,并且人口。您可以这样做:

jadeed.foo = data;

在返回jadeed之前。

Your GhostList class will have as its data member a reference to the array of Ghost. You've not shown us the class definition, so lets say that member is named foo. Now all you need to do is make the foo reference of the newly created jadeed object refer to the array of Ghost which you've created and populated. You can do it as:

jadeed.foo = data;

before you return jadeed.

清风夜微凉 2024-10-04 23:15:00

如果 GhostList 及其组成的所有内容都是可序列化,则您可以将 GhostList 实例序列化为字节数组并重新读取它。这是几行代码,除非您使用“Jakarta Commons Lang” - 一行代码:

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/ SerializationUtils.html#clone%28java.io.Serialized%29

If GhostList and everything it's composed of is Serializable, you can serialize the GhostList instance into a byte array and re-read it. It's a few lines of code, unless you use `Jakarta Commons Lang - one line of code:

http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/SerializationUtils.html#clone%28java.io.Serializable%29

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