Java 2个相同的类对象被另一个相同的类对象错误实例化
我目前正在使用*算法(Hamming)方法实现八个难题,并提供了代码:Java 2个相同的类对象被另一个相同的类对象错误实例化,java,Java,我目前正在使用*算法(Hamming)方法实现八个难题,并提供了代码: public class Board { int board[][]; public Board() { board = new int[3][3]; } public void initialise(int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9) { //first row board[0][0] =
public class Board
{
int board[][];
public Board()
{
board = new int[3][3];
}
public void initialise(int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8, int n9)
{
//first row
board[0][0] = n1;
board[0][1] = n2;
board[0][2] = n3;
//second row
board[1][0] = n4;
board[1][1] = n5;
board[1][2] = n6;
//third row
board[2][0] = n7;
board[2][1] = n8;
board[2][2] = n9;
}
public int states_out_of_order()
{
int count = 0;
for(int i = 0; i < 3; i++)
{
//Checking if first row is
//1 2 3
if(i==0)
{
if(board[i][0]!=1)
{
count++;
}
if(board[i][1]!=2)
{
count++;
}
if(board[i][2]!=3)
{
count++;
}
}
//Checking if second row is
//4 5 6
if(i==1)
{
if(board[i][0]!=4)
{
count++;
}
if(board[i][1]!=5)
{
count++;
}
if(board[i][2]!=6)
{
count++;
}
}
//Checking if second row is
//7 8 0
if(i==2)
{
if(board[i][0]!=7)
{
count++;
}
if(board[i][1]!=8)
{
count++;
}
if(board[i][2]!=9)
{
count++;
}
}
}
return count;
}
public boolean GoalStateCheck()
{
//Checking first row
if(board[0][0]!=1)
{
return false;
}
if(board[0][1]!=2)
{
return false;
}
if(board[0][2]!=3)
{
return false;
}
//Checking second row
if(board[1][0]!=4)
{
return false;
}
if(board[1][1]!=5)
{
return false;
}
if(board[1][2]!=6)
{
return false;
}
//Checking third row
if(board[2][0]!=7)
{
return false;
}
if(board[2][1]!=8)
{
return false;
}
if(board[2][2]!=0)
{
return false;
}
return true;
}
public void printBoard()
{
System.out.print(board[0][0] + " " + board[0][1] + " " + board[0][2]);
System.out.println();
System.out.print(board[1][0] + " " + board[1][1] + " " + board[1][2]);
System.out.println();
System.out.print(board[2][0] + " " + board[2][1] + " " + board[2][2]);
System.out.println();
}
}
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
但是,在这两行中:
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
//Check move #1 from original state
b1 = SwapStates(b1, b1.board,0,0,0,1);
//Check move #2 from original state
//Problem is that it is not swapping ptr but rather its swapping b1. why?
b2 = SwapStates(b2, b2.board,0,0,1,0);
b2使用b1的交换状态,而不是我想要使用的原始ptr。为什么?当你写了
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
您没有创建名为ptr
的b
的不可变副本和名为b1
和b2
的ptr
的两个可变副本
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
您只创建了对同一对象的3个引用。使用4个引用中的任何一个对对象进行的任何修改最终都会以相同的方式影响对象,从而更改所有4个引用的可见状态
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
您应该做的是在Board类中添加一个public Board copy()
方法,该方法将返回Board的另一个实例(使用new Board()
并复制值),并通过以下方式更改代码:
final Board ptr = b;
Board b1 = ptr;
Board b2 = ptr;
final Board ptr = b.copy(); // but is there any sense to use ptr rather than b ?
Board b1 = ptr.copy();
Board b2 = ptr.copy();
ptr、b1和b2是同一对象。您可能想了解Java中引用是如何工作的,以及对象的可变性。我还通过创建副本构造函数解决了这个问题。