繁体   English   中英

ArrayIndexOutOfBoundsException迭代Java中的2D数组

[英]ArrayIndexOutOfBoundsException iterating over 2D arrays in Java

抱歉,这个问题无法真正适用于其他人。 我已经看了几个小时,但仍然看不到它。 它必须非常简单,但我找不到要访问的内容。 我正在

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

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; i < 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;

}

线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:sudoku.Solver.solveRecurse(Solver.java:54)上sudoku.Solver.solveRecurse(Solver.java:54)上sudoku.Solver.solveRecurse(Solver.java:54)下的sudoku.Grid.next9Grids(Grid.java:112): 56)在sudoku.Solver.solveRecurse(Solver.java:56)

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

另一个问题是,为什么当访问第二个for循环首先迭代而不是第二个for循环的东西时,在第112行会引发错误?

一切都非常感谢。 感谢您的反馈。

您在嵌套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;

    }

试试这个:

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++){

在代码的第六行中,将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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM