sum of columns in a 2 dimensional array

旧时模样 提交于 2019-11-29 08:05:42

Your outer for loop condition is giving you problems. Here's your loop: -

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

Now, when i reaches the value 3, you are trying to access array[3].length. This will throw you IndexOutOfBounds exception.


Since the size of every internal arrays are same, you can change your loop to: -

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

Or, even better, just store the array[0].length in some variable before hand. But that will not make much of a difference.


I would also suggest you to use a better way to calculate the sum of columns. Avoid iterating over rows first. Keep the iteration a normal one probably like this: -

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
}

So, you can see that how your problem is highly simplified. What I did is, rather than assigning the value to the array, I added the new value of array[i][j] to the existing value of temp[j]. So, gradually, the value of array[i][j] for all i's (rows) gets summed up in temp[j]. This way you don't have to use confusing iteration. So, just add the above code to your method, and remove the old one.

This method will also work fine, even if you have jagged-array, i.e., you inner arrays are not of same size. But just remember to define the size of temp array carefully.

Also note that, I have used Arrays.toString(temp) method to print the array.

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

for(int i=0;i<size;i++)

i & size must never be change in the loop

So close. The problem is that you are using array[i].length in your for loop. I changed it from array[i].length to array[0].length and your problem is gone. You need j there but you don't actually HAVE it yet.

You COULD do something like this although there isn't really any point if you know how you are going to get your array. Differently sized lists still would break the code for calculating sum though, you'd have to change that as well.

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

And here is your modified program.

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);
  }
}

for index = 3, i is also equal with 3 and you have array[i].length in your code, but array have 3 item so you get Exception on array[3].length expression

try it

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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!