Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ArrayIndexOutOfBoundsException在Java中迭代二维数组_Java_Indexoutofboundsexception - Fatal编程技术网

ArrayIndexOutOfBoundsException在Java中迭代二维数组

ArrayIndexOutOfBoundsException在Java中迭代二维数组,java,indexoutofboundsexception,Java,Indexoutofboundsexception,对不起,这个问题不能真正应用于其他人。我已经看了几个小时了,还是看不见。它必须是超级简单,但我找不到我试图访问的东西不在那里。我得到一份工作 在9x9网格上迭代时出现ArrayIndexOutOfBoundsException public ArrayList next9Grids() { ArrayList next9=新ArrayList();//新网格列表 对于(int i=0;i

对不起,这个问题不能真正应用于其他人。我已经看了几个小时了,还是看不见。它必须是超级简单,但我找不到我试图访问的东西不在那里。我得到一份工作

在9x9网格上迭代时出现ArrayIndexOutOfBoundsException

public ArrayList next9Grids()
{
ArrayList next9=新ArrayList();//新网格列表
对于(int i=0;i
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:9 位于sudoku.Grid.next9Grids(Grid.java:112) sudoku.Solver.solveRecurse(Solver.java:54)位于 sudoku.Solver.solveRecurse(Solver.java:56)位于 sudoku.Solver.solveRecurse(Solver.java:56)

这就是错误的来源。(Grid.java:112)是-->
if(value[i][j]==0){

另一个问题是,为什么第112行在访问第二个for循环第一次迭代而不是第二个for循环的内容时会抛出错误


非常感谢您的反馈。

您在嵌套forloop中的条件是错误的。应该是
j
而不是
i

public ArrayList<Grid> next9Grids()
    {
        ArrayList<Grid> next9 = new ArrayList<Grid>();//list of new Grids

        for(int i = 0; i < values.length; i++){
            for(int j = 0; j < values[i].length; j++){
                if (values[i][j] == 0){//if empty
                    for(int next = 1; next <= 9; next++){
                        Grid temp9 = new Grid(this);//need to make another grid for each #
                        temp9.values[i][j] = next; // changes value of empty space to 1->9
                        next9.add(temp9); //add grid to arrayList
                    }
                    return next9;
                }
            }

        }
        return null;

    }
public ArrayList next9Grids()
{
ArrayList next9=新ArrayList();//新网格列表
对于(int i=0;i
试试这个:

for(int i = 0; i < values.length; i++){ 
            for(int j = 0; j < values[i].length; j++){// replaced j in (i < values[i].length) by i in (j < values[i].length)
                if (values[i][j] == 0){//if empty
                    for(int next = 1; next <= 9; next++){
for(inti=0;i对于代码第六行中的(int next=1;next,将i替换为j

public ArrayList<Grid> next9Grids(){

ArrayList<Grid> next9 = new ArrayList<Grid>(); //list of new Grids

for(int i = 0; i < values.length; i++){
    for(int j = 0; j < values[i].length; j++){  // The change is here
        if (values[i][j] == 0){ //if empty
            for(int next = 1; next <= 9; next++){
                Grid temp9 = new Grid(this);//need to make another grid for each #
                temp9.values[i][j] = next; // changes value of empty space to 1->9
                next9.add(temp9); //add grid to arrayList
            }
            return next9;
        }
    }
}
return null;
public ArrayList next9Grids(){
ArrayList next9=新ArrayList();//新网格列表
对于(int i=0;i

}
i应该是
jpublic ArrayList<Grid> next9Grids(){

ArrayList<Grid> next9 = new ArrayList<Grid>(); //list of new Grids

for(int i = 0; i < values.length; i++){
    for(int j = 0; j < values[i].length; j++){  // The change is here
        if (values[i][j] == 0){ //if empty
            for(int next = 1; next <= 9; next++){
                Grid temp9 = new Grid(this);//need to make another grid for each #
                temp9.values[i][j] = next; // changes value of empty space to 1->9
                next9.add(temp9); //add grid to arrayList
            }
            return next9;
        }
    }
}
return null;