创建深度复制方法,Java [英] Creating a deep copy method, Java

查看:61
本文介绍了创建深度复制方法,Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }

现在,当我创建一个新的名为jadeed的GhostList时,然后在此下创建一个新的鬼数据数组,它是否知道数据属于翡翠GhostList?我不知道两者如何关联,即使它们应该关联。

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.

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

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

推荐答案

您创建了一个新的 GhostList 和一个新的 Ghost 数组。

填写 Ghost 数组并返回 GhostList ,但返回的 GhostList Ghost 数组无关。 br>
您应该将所有新的幽灵添加到 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

这篇关于创建深度复制方法,Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆