title: Adding different arrayStates to ArrayList without changing original arrayState
body: <p>I'm currently trying to solve a 2d slider puzzle game in java. I want to solve the puzzle using BFS, but whenever I add vertices to the tree, the parent node gets changed and all the adjacent vertices are the same. Let me show some code to explain what I mean. </p>
  
  <p>Here is my GameState object, which is what is representing every node in the tree. </p>
  
  <pre><code>public class GameState {
  public int[][] state; //state of the puzzle
  public GameState parent; //parent in the game tree
  
  
  public GameState(int[][] state, GameState parent) {
      //initialize this.state to state, this.parent to parent
      this.state = new int[3][3];
  
      for (int i = 0; i &lt; 3; i++){
          for (int j = 0; j &lt; 3; j++) {
              this.state[i][j] = state[i][j];
          }
      }
  
      this.parent = parent;
  }
  </code></pre>
  
  <p>And here is my method to get all possible adjacent vertices.</p>
  
  <pre><code>public ArrayList&lt;GameState&gt; getAdjacent() {
      //Use the swap functions to generate the new vertices of the tree
      //Beware of the boundary conditions, i.e. don’t swap left when you are
      //already on the left edge
  
      ArrayList&lt;GameState&gt; vertices = new ArrayList&lt;&gt;();
      int row = 0;
      int col = 0;
  
  
      //Gets my zero int index
      for (int i = 0; i &lt; 3; i++){
          for (int j = 0; j &lt; 3; j++) {
              if(this.state[i][j] == 0){
                  row = i;
                  col = j;
              }
          }
      }
  
      //Checking if we can swap up
      if(row != 0) {
          vertices.add(swapUp(this, row, col));
      }
  
      //Checking if we can swap down
      if(row != 2) {
          vertices.add(swapDown(this, row, col));
      }
  
      //Checking if we can swap left
      if(col != 0) {
          vertices.add(swapLeft(this, row, col));
      }
  
      //Checking if we can swap right
      if(col != 2) {
          vertices.add(swapRight(this, row, col));
      }
  
  
      return vertices;
  }
  </code></pre>
  
  <p>Lastly, for example, here is what happened when swapUp is called. </p>
  
  <pre><code>public GameState swapUp(GameState s, int row, int col) {
  
      s.parent = s;
  
      int temp = state[row][col]; // Mark zero as temp
      s.state[row][col] = s.state[row-1][col];
      s.state[row-1][col] = temp;
  
  
      return s;
  }
  </code></pre>
  
  <p>I need every gamestate added to the arrayList to be different, but when I test the program this is what gets outputted as the vertices added to the list. </p>
  
  <pre><code>[1, 5, 0]
  [3, 7, 4]
  [6, 8, 2]
  1
  [1, 5, 0]
  [3, 7, 4]
  [6, 8, 2]
  2
  [1, 5, 0]
  [3, 7, 4]
  [6, 8, 2]
  </code></pre>