[Solved] How to convert my byte[] methods to arraylist [] methods? [closed]


Replacing byte[][] with a List<List<Byte>> makes little sense, but here is how to do it.

First, though, your code is inconsistent with order of array indexes:

  • getLive() parameters declared as x,y, but you call it with if (getLive(y, x) == 1).
  • In getLive(), you use board[y][x], but then you use testBoard[x][y + movedistance] = 1;
  • But you also use getHeight() with x, and getWidth() with y, so maybe it (accidentally?) “adds up”.

I’ll assume that method should always be x,y, array should be [y][x], and that x is “width” and y is “height”.

Also, [y + movedistance] will cause ArrayIndexOutOfBoundsException, since your loop uses full range of values. I’ll assume you want to “wrap around” on overflow.

public byte getLive(int x, int y) {
    return board.get(y).get(x);
}

public void patternRight(){
    List<List<Byte>> testBoard = new ArrayList<>();
    for (int y = 0; y < getHeight(); y++) {
        List<Byte> row = new ArrayList<>();
        for (int x = 0; x < getWidth(); x++)
            row.add((byte) 0);
        testBoard.add(row);
    }
    for (int x = 0; x < getWidth(); x++) {
        for (int y = 0; y < getHeight(); y++) {
            if (getLive(x, y) == 1)
                testBoard.get((y + movedistance) % getHeight()).set(x, (byte) 1);
        }
    }
}

solved How to convert my byte[] methods to arraylist [] methods? [closed]