I've been teaching myself Java through some free online tutorials and have challenged myself to work through just about all of the practice exercises. I've been stuck on this one for a week now and it's driving me crazy. I feel I am fairly close, just tripped up by the varying array lengths that make up the uneven columns.
public class Testing {
public static void main(String[] args) {
int[][] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
int sum = 0;
int row = 0;
int col = 0;
int currentRow = 0;
while (currentRow < data.length) {
for (col = 0; col < data[currentRow].length; col++) {
sum = 0;
for (row = 0; row < data.length; row++) {
System.out.println(data[row][col]);
sum += data[row][col];
}
System.out.println("Sum: " + sum);
}
currentRow++;
}
}
}
it seems as if there is one loop too much. Your while
walks through the first dimension and for(row...
the second dimension. So there is no need to walk through data again by for(row...
try it with one loop less like:
public class Testing {
public static void main(String[] args) {
int[][] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
int sum = 0;
for (int currentRow = 0; currentRow < data.length; currentRow++) {
sum = 0;
for (int col = 0; col < data[currentRow].length; col++) {
System.out.println(data[currentRow][col]);
sum += data[currentRow][col];
}
System.out.println("Sum: " + sum);
}
}
}
public class Testing {
public static void main(String[] args) {
int[][] data = { {3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8} };
// get max length of a row == number of columns
int length = 0;
for (int r = 0; r < data.length; r++) {
int currLength = data[r].length;
if(currLength>length) length = currLength;
}
// create array for column sums
int[] sums = new int[length];
// fill array with zeros
Arrays.fill(sums, 0);
// sum up
for (int currentRow = 0; currentRow < data.length; currentRow++) {
for (int col = 0; col < data[currentRow].length; col++) {
System.out.println(data[currentRow][col]);
sums[col] += data[currentRow][col];
}
}
// print sums
for (int i = 0; i < sums.length; i++) {
System.out.println(i + ": " + sums[i]);
}
}
}
If im understanding your question right, and you want the sum of the first, second, third, etc. columns then you could try the following.
// array for the sums of the columns
int[] colSums;
int largestSize = 0;
// loop through and find the largest array of values (in this example its the 4th)
for(int x = 0; x < data.length; x++) {
largestSize = Math.max(largestSize,data[x].length);
}
colSums = new int[largestSize]; // create the int array with the right length
// loop through the data array and add sums to the colSums array
for(int x = 0; x < data.length; x++) {
colSums[x] = 0; // so that its not null when we add to it
for(int y = 0; y < data[x].length; y++) {
// here I add the number to the solSums array
colSums[y] += data[x][y];
}
}
// now colSums[0] will be the sum of the first column, colSums[1] = sum of second, etc...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.