Java 对象分配问题

Java 对象分配问题,java,oop,object,reference,Java,Oop,Object,Reference,我在指定对象时遇到问题,该对象会生成一个引用副本,从而更改原始对象中的值 这是我的原始对象 在deque数据结构中推送根之后,我在tmp变量中检索它 之后,我希望我的tmp变量应用所有操作符(向上、向下、向左、向右,哪一个是可能的)并检查最佳结果 if ( !tmp.goalTest(goal) ) { if ( tmp.ifDown() ) { if ( !visited[tmp.getX()+1]

我在指定对象时遇到问题,该对象会生成一个引用副本,从而更改原始对象中的值

这是我的原始对象

在deque数据结构中推送根之后,我在tmp变量中检索它

之后,我希望我的tmp变量应用所有操作符(向上、向下、向左、向右,哪一个是可能的)并检查最佳结果

            if ( !tmp.goalTest(goal) ) {
                if ( tmp.ifDown() ) {
                    if ( !visited[tmp.getX()+1][tmp.getY()] ) {
                        newDown = new Node(tmp);
                        newDown.moveDown();
                    } else newDown = null;
                } // #EndOfDown!
                if ( tmp.ifUp() ) {
                    if ( !visited[tmp.getX()-1][tmp.getY()] ) {
                        newUp = new Node(tmp);
                        newUp.moveUp();
                    } else newUp = null;
                } // #EndOfUp!
                if ( tmp.ifLeft() ) {
                    if ( !visited[tmp.getX()][tmp.getY()-1] ) {
                        newLeft = new Node(tmp);
                        newLeft.moveLeft();
                    } else newLeft = null;
                } // #EndOfMoveLeft!
                if ( tmp.ifRight() ) {
                    if ( !visited[tmp.getX()][tmp.getY()+1] ) {
                        newRight = new Node(tmp);
                        newRight.moveRight();
                    } else newRight = null;
                } // #EndOfMoveRight!
            } else break;
操作员功能

我尝试创建一个复制构造函数,但没有解决问题

复制构造函数

public Node ( Node tmp ) {
    this.board = new char[5][5];
    for ( int i=0; i<5; ++i ) {
        for ( int j=0; j<5; ++j ) this.board[i][j] = tmp.board[i][j];
    }

    this.x_blank = tmp.x_blank;
    this.y_blank = tmp.y_blank;

    if ( tmp.parent != null ) this.parent = tmp.parent;

    if ( tmp.up != null ) this.up = tmp.up;
    if ( tmp.left != null ) this.left = tmp.left;
    if ( tmp.right != null ) this.right = tmp.right;
    if ( tmp.down != null ) this.down = tmp.down;
}
公共节点(节点tmp){
this.board=新字符[5][5];

对于(int i=0;i将该对象的实例赋值给新变量时,不会自动使用复制构造函数。在Java中,对象赋值是通过引用进行的。您需要在赋值中显式使用复制构造函数:

Node tmp = new Node(deque.pop());

我认为最好的方法是实现一个克隆函数,它将创建一个具有相同值的新对象,这样就不会更改原始对象的值

例如:

clone(Node mynode){
    return new Node(mynode.getValue());
    }    

tmp似乎很好,因为它无论如何都不会被更改,newDown=newnode(tmp);新系列的其余部分实际上并没有创建自己的内存域,而是引用tmps原始值。您应该给出一个更具体的示例,说明如何正确使用/实现克隆。我真的不理解这部分。克隆是什么意思?我甚至没有实现像getValue()这样的任何东西;clone是object类型的方法。从javadoc:clone()-创建并返回此对象的副本。
private char[][] board;

private int x_blank = -1;
private int y_blank = -1;

private Node parent = null;

private Node up = null;
private Node left = null;
private Node right = null;
private Node down = null;

/* Constructor
    Allocates board to a 5x5 2D Array
*/
public Node ( ) { board = new char[5][5]; } 
public Node ( Node tmp ) {
    this.board = new char[5][5];
    for ( int i=0; i<5; ++i ) {
        for ( int j=0; j<5; ++j ) this.board[i][j] = tmp.board[i][j];
    }

    this.x_blank = tmp.x_blank;
    this.y_blank = tmp.y_blank;

    if ( tmp.parent != null ) this.parent = tmp.parent;

    if ( tmp.up != null ) this.up = tmp.up;
    if ( tmp.left != null ) this.left = tmp.left;
    if ( tmp.right != null ) this.right = tmp.right;
    if ( tmp.down != null ) this.down = tmp.down;
}
Node tmp = new Node(deque.pop());
clone(Node mynode){
    return new Node(mynode.getValue());
    }