0

The task is to return the sum of each row of a 2D array in a 1D array.

Example

int[][] values = {  
                     {1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, 
                     {3, 4, 5, 1, 2}, {4, 5, 1, 2, 3}, 
                     {5, 1, 2, 3, 4}
                 };

I want to print: Row sums: 15 15 15 15 15

Code

public int[]allRowSums()
    {
        int rowSum = 0;
        int current = 0;
        int[]a = null;
        for(int i = 0; i < values.length; i++)
        {
            a = new int[values[i].length];
            for(int j = 0; j < values[current].length; j++)
            {
                rowSum = values[current][j];
                a[i] += rowSum;
            }
            rowSum = 0;
            current++;            
        }
        return a;        
    }

when I call this method I get: Row sums: [I@15db9742

Philosophist
  • 103
  • 1
  • 13
Roy
  • 39
  • 4

3 Answers3

0

You are returning the reference to the array and not the sum. Also in each iteration you are creating a new array, due to which the previous values are lost.

Simpy move the initialization outside the loop:

public static int[] allRowSums() {
    int rowSum = 0;
    int current = 0;
    int[] a = new int[values.length];        // observe this change (only 1 change made)
    for (int i = 0; i < values.length; i++) {
        for (int j = 0; j < values[current].length; j++) {
            rowSum = values[current][j];
            a[i] += rowSum;
        }
        rowSum = 0;
        current++;
    }
    return a;
}

And as mentioned in the comments, you are printing the reference of the array, use Arrays.toString() to print out the array.

System.out.println(Arrays.toString(allRowSums()));
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • This is my point, how can I return the array and not the reference? – Roy Mar 22 '19 at 17:20
  • Returning the reference to the array is fine, but when you try to print it out what you get is in fact the *memory address* of the reference. Either use Arrays.toString or you can *manually* iterate over the array and print it out. – Nicholas K Mar 22 '19 at 17:22
0

To iterate the result of the sum, you should use a loop to iterate all the elements of the array.

Try something like this

for(int intVar :  a)
    System.out.println(intVar);
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
John Doe
  • 2,752
  • 5
  • 40
  • 58
0

Code fix

The current implementation returns somehow 0, 0, 0, 0, 15 if printed correctly converting the array to String first:

System.out.println(Arrays.toString(array));

It happens because you always refresh all the values in the first (outer) for-loop. Do the following

int rowSum = 0;
int current = 0;
int[] a = new int[values.length];                   // proper initialization to avoid NPE
for (int i=0; i<values.length; i++) {
                                                    // this line removed
    for (int j=0; j<values[current].length; j++) {
        rowSum = values[current][j];
        a[i] += rowSum;
    }
    rowSum = 0;
    current++;
}
return a;

Now the code works correctly and will contain 15, 15, 15, 15, 15 on your sample.

Better solution

Stream API is suitable for this use case. A proper combination of collector Collectors::summingInt and mapping does the trick

int[] array = Arrays.stream(values)
                    .map(i -> Arrays.stream(i)
                                    .boxed()
                                    .collect(Collectors.summingInt(j -> j)))
                    .mapToInt(i -> i)
                    .toArray();

Printing this array to ex. a console will produce the very same result as above.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183