【问题标题】:2D Array(Ragged) - Print Each Column Sum2D Array(Ragged) - 打印每列总和
【发布时间】:2015-04-12 21:42:44
【问题描述】:

我正在尝试计算二维数组的按列和。

对于这个二维数组:

int[][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

打印每一列的总和没有问题。

这是我的代码

int total;
for (int col = 0; col < array[0].length; col++)
{
  total = 0;
  for (int row = 0; row < array.length; row++)
    total += array[row][col];
  System.out.println("Column " + col + " total: " + total);
}

但是,对于这个参差不齐的二维数组:

int[][] array = {{1,2},{5,6,7},{9,10,11,12}};

如果没有 outofboundsexception 错误,我似乎无法打印最后两列。我们的教授并没有真正教我们 try 和 catch 语句,所以我假设必须进行某种小的调整。但是,我已经篡改了上面的代码来打印最后两列但没有运气......

有什么想法吗?

【问题讨论】:

    标签: java arrays for-loop multidimensional-array indexoutofboundsexception


    【解决方案1】:

    下面是程序的正确代码:: 我从上面的帮助材料中学到了,但是由于代码是零碎的,特别是最大值取最大值(如果我们需要更少的最大值)下一个计数器然后这个程序失败了)

    package arrayPractice;
    
    public class SumColumArray {
        public static void main (String [] args){
            int [][] matrix = new int [5][];
                matrix[0] = new int[2];
                matrix[1] = new int[3];
                matrix[2] = new int[4];
                matrix[3] = new int[2];
                matrix[4] = new int[1];
    
    
        int total = 0;
        int max = 0;
        for(int row = 0; row < matrix.length; row++){
        max = matrix[row].length; // Variable Length of Column Accessing 
        System.out.println(max);
        for(int column = 0; column < max; column++){
        total = 0;
        matrix[row][column] = (int)(Math.random() * 100);
        if(column < matrix[row].length);
        total += matrix[row][column];
        System.out.println("Column " + column + " total: " + total);
    
        }
         }
       }
    }
    

    【讨论】:

      【解决方案2】:

      您的教授可能不接受这一点,因为他(还)没有教过您,但最优雅的解决方案是让 Java 自己做出低级循环决策。您可以使用for-each loop

      int total = 0;
      int iterator = 0; // this variable is only necessary if you need to know which row you're in
      for (int[] row : array) {
          int sum = 0;
          for (int item : row) {
              sum += item;
          }
          System.out.println("Column " + iterator + " total: " + sum);
          total += sum;
          iterator++;
      }
      System.out.println(total);
      

      它的工作方式是指定一个数组及其元素的类型。所以int[][]int[] 的数组,这就是你指定for (int[] row : array) 的原因。您可以将其理解为“对于这个二维array[][] 中的每个一维row[]”。在循环中,您可以在 row[] 的每个 int 上嵌套另一个循环。

      【讨论】:

        【解决方案3】:

        试试这个:

        int total;
        int max = //This is the max number of column one row can have 
        for (int col = 0; col < max; col++)
        {
          total = 0;
          for (int row = 0; row < array.length; row++)
            if(col < array[row].length)//Check for row length here.
               total += array[row][col];
          System.out.println("Column " + col + " total: " + total);
        }
        

        基本上,在访问其元素之前,您需要先检查行的长度。

        求最大值:

        int max = 0;
        for(int i = 0; i < array.length; i++)
            max = Math.max(array[i].length, max);
        

        【讨论】:

        • 有效!谢谢!但是max只能是固定值?不能是array.length之类的值吗?
        • @user3718591 看看我更新的答案,你需要遍历所有行,找到列的最大值:)
        【解决方案4】:

        您不需要捕获任何异常。首先,您应该找出二维数组中最长的行(您需要一个初步循环)。

        假设它是 x。然后在外循环中从 0 迭代到 x-1,在内循环中,在访问 array[row][col] 之前,确保 array[row].length &gt; col

        【讨论】:

        • 感谢您的解释。现在更有意义了。
        猜你喜欢
        • 2018-12-23
        • 2015-05-28
        • 2019-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多