java - Check if index is OutOfBounds in a 2D array -
so i'm building sort of "game of life" application in java , having trouble detecting when index out of bounds.the code below modified version qustion here so.
2d array:
1,1,1,1 1,1,1,1 1,1,1,1 1,1,1,1
code
private void traverse() { (int = 0; < brickarray.length; i++) { (int j = 0; j < brickarray[i].length; j++) { system.out.println(i - 1 + "," + j); checkifallowed(i - 1, j); } } } public void checkifallowed(int row, int column) { boolean incorrect = check(row) && check(column); if (incorrect) { system.out.println("wall"); } else { system.out.println("road"); } } private boolean check(int index) { return (index < 0 || index > board.getheight()); }
result:
traversing:
-1,0 road -1,1 road -1,2 road -1,3 road 0,0 road 0,1 road 0,2 road 0,3 road 1,0 road 1,1 road 1,2 road 1,3 road 2,0 road 2,1 road 2,2 road 2,3 road
in example, 4 top cells should have printed "wall", didn't.
the pitfall of negative names, , neutral names (check
).
boolean incorrect = check(row) || check(column);
when either row or column incorrect, whole coordinates incorrect.
i write check
out here, board must square using getheight in check.
Comments
Post a Comment