简体   繁体   English

当我尝试运行此程序时,为什么会有(java.lang.ArrayIndexOutOfBoundsException:5)异常?

[英]why there is an (java.lang.ArrayIndexOutOfBoundsException: 5) exception when I try to run this program?

public class number_of_islands {
    public static void main(String[] args) {
        number_of_islands tester = new number_of_islands();
        char[][] testData = {{'1', '1', '1', '1', '0'}, 
                            {'1', '1', '0', '1', '0'},
                            {'1', '1', '0', '0', '0'},
                            {'0', '0', '0', '0', '0'}};
        System.out.println(tester.numIslands(testData));
    }

    public int numIslands (char[][] grid) {
        if (grid == null || grid.length == 0) {
            throw new IllegalArgumentException("Invalid argument");
        }

        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; i < grid[i].length; j++) {
                if ((grid[i][j] == '0')
                   /* the line below is where the above error occurs */
                   ||((i > 0) && (grid[i - 1][j] == '1'))
                   || ((j > 0) && (grid[i][j - 1] == '1'))) {
                    continue;
                }
                res++;
            }
        }
        return res;
    }
}

In the code above, the numIslands function accepts an 2-d array as the input and compute something, but there is an array out of bounds error in the line below the comment, I have checked to let i > 0 and j > 0, why the question still there? 在上面的代码中,numIslands函数接受一个二维数组作为输入并计算内容,但是在注释下方的行中有一个超出范围的错误,我检查了让i> 0和j> 0,为什么问题仍然存在?

Thanks! 谢谢!

Just make these small changes in for loops 只需在for循环中进行这些小的更改

for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[i].length; j++) {
...

Your condition in the second for loop was "i < grid[i].length" 您在第二个for循环中的条件是“ i <grid [i] .length”

暂无
暂无

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

相关问题 编译程序时出现错误(线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException) - I get the Error when i compile my program (Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException) 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? java.lang.ArrayIndexOutOfBoundsException:0,当我尝试访问args参数的第一个参数时,为什么? - java.lang.ArrayIndexOutOfBoundsException: 0 when I try to access to the first argument of args parameters, why? 当我运行我的代码时,出现一个错误,说“线程中的异常”主”java.lang.ArrayIndexOutOfBoundsException:-1” - An error saying "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1" shows up when I run my code “ java.lang.ArrayIndexOutOfBoundsException:1”运行时错误 - “java.lang.ArrayIndexOutOfBoundsException: 1” Error When Run 异常:java.lang.ArrayIndexOutOfBoundsException - Exception : java.lang.ArrayIndexOutOfBoundsException 渗透程序中的错误:线程“ main”中的异常java.lang.ArrayIndexOutOfBoundsException:0 - error in percolation program: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 为什么Java会引发异常java.lang.ArrayIndexOutOfBoundsException:1 - Why java throws exception java.lang.ArrayIndexOutOfBoundsException: 1 当我尝试运行此代码时,它将导致“线程“ main”中的异常“ java.lang.ArrayIndexOutOfBoundsException:0” - When i tried running this code it causes “ Exception in thread ”main“ java.lang.ArrayIndexOutOfBoundsException: 0” this exception 我有一个异常“ java.lang.ArrayIndexOutOfBoundsException”的问题 - i have a problem with exception “java.lang.ArrayIndexOutOfBoundsException”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM