无法在Java 2D数组中赋值-ArrayIndexOutOfBoundsException
我在Java中为2D数组赋值时遇到问题。代码的最后一行,网格[rowLoop][colLoop]=“x”;,正在抛出ArrayIndexOutOfBoundsException错误。有人能解释一下为什么会这样吗 这是我的密码无法在Java 2D数组中赋值-ArrayIndexOutOfBoundsException,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,我在Java中为2D数组赋值时遇到问题。代码的最后一行,网格[rowLoop][colLoop]=“x”;,正在抛出ArrayIndexOutOfBoundsException错误。有人能解释一下为什么会这样吗 这是我的密码 public class Main { public static char[][] theGrid; public static void main(String[] args) { createAndFillGrid(10,10);
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';
}
}
}
}
这里是问题rowLoop这里是问题rowLoop,这是一个难以置信的快速答案!而且很有效,非常感谢@马什,很乐意帮忙。一旦系统允许,不要忘记接受这个答案。这是一个难以置信的快速答案!而且很有效,非常感谢@马什,很乐意帮忙。一旦系统允许,不要忘记接受这个答案。
rowLoop < theGrid.length
colLoop < theGrid[0].length