好吧,基本上,我有一個多維數組,Board [8] [8]。我試圖在數組內使用隨機值並使其成爲不同的值。我正在改變的價值必須已經是一定的價值。我正在運行的代碼保持這些結果:更改二維數組中的值
java.lang.ArrayIndexOutOfBoundsException: 8
at checkers.init(checkers.java:32)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
這是導致問題的代碼。注意第8行是一個變量聲明:
int BLACK = 1;
Random generator = new Random();
int checkersCount_B = 0, checkersCount_W = 0, x, y;
while(checkersCount_B < 9){
x = generator.nextInt(9);
y = generator.nextInt(9);
if(Board[x][y] == BLACK){
Board[x][y] = BLACK_CHECKER;
// System.out.println(x + " " + y);
checkersCount_B ++;
} else{
//nothing
}
}
第32行是if語句。
該代碼適用於幾個運行while循環,但從來沒有使過去兩三年,任何建議?
這裏是關於一個更深入的描述爲什麼數組是0索引:http://developeronline.blogspot.com/2008 /04/why-array-index-should-start-from-0.html – 2013-03-21 20:45:05