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