Java comparing 2D array to digit is giving exceptions -
i'm trying compare 2d array digit, throwing exception.
code
import org.newdawn.slick.color; public class main { public static int[][] map = {{0, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {1, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {2, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {3, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {4, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {5, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {6, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}, {7, 2, 1, 3, 4, 1, 1, 3, 4, 1, 3, 5, 1}}; public static void main(string args[]) { int = 0; int ii = 0; (int y = 0; y < 7; y++) { (int x = 0; x < 11; x++) { if (map[x][y] == 0) { g.setcolor(color.blue); g.fillrect(i, ii, 100, 100); } if (map[x][y] == 1) { g.setcolor(color.orange); g.fillrect(i, ii, 100, 100); } if (map[x][y] == 2) { g.setcolor(color.white); g.fillrect(i, ii, 100, 100); } if (map[x][y] == 3) { g.setcolor(color.red); g.fillrect(i, ii, 100, 100); } if (map[x][y] == 4) { g.setcolor(color.green); g.fillrect(i, ii, 100, 100); } if (map[x][y] == 5) { g.setcolor(color.gray); g.fillrect(i, ii, 100, 100); } = x * 100; = y * 100; } } } }
the exception throwing java.lang.arrayindexoutofboundsexception: 8
bare bones tile map system, if want test need slick2d , need put in render loop.
it because trying access row 8
, have maximum of position 7
.
remember index of rows , columns starts in position 0 , ends in length-1.
also, have remember first position of array, in case, x
, rows, , y
columns.
array[rows][columns]
so have change order of variables in loops. this:
for(int x = 0; x < map.length; x++){ for(int y = 0; y < map[x].length; y++){
the rest of code can have above.
i expect helpful you!
Comments
Post a Comment