1

I'm attempting to iterate through a list, 'breadthBoard', and add an array to it,'board'. However, each array I add into the array somehow turns into the into a the original array which is then duplicated even though I've tested the array has been changed.

neighbourNodes is a list which contains all the values adjacent to the currentNode on the board.

public List breadthBoard(List neighbourNodes, int [] currentNode, int [][] board)
{

    int x = currentNode[0] - 1; 
    int y = currentNode[1] - 1;
    //create lists
    List breadthBoard = new ArrayList();

for (int i=0; i<3;i++)
        {
            for(int j=0; j<3;j++)
            {

                if (neighbourNodes.contains(board[i][j]))
                {
                    // the temp variables allow me to switch the values then switch back later on
                    int temp = board[i][j];
                    int temp2 = board[x][y];
                    //initial switch
                    board[i][j] = temp2;
                    board[x][y] = temp;// at this point I get a successful swap but it isn't getting added to the breadth board
                    breadthBoard.add(board);

                    //test to see if I get the right results which I do
                    System.out.println("what's being added into breadth board (should be swapped)" + Arrays.deepToString(board)); 
                    System.out.println('\n');

                    switching the values back to the original postions
                    board[i][j] = temp;
                    board[x][y] = temp2;
                    System.out.print("back to normal " + Arrays.deepToString(board));// another test passed
                    System.out.println('\n');

                } 

            }

2 Answers2

0

You need to make a method that makes a deep copy of board. clone only makes a shallow copy.

I would advice wrapping the board inside a class, that has a swap method that simply return a new board:

class Board {
    private final int[][] board;

    public Board(Board other) {
        this.board = new int[other.board.length][];
        for(int i = 0; i < board.length; i++) {
            board[i] = Arrays.copyOf(other.board[i], other.board[i].length);
        }
    }

    public Board swap(int i, int j, int x, int y) {
        Board result = new Board(this); // copy this board

        // swap elements    
        result.board[i][j] = board[x][y];
        result.board[x][y] = board[i][j];

        return result;
    }

    ...

}
// in the loops
breadthBoard.add(board.swap(i, j, x, y));
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
0

I did some tests. In fact, clone() makes a deep copy with an array of primitive types but it doesn't work in the same way with a two dimension array of primitives.
With an array with two dimensions, you should iterate on the first dimension and perform clone() on the int[]array of the second dimension :

   board[i][j] = temp2;
   board[x][y] = temp;

   //Here, I suppose that the second dimension has always the same size. 
   int[][] clonedBoard = new int[board[0].length][board[1].length];

   for (int t = 0; t < board.length; t++) {
     clonedBoard[t] = board[t].clone();
   }
   breadthBoard.add(clonedBoard);
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • :) No problem. For information, with this way to do, the swap is not required any longer. The idea is to clone first the array, then you modify directly the cloned array. – davidxxx Dec 08 '16 at 09:54