Is there a better way I can create a copy of a variable?

I am currently making a small project in Java based on Conway's Game of Life. Right now I am on the very final steps, and I just have one quick question. So far everything works, but I am wondering if I can create a copy of the game board array in a way better than I am doing right now. Currently I have boolean[][] nextBoard = new boolean[maxRow][maxCol]; because boolean[][] nextBoard = new board.clone(); would still have a reference. The entire class is below, so please let me know if you know a better way. Also, if you see anything in my code that I can improve on (either syntax or formatting), I would greatly appreciate it because I am always looking to improve. Thanks!

public class GameOfLife
{
    private boolean[][] board;
    private int maxRow, maxCol;

    public GameOfLife(boolean[][] board)
    {
        if(board != null)
            this.board = board;
        else
            this.board = new boolean[1][1];

        this.maxRow = this.board.length;
        this.maxCol = this.board[0].length;
    }

    public void calculateNextGeneration()
    {
        boolean[][] nextBoard = new boolean[maxRow][maxCol];

        for(int row = 0; row < maxRow; row++)
        {
            for(int col = 0; col < maxCol; col++)
            {
                nextBoard[row][col] = nextState(row, col);
            }
        }

        board = nextBoard;
    }

    public void print()
    {
        for(int row = 0; row < maxRow; row++)
        {
            for(int col = 0; col < maxCol; col++)
            {
                if(isAlive(row, col))
                    System.out.print("*");
                else
                    System.out.print(".");  
            }
            System.out.println();
        }
    }

    public void setAlive(int row, int col, boolean isAlive)
    {
        if(inBounds(row, col))
            board[row][col] = isAlive;
    }

    public boolean isAlive(int row, int col)
    {
        if(inBounds(row, col))
            return board[row][col];
        else
            return false;
    }

    public boolean nextState(int rowPos, int colPos)
    {
        int alive = 0;

        for(int row = rowPos - 1; row <= rowPos + 1; row++)
        {
            for(int col = colPos - 1; col <= colPos + 1; col++)
            {
                if(!inBounds(row, col) || (row == rowPos && col == colPos))
                    continue;

                if(isAlive(row, col))
                    alive++;
            }
        }

        if(alive == 3)
            return true;
        else if(isAlive(rowPos, colPos) && alive == 2)
            return true;
        else
            return false;
    }

    private boolean inBounds(int row, int col)
    {
        if(row < 0 || row >= maxRow || col < 0 || col >= maxCol)
            return false;
        else
            return true;
    }
}

1 answer

  • answered 2018-02-13 00:46 ShioT

    In Java, a 2D array is actually just an array of array. For arrays holding primitive data types, cloning is the correct thing to do. Combining these two facts, if you want to clone an 2D boolean array in Java, the correct way is to create the boolean[][], then clone every boolean[] inside of it manually.

    However, since you are calculating all values inside the array anyways, I do not see the value of doing this clone operation. If you are concerned with memory usage (your inplementation takes 2 times the memory of the board) and you want to do micro-optimization to a crazy level, then you can consider the nature of game of life. Only storing 3 columns at a time and replace elements of the array makes the extra memory cost only 3x the row size, for example.