Note that there are some explanatory texts on larger screens.

plurals
  1. PODealing with ArrayList and pass by reference
    primarykey
    data
    text
    <p>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.</p> <p>I will also try to follow naming conventions more often thanks for the tip.</p> <pre><code> private ArrayList&lt;int[][]&gt;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&lt;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 &amp;&amp; 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; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload