简体   繁体   English

如果行和列不相等,如何对列求和?

[英]How do I sum a column if the rows and columns are not equal?

so I've gone on a few other summing columns in 2d arrays, but all of them have equal lengths whereas my does not.所以我在二维数组中进行了其他一些求和列,但它们的长度都相等,而我的则没有。 I have summed the rows (pretty straight forward), however, when I try to sum the column, it stops because there's not a number in the first row.我已经对行求和(非常直接),但是,当我尝试对列求和时,它停止了,因为第一行中没有数字。 How do I make it so it continues?我该如何让它继续下去? I have included what the output is.我已经包括了输出是什么。

 import java.util.*;
 import java.io.*;



 public class Main
   {
      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} };

       // declare the sum
       int sum;
       int sum2 = 0;

       // compute the sums for each row
       for ( int row=0; row < data.length; row++)
       {
       // initialize the sum, i did it to zero
       sum = 0;


       // compute the sum for this row
       for ( int col=0; col < data[row].length; col++) 
       {
           sum+=data[row][col];
       }

       // write the sum for this row
       System.out.println("The sum of this row is: " + sum);
   }

       for(int i = 0; i < data[0].length; i++)
        {  
          sum2= 0;  
          for(int j = 0; j < data.length; j++)
          {  
            sum2 = sum2 + data[j][i];  
          }   
          System.out.println("Sum of " + (i+1) +" column: " + sum2);  
        }
   }
}





/*Sample Output:
The sum of this row is: 10
The sum of this row is: 30
The sum of this row is: 12
The sum of this row is: 2
Sum of 1 column: 13
Sum of 2 column: 9
Sum of 3 column: 15
*/

Thank you to everyone!谢谢大家!

Try this.尝试这个。 The trick is to fill in the empty columns with zeroes as placeholders:诀窍是用零作为占位符填充空列:

int[][] data = { {3, 2, 5},
                 {1, 4, 4, 8, 13},
                 {9, 1, 0, 2},
                 {0, 2, 6, 3, -1, -8} };

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++) {
       sums[col] += data[currentRow][col];
   } 
}   
// print sums
for (int i = 0; i < sums.length; i++) {
    System.out.println("Sum of column " + (i+1) + ": " + sums[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用随机整数[1,9]填充2D数组,并打印出2D数组,其中每行的行总和和每列的列总和? - How do I fill a 2D array with random integers [1,9] and print out a 2D array with sums of rows at each row and sum of columns at each column? 如何有效地对具有相同顶行值的列求和 - How do I sum columns with the same top row value efficiently 如何在python中打印列和行的总和? - How to print the sum of columns and rows in python? 基于两列合并数组行并对另一列求和 - Merge array rows based on two columns and sum another column 按 3 列对数据行进行分组,并对各个组中的另一列求和 - Group rows of data by 3 columns and sum another column within respective groups 如何向NUMPY数组添加行和列? - How do I add rows and columns to a NUMPY array? 如何在2D数组中切换行和列? - How do I switch rows and columns in a 2D array? 如何迭代多维数组的行和列? - How do I iterate rows and columns of a multidimensional array? 如何将行转置为值数组中的列? - How do I transpose rows to columns in a array of values? 我如何找到多维数组的总和,从而使用户在多维数组中输入行和列 - how can i find the sum of multidimensional array which makes the user input the rows and columns in a multidimensional array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM