我正在使用数组列表添加状态(8 谜题的棋盘状态)。我的问题是当我得到状态的孩子时,它会改变存储在我的数组列表中的值。我假设这是因为 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;
}
}