java - Array out of bounds but im not sure why? -
so double array im using
public int[][] map = { { 8, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8 }, { 8, 8, 9, 9, 8, 8, 8, 8, 8, 8, 8 }, { 8, 8, 8, 9, 8, 8, 8, 8, 8, 8, 8 }, { 8, 8, 8, 9, 9, 8, 8, 8, 8, 8, 8 }, { 8, 8, 8, 8, 9, 8, 8, 8, 8, 8, 8 }, { 8, 8, 8, 9, 9, 9, 8, 8, 8, 8, 8 }, { 8, 8, 9, 9, 9, 8, 8, 8, 8, 8, 8 }, { 8, 8, 8, 9, 9, 9, 8, 8, 8, 8, 8 }, { 8, 8, 8, 8, 8, 9, 9, 9, 9, 8, 8 }, { 8, 8, 8, 8, 8, 9, 9, 9, 8, 8, 8 }, { 9, 8, 8, 8, 8, 8, 9, 9, 9, 8, 8 }, { 9, 8, 8, 8, 8, 8, 9, 9, 9, 8, 8 }, { 9, 8, 8, 8, 8, 8, 9, 9, 9, 8, 8 }, { 9, 8, 8, 8, 8, 8, 9, 9, 9, 8, 8 }, { 9, 8, 8, 8, 8, 8, 9, 9, 9, 8, 8 } }; my problem when try use load corresponding image, error saying out of bounds when y = 12.
image[][] displayedmap = new image[map[0].length][map.length]; public town() { system.out.println("map len" + map.length); (int x = 0; x < map[0].length; x++) { (int y = 0; y < map.length; y++) { system.out.println("x:" + x + ",y:" + y); setimagecontent(x, y); } } } private void setimagecontent(int x, int y) { terrain t = terrain.getterrainfor(map[x][y]); displayedmap[x][y] = t.getimage(); } its when y turns 11 in town constructor , goes setimagecontent says
exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: 11
you have array column length of 11, row length of 15. have flipped array references, , getting exception since rows , columns of different lengths.
try instead:
image[][] displayedmap = new image[map[0].length][map.length]; public town() { system.out.println("map len" + map.length); (int x = 0; x < map.length; x++) { (int y = 0; y < map[0].length; y++) { system.out.println("x:" + x + ",y:" + y); setimagecontent(x, y); } } } private void setimagecontent(int x, int y) { terrain t = terrain.getterrainfor(map[x][y]); displayedmap[x][y] = t.getimage(); } notice i'm using map.length limit x , map[0].length limit y now.
you've got x , y flipped. x number of rows, y number of columns.
might suggest check out tiled. easier way of doing sort of thing. no more coding tiled maps source files hand! edit them in nice program, parse data file in. there many different formats, , parsers.
Comments
Post a Comment