解决arrayoutofbounds

时间:2011-03-10 23:47:33

标签: java arrays

我正在尝试解决arrayoutofbounds异常 确切的错误是:

  

线程“main”中的异常   java.lang.ArrayIndexOutOfBoundsException:   -1   Java结果:1

    public static void emptyBox() {
        int , boxRow, boxColumn, i, j;
        emptyBox = 0;
        Row = 0;
        Column = 0;

        for (i = 0; (i < grid.length) && !(1 == emptyBox); i++) {
            for (j = 0; (i < grid[i].length) && !(1 == emptyBox); j++) {
                if (grid[i][j] == 0) {
                    emptyBox = 0;
                    Row = 0;
                    Column = 0;
                }
            }
        }
    }

    I've tried a few attempts at debugging but no dice.

2 个答案:

答案 0 :(得分:4)

    for (i = 0; (i < grid.length) && !(1 == emptyBoxFound); i++) {
        for (j = 0; (i < grid[i].length) && !(1 == emptyBoxFound); j++) {

---------------------^

应该是j而不是i

我不知道这是否只是 问题,但是......

答案 1 :(得分:0)

试试这个:

public static int[][] getSolution(int[][] grid) 
{
    int [][] solution = new int[grid.length];

    if (grid != null)
    {
        for (int i = 0; i < grid.length; ++i) 
        {
            grid[i] = new int[grid.length];
            System.arraycopy(grid[i], 0, SolveSudoku.grid[i], 0, grid.length);
        }
        int n = getZeroes();
        solution = getSolution(n);
    }

    return solution;
}