Java で 2D 配列に値を割り当てることができません。コードの最後の行 でエラーtheGrid[rowLoop][colLoop] = 'x';
がスローされていArrayIndexOutOfBoundsException
ます。誰かがなぜこれが起こっているのか説明してもらえますか?
これは私のコードです...
public class Main {
public static char[][] theGrid;
public static void main(String[] args) {
createAndFillGrid(10,10);
}
public static void createAndFillGrid(int rows, int cols) {
theGrid = new char[rows][cols];
int rowLoop = 0;
for (rowLoop = 0; rowLoop <= theGrid.length; rowLoop++) {
int colLoop = 0;
for (colLoop = 0; colLoop <= theGrid[0].length; colLoop++) {
theGrid[rowLoop][colLoop] = 'x';
}
}
}
}