Dealing with ArrayList and pass by reference

nsan3

New Member
I'm using a arraylist to add states(the board state for the 8 puzzle). My problem is when I get the children of the state it changes the values stored in my array list. I'm assuming this is because ArrayList just stores pointers to the objects and not the values themselves. In order to fix this I create a new object every time before I store it into the ArrayList but I'm still having the same problem.I will also try to follow naming conventions more often thanks for the tip.\[code\] 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 classprivate 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;}\[/code\]}
 
Back
Top