简体   繁体   中英

Making copies of Objects in Java

I have this Java code:

boolean[][] defaultBoard = {{false, false, false},
                            {false, false, false},
                            {false, false, false}};
Board board = new Board(defaultBoard);
Board nextBoard = new Board(defaultBoard);
nextBoard.getBoard()[1][5] = true;
board.printBoard();

printBoard :

public void printBoard() {
    for (boolean row[] : board) {
        for (boolean cell : row) {
            System.out.print( cell ? "#" : "." );
        }
        System.out.println();
    }
    System.out.println();
}

But it returns

...
.#.
...

Which makes me think that nextBoard and board have got the same memory address. My question is: How do create a copy of nextBoard so that when nextBoard is edited, board isn't edited as well?

It's not board and nextBoard that are the same. It is the defaultBoard array you are passing to the constructor. You need to pass different arrays. Otherwise, you are just modifying the same array.

Better yet, to ensure instances of Board always contain a new array, I would recommend doing a deep copy of the array inside the constructor.

An array is an object, so you will pass a reference to the Board constructor. Since you are using the same array, both board and nextBoard are sharing the same object. To avoid this situation, use the clone method. This way, you will have a copy of the defaultBoard array and every instance you have their own board. It will look like this:

Board board = new Board(cloneBoard(defaultBoard));
Board nextBoard = new Board(cloneBoard(defaultBoard));

private boolean[][] cloneBoard(boolean[][] boardToClone) {
    boolean[][] clone = new boolean[boardToClone.length][];
    for(int i = 0; i < boardToClone.length; i++) {
        clone[i] = boardToClone[i].clone();
    }
    return clone;
}

使用.clone()方法,存在于任何Java Object子类中,即所有这些方法,就我所知。

In this case, I would have the board create the array base don a size

class Board {
    final boolean[] board;

    public Board(int width, height) {
        board = new[width][height];
    }

    public boolean[][] getBoard() { return board; }

    public void set(int x, int y, boolean value) { board[x][y] = value; }
}

Now your code looks like this

Board board = new Board(3, 3);
Board nextBoard = new Board(3, 3);
nextBoard.set(1, 2, true); // the last element is 2 not 5
board.printBoard();

每次需要将它传递给对象时,您需要创建一个新的新数组,以便对象看到一个新数组,而不是相同的数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM