简体   繁体   English

对对象的任何更改,也会更改当前对象

[英]Any changes made to an object, changes also the current object

Sorry, the title is not very understandable, but my English is not helping. 抱歉,标题不是很容易理解,但是我的英语没有帮助。 I am a new programmer in java, and despite having read how parameters work, I do not really understand what is happening. 我是Java的新程序员,尽管阅读了参数的工作原理,但我并不真正了解发生了什么。

sudokuBoard alter = new sudokuBoard();
this.createRandomSudokuBoard();
alter.setBoardFromArray(this.getBoard().clone());

(...) 

for(int i = 0; i < 81; i++) {
    alter.clearCell(positionListonX[i], positionListonY[i]); <<<<<<<<<<<<< Here
    if(alter.numberOfSolutions(2) < 2) {
        this.clearCell(positionListonX[i], positionListonY[i]);
        alter.setBoardFromArray(this.getBoard().clone());
    } else {
        alter.setBoardFromArray(this.getBoard().clone());
    }
}

What happens is that in the indicated line, calling the method clearCell of the object alter is also modifying the current object (this). 发生的是,在指示的行中,调用对象alter的方法clearCell也在修改当前对象(此)。 In a last desperate attempt, I tried to resolve it with the clone() method (as you can see), but it did not work. 在最后一次绝望的尝试中,我尝试使用clone()方法解决它(如您所见),但是它没有用。

What's going on? 这是怎么回事? What am I missing? 我想念什么? thank you very much. 非常感谢你。

If you haven't implemented clone() in SudokuBoard , then you're probably getting the default clone() defined on Object , which doesn't perform a deep copy of the object. 如果尚未在SudokuBoard实现clone() ,则可能会得到在Object定义的默认clone() ,它不会执行对象的深层复制。 See Deep Copy for an explanation. 有关说明,请参见Deep Copy If you actually want a completely separate instance of your board in alter , you will need to do something like this: 如果您实际上需要在alter完全分离板的实例,则需要执行以下操作:

class SudokuBoard
{
    public void setBoard( SudokuBoard other )
    {
        for( int i = 0; i < 81; i++ )
        {
            this.positionListonX[i] = other.positionListonX[i];
            this.positionListonY[i] = other.positionListonY[i];
        }
        // Copy any other properties
    }
}

Note that if the values in your positionListonX and positionListonY arrays are not primitive types, you'll also need deep copies of those. 请注意,如果positionListonXpositionListonY数组中的值不是原始类型,则还需要它们的深层副本。 This is effectively a copy constructor, but I didn't give it that signature ( public SudokuBoard( SudokuBoard other) ) because I don't know the rest of the internals of SudokuBoard. 这实际上是一个复制构造函数,但是我没有给它签名( public SudokuBoard( SudokuBoard other) ),因为我不知道SudokuBoard的其余内部组件。

It would help to see more of the method signatures defined in your SudokuBoard class, so we know what methods are available and can understand what they do. 这将有助于查看SudokuBoard类中定义的更多方法签名,因此我们知道可用的方法并可以理解它们的作用。

Edit 编辑

class SudokuBoard
{
    public void setBoardFromArray( int[][] otherBoard )
    {
        for( int i = 0; i < otherBoard.length; i++ )
        {
            // Clone the arrays that actually have the data
            this.board[i] = otherBoard[i].clone();
        }
    }
}

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

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