【问题标题】:ArrayIndexOutOfBoundsException iterating over 2D arrays in JavaArrayIndexOutOfBoundsException 在 Java 中迭代二维数组
【发布时间】:2017-04-03 05:33:26
【问题描述】:

抱歉,这个问题不能真正应用于其他人。我已经看了几个小时了,我仍然看不到它。它一定非常简单,但我找不到我试图访问不存在的东西的地方。我得到了一个

遍历 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: 9 在数独.Grid.next9Grids(Grid.java:112) 在 数独.Solver.solveRecurse(Solver.java:54) 在 数独.Solver.solveRecurse(Solver.java:56) 在 数独.Solver.solveRecurse(Solver.java:56)

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

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

任何事情都非常感谢。感谢您的反馈。

【问题讨论】:

  • i &lt; values[i].length 应该是j &lt; values[i].length

标签: java indexoutofboundsexception


【解决方案1】:

试试这个:

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

【讨论】:

  • 原来如此 ༼ つ ◕_◕ ༽つ。我现在很生气也很开心。谢谢你。我想这就是剥夺睡眠对你的影响。
【解决方案2】:

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

    }

【讨论】:

  • 感谢您没有让您的答案成为其他人更快回答的答案。谢谢你的回答。也许有一天我可以帮你解决问题。
【解决方案3】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-29
    • 2011-12-28
    • 2015-04-04
    • 2014-05-30
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    相关资源
    最近更新 更多