Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Java 如何修复边界外异常错误?请解释一下_Java_Arrays - Fatal编程技术网

Java 如何修复边界外异常错误?请解释一下

Java 如何修复边界外异常错误?请解释一下,java,arrays,Java,Arrays,OOB异常。如何调试 int sum[]= new int[4], ctr = 0, sums=0; for(int row=0;row<x.length;row++){ int col = 0; for(int column=0;column<x[row].length;column++){ sum[row] += x[row][column];

OOB异常。如何调试

        int sum[]= new int[4], ctr = 0, sums=0;
        for(int row=0;row<x.length;row++){
            int col = 0;
            for(int column=0;column<x[row].length;column++){
                sum[row] += x[row][column];
                sums += x[row][column];
                col += x[column][row];
            }
            ctr++;
            System.out.println("The sum of column " + ctr +": " + col);
            System.out.println("The sum of row " + ctr +": " + sum[row]);
        }
        System.out.println("The sum of all arrays: "+sums);

    }
}
int sum[]=new int[4],ctr=0,sums=0;

对于(int row=0;row,只需在代码中添加这一行即可

 System.out.println(column+" "+row);
 col += x[column][row];
运行代码后..最后,
值变为3

因此,
x[0][3]
将为您提供OOB异常

您必须在某些条件下处理
col+=x[column][row];
,以便在row超过值3时永远不会执行此操作

for(int column=0;column<x[row].length;column++){
    sum[row] += x[row][column];
    sums += x[row][column];
    col += x[column][row];
}

for(int column=0;column那么我如何删除OOB异常?请进一步解释。请容忍我的无知。感谢您的回答非常感谢!我现在得到了AJ的帮助,“因此x[0][3]将给您OOB异常”。谢谢大家!可能是
for(int column=0;column<x[row].length;column++){
    sum[row] += x[row][column];
    sums += x[row][column]

    if (row < x[row].length){
        col += x[column][row];
    }
}