所以這是一個即時通訊使用數組越界,但我不知道爲什麼?
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日在鎮構造函數,然後去,它說
異常在線程 「AWT-EventQueue的 - 0」的setImagecontentjava.lang.ArrayIndexOutOfBoundsException:11
我將長度設置爲MAP.length,在本例中爲15,所以它將是0-14 – Exikle
正確,但索引是翻轉的。看到我所做的改變? –
謝謝你刪除downvotes誰刪除他們。我很感激。 –