创建深拷贝方法,Java
我想做一个深复制方法。前几天我在这里寻求了有关此问题的帮助,但那是针对复制构造函数的。现在我需要一个常规方法。我已经创建了代码(不起作用),但我只是不完全理解它。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您创建了一个新的
GhostList
和一个新的Ghost
数组。您填充
Ghost
数组并返回GhostList
,但返回的GhostList
与Ghost
数组无关.您应该将所有新幽灵添加到
GhostList
You created a new
GhostList
and a newGhost
array.You fill in the
Ghost
array and return theGhostList
but the returnedGhostList
has nothing to do with theGhost
array.You should add all the new ghosts to the
GhostList
首先,您提到了复制构造函数。如果您已经可以正常工作,那么您在
deepCopy
方法中需要做的就是:让我们暂时忘记这一点,然后回到您发布的代码。您正在创建一个名为
data
的数组,但您从未在任何地方使用过它。您不应该将此数组分配给jadeed
吗?类似于:最后,与其调用方法
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: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 tojadeed
? Something like:And finally, instead of calling the method
deepCopy
, it would be better to call itclone
and implement theCloneable
interface. Doing this allows everyone to know how to get a copy of your object using a standard interface.您的
GhostList
类将以对Ghost
数组的引用作为其数据成员。您还没有向我们展示类定义,因此假设该成员名为foo
。现在您需要做的就是使新创建的jadeed
对象的foo
引用引用您创建的Ghost
数组,并且人口。您可以这样做:在返回
jadeed
之前。Your
GhostList
class will have as its data member a reference to the array ofGhost
. You've not shown us the class definition, so lets say that member is namedfoo
. Now all you need to do is make thefoo
reference of the newly createdjadeed
object refer to the array ofGhost
which you've created and populated. You can do it as:before you return
jadeed
.如果
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 isSerializable
, you can serialize theGhostList
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