我无法在 Java 中为我的二维数组赋值。代码的最后一行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';
}
}
}
}