0
基本上稱謂。我創建了一個更大陣列的新片段,並且出現此錯誤。我必須創建一個更大的陣列的新fragemnt來顯示它。 (在Pokemon中思考,其中地圖是大數組,但顯示的屏幕是該地圖的片段。)以下是代碼。爲什麼我在這裏得到「ArrayIndexOutOfBoundsException」?
當方法被稱爲:
public void showWorldFragment(){
******ERROR WITH THIS LINE*********
int[][] map = getWorldFragment(mapPack.map);
if(map[0][0] == 1){
c.drawBitmap(balloon, 0, 0, null);
}
if(map[0][0] == 2){
c.drawBitmap(archer, 0, 0, null);
}
if(map[1][0] == 1){
c.drawBitmap(balloon, canvasWidth/3, 0, null);
}
if(map[1][0] == 2){
c.drawBitmap(archer, canvasWidth/3, 0, null);
}
if(map[2][0] == 1){
c.drawBitmap(balloon, 2*(canvasWidth/3), 0, null);
}
if(map[2][0] == 2){
c.drawBitmap(archer, 2*(canvasWidth/3), 0, null);
}
if(map[0][1] == 1){
c.drawBitmap(balloon, 0, canvasHeight/3, null);
}
if(map[0][1] == 2){
c.drawBitmap(archer, 0, canvasHeight/3, null);
}
if(map[1][1] == 1){
c.drawBitmap(balloon, canvasWidth/3, canvasHeight/3, null);
}
if(map[1][1] == 2){
c.drawBitmap(archer, canvasWidth/3, canvasHeight/3, null);
}
if(map[2][1] == 1){
c.drawBitmap(balloon, 2*(canvasWidth/3), canvasHeight/3, null);
}
if(map[2][1] == 2){
c.drawBitmap(archer, 2*(canvasWidth/3), canvasHeight/3, null);
}
if(map[0][2] == 1){
c.drawBitmap(balloon, 0, 2*(canvasHeight/3), null);
}
if(map[0][2] == 2){
c.drawBitmap(archer, 0, 2*(canvasHeight/3), null);
}
if(map[1][2] == 1){
c.drawBitmap(balloon, (canvasWidth/3), 2*(canvasHeight/3), null);
}
if(map[1][2] == 2){
c.drawBitmap(archer, (canvasWidth/3), 2*(canvasHeight/3), null);
}
if(map[2][2] == 1){
c.drawBitmap(balloon, 2*(canvasWidth/3), 2*(canvasHeight/3), null);
}
if(map[2][2] == 2){
c.drawBitmap(archer, 2*(canvasWidth/3), 2*(canvasHeight/3), null);
}
}
當方法被稱爲:
public int[][] getWorldFragment(int[][] map) {
int centerX = x;
int centerY = y;
if(x == 0){
centerX = 1;
}
if(x == map.length){
centerX = map.length-1;
}
if(y == 0){
centerY = 1;
}
if(y == map.length){
centerY = map.length-1;
}
int[][] tempFragmentArray = new int[][]{
{map[centerX-1][centerY-1], map[centerX][centerY-1], map[centerX + 1][centerY - 1]},
{map[centerX-1][centerY], map[centerX][centerY], map[centerX + 1][centerY]},
{map[centerX-1][centerY+1], map[centerX][centerY+1], map[centerX + 1][centerY + 1]},
};
return tempFragmentArray;
}
的mapPack.map是一個較大的5×5陣列,只是保持基本整數值。有沒有人有任何想法,爲什麼會發生這種情況?
讓我知道是否需要更多細節。
謝謝:)
因爲它超出了界限。不要懷疑。找到數組的* actual *大小和* actual *索引。至少,例外消息將指示後者。 – user2864740 2015-04-04 01:57:35
什麼行?向我們展示堆棧跟蹤。 – 2015-04-04 02:03:52
這是使用調試器來調試代碼的地方,它真的是最好的開始。 – 2015-04-04 02:05:59