所以这是我使用的双数组
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 } };
我的问题是,当我尝试使用它加载相应的图像时,我收到一条错误消息,指出当 y = 12 时它超出范围。
Image[][] displayedMap = new Image[MAP[0].length][MAP.length];
public Town() {
System.out.println("Map len" + MAP.length);
for (int x = 0; x < MAP[0].length; x++) {
for (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();
}
当城镇构造函数中 y 变为 11,然后转到它所说的 setImagecontent 时
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 11
最佳答案
您交换了数组的尺寸,在数组的一部分中,您使用“X 轴”的大小作为 Y 轴,反之亦然
关于java - 数组越界,但我不确定为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983163/