简体   繁体   English

具有原始类型的Java克隆类

[英]Java Clone Class With Primitive Types

I am trying to create a deep copy of an Object (called State ) to do modifications to one of the instances within it, then follow up with code that modifies the old State based on the new State . 我正在尝试创建Object (称为State )的深层副本,以对其中的一个实例进行修改,然后继续执行基于新State修改旧State的代码。

Here is State : 这是State

public class State implements Cloneable {

    // 0: empty
    // 1: white
    // 2: black
    private int[][] board;

    private int player;

    public State(int[][] board, int player) {
        this.board = board;
        this.player = player;
    }

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

    public int getPlayer() {
        return player;
    }

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

    public void setPlayer(int player) {
        this.player = player;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}

Then, here is how I try and access it: 然后,这是我尝试访问它的方法:

State temp = (State) s.clone();
Action act = determineAction(temp);
doSomething(s, act);

s is a State passed into the method. s是一个State传递到方法。 After determineAction is called, for some reason the board in s is being modified along with temp even though it is not passed... How come this is? 之后determineAction被调用时,由于某些原因, boards被改良沿着temp即使它不过去了......为什么这是? Shouldn't the call to .clone() clone instances of all primitive types such that they can be uniquely modified? .clone()的调用是否应该克隆所有原始类型的实例,以便可以对其进行唯一修改?

That is what this post suggests: http://howtodoinjava.com/core-java/cloning/a-guide-to-object-cloning-in-java/ 这就是这篇文章的建议: http : //howtodoinjava.com/core-java/cloning/a-guide-to-object-cloning-in-java/

I'm struggling to see why this wouldn't be deep copied, and why my modifications to temp would also modify s . 我正在努力查看为什么不会对此进行深度复制,以及为什么我对temp修改也会修改s

Any tips would be greatly appreciated -- thanks! 任何提示将不胜感激-谢谢!

Edit - For anyone curious, here's what fixed it: 编辑-对于任何好奇的人,这是解决问题的方法:

@Override
protected Object clone() throws CloneNotSupportedException {

    State cloned = (State) super.clone();

    int[][] clonedBoard = new int[8][8];

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            clonedBoard[i][j] = board[i][j];
        }
    }

    cloned.setBoard(clonedBoard);

    return cloned;
}

super.clone() does not do a deep-copy, and an int[][] is not a primitive type. super.clone()不会进行深度复制,并且int[][]不是原始类型。

It works for the int player because that is a primitive type and a simple copy (as done by Object#clone ) is enough. 它适用于int player因为它是原始类型,并且简单的副本(由Object#clone )就足够了。

You need to (deep-) copy your int[][] yourself. 您需要自己(深度)复制int[][]

Deep cloning for Primitive data types. 对原始数据类型的深度克隆。 You can achieve deep cloning using serialization also 您还可以使用序列化实现深度克隆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM