复制对象

发布于 2024-12-18 10:17:11 字数 1016 浏览 3 评论 0原文

我试图复制一个对象但没有成功。我尝试过

  1. 序列化
  2. 克隆

这两种方法都不适合我。

当我使用序列化时(我使用此处指定的技术 更快的深层副本Java 对象)我得到了NullPointerException。通过克隆,我获得了原始对象引用。

场景是:

我有一个带有数据 char[][] board 的抽象类 A 和一个扩展类 B。我想复制数据 board 为此我在 B 中实现了两个方法 - getboard()setboard() > 并实现了一个克隆方法,

B b1 = new B;
B.initialize();
B b2 new B;
B2 = B1.clone(B2)

但是这不起作用。任何帮助将不胜感激。

谢谢:-)

       public B clone()  {

            B N = new B();
            try {
                    N = (B)super.clone();
            } catch (CloneNotSupportedException e) {
                  e.printStackTrace();
            }
            N.setBoard(this.getBoard());
            return N;
    }

关于序列化,反序列化后,当我尝试绘制板时,它给了我NullPointerException。我的结论是,反序列化无法正常工作。

I am trying to duplicate an object with no success. I tried

  1. Serialization
  2. Cloning

both methods don't work for me.

When I used serialization (I am using the technique specified here Faster Deep Copies of Java Objects) I got NullPointerException. With cloning I got original object reference.

Scenario is:

I have one abstract class A with data char[][] board and an extended class B. I want to duplicate the data board for this I implemented two methods in B - getboard() and setboard() and implemented a clone method such that

B b1 = new B;
B.initialize();
B b2 new B;
B2 = B1.clone(B2)

But this is not working. Any help would be appreciated.

Thanks :-)

       public B clone()  {

            B N = new B();
            try {
                    N = (B)super.clone();
            } catch (CloneNotSupportedException e) {
                  e.printStackTrace();
            }
            N.setBoard(this.getBoard());
            return N;
    }

Regarding the serialization, after deserializing when i try to draw the board it is giving me NullPointerException. I conclude that, deserialization didn't work properly.

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

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

发布评论

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

评论(2

最舍不得你 2024-12-25 10:17:11

覆盖 B 中的 clone 方法:

public class B {
    private int f1;
    private String f2;

    @Override
    public B clone() {
        B clone = new B();
        clone.f1 = this.f1;
        clone.f2 = this.f2;
        return clone;
    }
}

并使用它来创建重复项:

B b1 = new B();
//...
B b2 = b1.clone();

更新:

public B clone()  {
        B N = new B();
        try {
                N = (B)super.clone();
        } catch (CloneNotSupportedException e) {
              e.printStackTrace();
        }
        N.setBoard(this.getBoard());
        return N;
}

尝试以下操作(假设 B > 实现Cloneable):

@Override
public B clone() throws CloneNotSupportedException {
    return (B) super.clone();
}

更新:

board的自定义实现:

class B implements Cloneable {
    private char[][] board;   

    @Override
    public B clone() throws CloneNotSupportedException {
        B clone = (B) super.clone();
        clone.board = new char[this.board.length][];
        for(int i=0; i<this.board.length; i++) {
            clone.board[i] = new char[this.board[i].length];
            System.arraycopy(this.board[i], 0, clone.board[i], 0, this.board[i].length);
        }
        return clone;        
    }

    public char[][] getBoard() {
        return board;
    }

    public void setBoard(char[][] board) {
        this.board = board;
    }

    @Override
    public String toString() {
        return Arrays.deepToString(this.board);
    }
}

Override the clone method in B:

public class B {
    private int f1;
    private String f2;

    @Override
    public B clone() {
        B clone = new B();
        clone.f1 = this.f1;
        clone.f2 = this.f2;
        return clone;
    }
}

And use it to create duplicate:

B b1 = new B();
//...
B b2 = b1.clone();

Update:

public B clone()  {
        B N = new B();
        try {
                N = (B)super.clone();
        } catch (CloneNotSupportedException e) {
              e.printStackTrace();
        }
        N.setBoard(this.getBoard());
        return N;
}

Try the following (assuming that B implements Cloneable):

@Override
public B clone() throws CloneNotSupportedException {
    return (B) super.clone();
}

Update:

Custom implementation for board:

class B implements Cloneable {
    private char[][] board;   

    @Override
    public B clone() throws CloneNotSupportedException {
        B clone = (B) super.clone();
        clone.board = new char[this.board.length][];
        for(int i=0; i<this.board.length; i++) {
            clone.board[i] = new char[this.board[i].length];
            System.arraycopy(this.board[i], 0, clone.board[i], 0, this.board[i].length);
        }
        return clone;        
    }

    public char[][] getBoard() {
        return board;
    }

    public void setBoard(char[][] board) {
        this.board = board;
    }

    @Override
    public String toString() {
        return Arrays.deepToString(this.board);
    }
}
彩扇题诗 2024-12-25 10:17:11

随着时间的推移,我发现使用第三方库进行对象克隆可以成为救星。我推荐的项目之一是“Cloning”,可在 http://code.google.com 上找到/p/cloning/

这是一个示例:

Cloner cloner=new Cloner();

MyClass clone=cloner.deepClone(o);
// clone is a deep-clone of o

Over the time I have found out that using a third party library to do object cloning can be a life saver. One of the projects I'd recommend is "Cloning", available at http://code.google.com/p/cloning/

Here's an example:

Cloner cloner=new Cloner();

MyClass clone=cloner.deepClone(o);
// clone is a deep-clone of o
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文