我使用arraylist添加状态(8拼图板状态)。我的问题是当我得到状态的孩子时,它改变了存储在我的数组列表中的值。我假设这是因为ArrayList只存储指向对象的指针而不是值本身。为了解决这个问题,我在每次将它存储到ArrayList之前创建一个新对象,但我仍然遇到同样的问题。处理ArrayList并通过引用
我也会尝试更经常地遵循命名约定感谢提示。
private ArrayList<int[][]>VisitedBoard;
if(RuleNumber ==2){
//Here is my problem. This will change what is stored in VistedBoards
NextState = new State(FireRule.Rule2(WM.get_Board()));//Fire Rule
for(int j=0;j<VisitedBoards.size();j++){
//Meaning this will always be true
if(Arrays.equals(VisitedBoards.get(j), NextState.get_Board())){
Loop =true; //Loop to previous state
}
if(j==VisitedBoards.size()-1 && Loop ==false){ //If the next state is not any previously visited
NotALoop =true;
VisitedBoards.add(NextState.get_Board());
WM.set_Board(NextState.get_Board());
}
}
}
public int[][] Rule2(int [][] Board){//The FireRule Class
Find_BlankLocation(Board);
int temp;
State NewState;
temp = Board[BlankLocation[0]-1][BlankLocation[1]];
Board[BlankLocation[0]-1][BlankLocation[1]] = 0;
Board[BlankLocation[0]][BlankLocation[1]] = temp;
NewState = new State(Board);
return Board;
}
public class State { //State class
private int[][] Board;
private int[][] Goal;
private Boolean GoalFound;
public State(int[][] Start, int[][] goal){
Board = Start;
Goal = goal;
GoalFound=false;
}
public State(int[][] NewState){
Board=NewState;
}
public int[][] get_Goal(){
return Goal;
}
public int[][] get_Board(){
return Board;
}
public void set_Board(int[][] board){
Board = board;
}
public Boolean get_GoalFound(){
return GoalFound;
}
}
由于您不遵循命名约定,因此很难遵循您的代码。类名应该大写,变量名应该是小写。 Java中不使用下划线(通常)。当你习惯使用不遵循这些约定的代码时,你会感到惊讶。 – jahroy 2013-03-22 22:01:46
请显示“NextState”类的代码。我们需要看看'get_Board()'方法真的知道发生了什么。 – jahroy 2013-03-22 22:03:24
我甚至都不明白你在问什么。请检查你的问题,并明确指出你看到的结果和预期的结果。可能,发布一个简明的代码示例,显示您的问题,并可以运行 – Raffaele 2013-03-22 22:13:43