二维数组中的列的总和

static double [][] initialArray = {{7.432, 8.541, 23.398, 3.981}, {721.859, 6.9211, 29.7505, 53.6483}, {87.901, 455.72, 91.567, 57.988}}; public double[] columnSum(double [][] array){ int index = 0; double temp[] = new double[array[index].length]; for (int i = 0; i < array[i].length; i++){ double sum = 0; for (int j = 0; j < array.length; j++){ sum += array[j][i]; } temp[index] = sum; System.out.println("Index is: " + index + " Sum is: "+sum); index++; } return temp; } public static void main(String[] args) { arrayq test = new arrayq(); test.columnSum(initialArray); } 

我想得到所有列的总和,但我不断得到一个outofboundsexception。 这是我得到的输出:

 Index is: 0 Sum is: 817.192 Index is: 1 Sum is: 471.18210000000005 Index is: 2 Sum is: 144.7155 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at NewExam.arrayq.columnSum(arrayq.java:11) 

你的外部for循环条件给你带来了问题。 这是你的循环: –

 for (int i = 0; i < array[i].length; i++) 

现在,当i达到值3 ,您正在尝试访问array[3].length 。 这将抛出IndexOutOfBoundsexception。


由于每个内部数组的大小相同,您可以将循环更改为 : -

 for (int i = 0; i < array[0].length; i++) 

或者,更好的是,只需将array[0].length存储在某个变量中。 但这不会产生太大的影响。


我还建议你使用更好的方法来计算列的总和。 避免首先迭代行。 保持迭代是正常的,可能是这样的: -

 public double[] columnSum(double [][] array){ int size = array[0].length; // Replace it with the size of maximum length inner array double temp[] = new double[size]; for (int i = 0; i < array.length; i++){ for (int j = 0; j < array[i].length; j++){ temp[j] += array[i][j]; // Note that, I am adding to `temp[j]`. } } System.out.println(Arrays.toString(temp)); return temp; // Note you are not using this return value in the calling method } 

因此,您可以看到您的问题如何高度简化。 我所做的是,不是将值赋给数组,而是将array[i][j]的新值添加到temp[j]的现有值。 因此,逐渐地,所有i's (rows)array[i][j]的值在temp[j]求和。 这样您就不必使用混乱的迭代。 因此,只需将上述代码添加到您的方法中,然后删除旧代码即可。

即使您有jagged-array ,即内部数组的大小不同,此方法也可以正常工作。 但请记住要仔细定义temp数组的大小。

还要注意,我已经使用Arrays.toString(temp)方法来打印数组。

for(int i = 0; i

 for(int i=0;i 

isize永远不能在循环中改变

很近。 问题是你在for循环中使用array[i].length 。 我将它从array[i].length更改为array[0].length ,你的问题就消失了。 你需要j ,但实际上还没有它。

你可以做这样的事情,虽然如果你知道如何获得arrays,那就没有任何意义。 不同大小的列表仍会破坏计算总和的代码,但您也必须更改它。

 for (int i = 0, j = 0; i < initialArray[j].length; i++) { for (; j < initialArray.length; j++) { System.out.println(i + " " + j); } j = 0; } 

这是你修改过的程序。

 public class Main { static double[][] initialArray = { { 7.432, 8.541, 23.398, 3.981 }, { 721.859, 6.9211, 29.7505, 53.6483 }, { 87.901, 455.72, 91.567, 57.988 } }; public double[] columnSum(double[][] array) { int index = 0; double temp[] = new double[array[index].length]; for (int i = 0; i < array[0].length; i++) { double sum = 0; for (int j = 0; j < array.length; j++) { sum += array[j][i]; } temp[index] = sum; System.out.println("Index is: " + index + " Sum is: " + sum); index++; } return temp; } public static void main(String[] args) { new Main().columnSum(initialArray); } } 

对于index = 3,i也等于3并且你的代码中有array [i] .length,但是数组有3项,所以你在数组[3] .length表达式上得到Exception

试试吧

 public double[] columnSum(double [][] array){ double temp[] = new double[array[0].length]; for (int i = 0; i < array[0].length; i++){ double sum = 0; for (int j = 0; j < array.length; j++){ sum += array[j][i]; } temp[i] = sum; System.out.println("Index is: " + i + " Sum is: "+sum); } return temp; }