Reputation: 41
I am writing some code to calculate the total of each row in the array.
public static int sum(int[][] array) {
int total = 0;
for (int i = 0; i < array.length; i++) {
for (int k = 0; k < array[i].length; k++) {
total = total + array[i][k];
}
}
return total;
}
The code above is to work out the total sum for all the numbers in the two dimensional array however I am trying to work out to the total for each individual row for the array.
public static int[] rowSums(int[][] array) {
}
I am very unsure on what to do for the code to work out the 2D array for each row.
Upvotes: 3
Views: 19504
Reputation: 12937
If you are using Java 8 you can do it in the following way:
int[] ar = Arrays.stream(new int[][]{{1,2},{3,5}}) // source
.mapToInt(arr -> IntStream.of(arr).sum()) // sum inner array
.toArray(); // back to int[]
System.out.println(Arrays.toString(ar)); // print the array
Without stream:
int[][] arr = {{1,2},{3,5}};
// create a array that stores the total of each inner array
int[] total = new int[arr.length];
// first loop for outer array
for (int i = 0; i < arr.length; i++) {
// loop for inner array
for (int k = 0; k < arr[i].length; k++) {
// since default value of total[i] is 0, we can directly use +=
total[i] += arr[i][k];
}
}
// print the array
System.out.println(Arrays.toString(total));
Upvotes: 4
Reputation: 1384
Based on @YFC_L answer, but using the enhanced loop:
public static int[] sum2(int[][] array) {
//create an array of size based of how many rows the array has
int[] result = new int[array.length];
int rowIndex = 0;
//Loop over each row
for (int[] row : array) {
int total = 0;
//Calculate the sum of the row
for (int n : row) {
total += n;
}
//Store the sum in the result
result[rowIndex++] = total;
}
return result;
}
Also, this method can be tested in the exatelly same way:
public static void main(String[] args) {
System.out.println(
Arrays.toString(sum2(new int[][]{{1, 1, 1}, {2, 2}, {3, 3}}))
);
}
and, of course the output is the same:
[3, 4, 6]
Upvotes: 3
Reputation: 611
public static List<Integer> sum(int[][] array) {
List<Integer> total = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
int sum = 0;
for (int k = 0; k < array[i].length; k++) {
sum = sum + array[i][k];
}
total.add(sum);
}
return total;
}
In case if you need to use int[] after getting result,
List<Integer> total = sum(ar);
Integer[] result = total.toArray(new Integer[0]);
Use result object for further use.
Upvotes: 1
Reputation: 4520
Try this with slight modification with your code
public static int[] sum(int[][] array) {
List<Integer> rowTotal = new ArrayList<>();
int total = 0;
for (int i = 0; i < array.length; i++) {
for (int k = 0; k < array[i].length; k++) {
total = total + array[i][k];
}
rowTotal.add(total);
total = 0;
}
return rowTotal.stream().mapToInt(i->i).toArray();
}
Upvotes: 1
Reputation: 59978
Did you mean something like this :
public static int[] sum(int[][] array) {
//create an array of size array.length
int[] result = new int[array.length];
int total;
//Loop over the first dimension
for (int i = 0; i < array.length; i++) {
total = 0;//Make sure to re-initialize the total in each iteration
//For each row calculate the sum and store it in total
for (int k = 0; k < array[i].length; k++) {
total += array[i][k];
}
//When you finish put the result of each row in result[i]
result[i] = total;
}
return result;
}
Example
System.out.println(
Arrays.toString(sum(new int[][]{{1, 1, 1}, {2, 2}, {3, 3}}))
);
Outputs
[3, 4, 6]
Upvotes: 5