java:2D数组,行多于列返回:ArrayIndexOutOfBoundsException:6
我有一个2D数组,它的行比列多。在这个数组中,我将一列中的所有值逐行求和并返回结果 在那之前,一切都很好,但在最后一个for循环之后,我得到了java:2D数组,行多于列返回:ArrayIndexOutOfBoundsException:6,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个2D数组,它的行比列多。在这个数组中,我将一列中的所有值逐行求和并返回结果 在那之前,一切都很好,但在最后一个for循环之后,我得到了 异常:java.lang.ArrayIndexOutOfBoundsException:6 从其他线程我只得到消息,这意味着行和列的数量是不均匀的。不幸的是,在我的情况下,这需要成为一种选择 请参见下面的我的代码: double max = 0; int machine = 0; for(int c = 0; c < excelMatrix.le
异常:java.lang.ArrayIndexOutOfBoundsException:6
从其他线程我只得到消息,这意味着行和列的数量是不均匀的。不幸的是,在我的情况下,这需要成为一种选择
请参见下面的我的代码:
double max = 0;
int machine = 0;
for(int c = 0; c < excelMatrix.length-1; c++){
double sum = 0;
for(int r = 0; excelMatrix.length-1; r++){
sum += excelMatrix[r][c];
}
if(max < sum){
max = sum;
machine = c;
}
}
double max=0;
int机器=0;
对于(int c=0;c
该代码一直工作到结束,但在r的最后一个for循环后返回异常。通常,当您在2D数组中循环时,使用以下代码:
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
正确的代码为:
double max = 0;
int machine = 0;
for(int c = 0; c < excelMatrix.length; c++){
double sum = 0;
for(int r = 0; excelMatrix[c].length; r++){
sum += excelMatrix[c][r];
}
if(max < sum){
max = sum;
machine = c;
}
}
double max=0;
int机器=0;
对于(int c=0;c
正确的代码应该是:
double max = 0;
int machine = 0;
int excelMatrix[][] =new int[][]{{1,2,3},{4,5,6}};
//System.out.println(excelMatrix.length);
//System.out.println(excelMatrix[0].length);
for(int c = 0; c <= excelMatrix[0].length-1; c++){
double sum = 0;
for(int r = 0; r<=excelMatrix.length-1; r++){
sum += excelMatrix[r][c];
}
if(max < sum){
max = sum;
machine = c;
}
}
System.out.println(max);
double max=0;
int机器=0;
int EXCEL矩阵[][]=新int[][{{1,2,3},{4,5,6};
//System.out.println(excelMatrix.length);
//System.out.println(excelMatrix[0].长度);
对于(int c=0;c是的,是的,但是我没有得到手动操作时应该得到的结果。它确实有效,但始终只返回0。看起来代码没有将数组的值添加到总和中。你知道为什么会这样吗?在我的原始代码中,我得到了正确的答案,但最后有一个例外,现在我没有得到异常,但没有得到正确的答案。奇怪的是,您有逻辑错误,例如if(maxsum)
double max = 0;
int machine = 0;
int excelMatrix[][] =new int[][]{{1,2,3},{4,5,6}};
//System.out.println(excelMatrix.length);
//System.out.println(excelMatrix[0].length);
for(int c = 0; c <= excelMatrix[0].length-1; c++){
double sum = 0;
for(int r = 0; r<=excelMatrix.length-1; r++){
sum += excelMatrix[r][c];
}
if(max < sum){
max = sum;
machine = c;
}
}
System.out.println(max);