Sum of columns in a two-dimensional array

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

I want to get the sum of all columns, but I still get an outofbounds exception. This is the result I get:

 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) 
+1
source share
4 answers

Your external loop condition creates problems. Here is your loop: -

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

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


Since the size of each internal array is the same, you can change your loop to : -

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

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


I also suggest you use the best way to calculate the sum of the columns. Avoid repeating line by line first. Keep the iteration normal, 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 how your problem is very simplified. What I did, instead of assigning a value to an array, I added a new value to array[i][j] to the existing temp[j] value. So, gradually the value of array[i][j] for all i (rows) summed up in temp[j] . This way you do not need to use confusing iteration. So, just add the above code to your method and delete the old one.

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

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

+1
source

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

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

i and size should never change in a loop

0
source

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 disappeared. You need j , but you really don't have it.

You CAN do something like this, although there really is no point if you know how you are going to get your array. Lists of different sizes will still break the code to calculate the amount, although you will also have to change this.

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

for index = 3, I also equal 3, and you have an array [i] .length in your code, but the array has 3 elements, so you get an Exception in the expression of the array [3] .length

try

 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; } 
0
source

Source: https://habr.com/ru/post/1273211/


All Articles