【问题标题】:Adding the columns of a jagged 2D array添加锯齿状二维数组的列
【发布时间】:2014-04-21 08:59:40
【问题描述】:

我到处寻找一个好的答案,但我很惊讶我找不到一个完全完成我想做的事情的答案。我想创建一个在锯齿状二维数组中查找列总和的方法,无论大小、是否锯齿状等。这是我的代码:

public static int addColumn(int[][] arr, int x) {
    int columnSum = 0;
    int i = 0;
    int j = 0;
    int numRows = arr.length;
    //add column values to find sum

    while (i < numRows) {
        while (j < arr.length) {
            columnSum = columnSum + arr[i][x];
            j++;
            i++;
        }
    }//end while loop
    return columSum;
}

例如,假设我有以下数组:

int[][] arr = {{10, 12, 3}, {4, 5, 6, 8}, {7, 8}};

我希望能够将x 传递为2,并找到Column 3 的总和,即9。或将x 传递为3 以查找Column 4,即8。我的代码有缺陷,因为我现在拥有它,我已经尝试了大约一百种方法来使它工作。这是一个家庭作业,所以我正在寻求帮助来理解逻辑。当我运行该方法时,我当然会不断收到超出范围的异常。我想我现在想多了,因为我认为这不应该太复杂。谁能帮帮我?

【问题讨论】:

    标签: java arrays sum jagged-arrays


    【解决方案1】:

    我认为您的第二个 while 循环使您的总和太大。您应该只需要遍历行。

    while (i < numRows) {
        if (x < arr[i].length) {
            columnSum += arr[i][x];
        }
        ++i;
    }
    

    【讨论】:

      【解决方案2】:

      我看到了上面的代码。由于在此处发布了符合要求的代码,因此这些都不是足够的答案。由于时间不够,代码可能看起来没有排列或优化。谢谢.... :)

      package LearningJava;
      
      import java.util.Scanner;
      
      public class JaggedSum {
          public static void main(String[] args) {
              int ik = 0;
              int ij = 0;
              Scanner scan = new Scanner(System.in);
              int p = 0;
              int q = 0;
              int[][] arr = new int[2][];
              System.out.println("Enter the value of column in Row 0 ");
              p = scan.nextInt();
              System.out.println("Enter the value of column in Row 1 ");
              q = scan.nextInt();
              arr[0] = new int[p];
              arr[1] = new int[q];
              for (int i = 0; i < arr.length; i++) {
                  for (int j = 0; j < arr[i].length; j++) {
                      arr[i][j] = scan.nextInt();
                  }
              }
              for (int i = 0; i < arr.length; i++) {
                  for (int j = 0; j < arr[i].length; j++) {
                      System.out.print(arr[i][j] + " ");
                  }
                  System.out.println();
              }
              if (arr[1].length > arr[0].length) {
                  int[] columnSum = new int[arr[1].length];
                  int x;
                  for (x = 0; x < arr[0].length; x++) {
                      columnSum[x] = arr[0][x] + arr[1][x];
                      System.out.println(columnSum[x]);
                  }
                  ik = arr[0].length;
                  for (int j = ik; j < arr[1].length; j++) {
                      columnSum[j] = arr[1][j];
                      System.out.println(columnSum[j]);
                  }
              } else {
                  int[] columnSum = new int[arr[0].length];
                  int x;
                  for (x = 0; x < arr[1].length; x++) {
                      columnSum[x] = arr[0][x] + arr[1][x];
                      System.out.println(columnSum[x]);
                  }
                  ij = arr[1].length;
                  for (int j = ij; j < arr[0].length; j++) {
                      columnSum[j] = arr[0][j];
                      System.out.println(columnSum[j]);
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        Java 8 中,您可以为此目的使用 IntStream

        public static int addColumn(int[][] arr, int x) {
            // negative column index is passed
            if (x < 0) return 0;
            // iteration over the rows of a 2d array
            return Arrays.stream(arr)
                    // value in the column, if exists, otherwise 0
                    .mapToInt(row -> x < row.length ? row[x] : 0)
                    // sum of the values in the column
                    .sum();
        }
        
        public static void main(String[] args) {
            int[][] arr = {{10, 12, 3}, {4, 5, 6, 8}, {7, 8}};
            System.out.println(addColumn(arr, 2)); // 9
            System.out.println(addColumn(arr, 3)); // 8
            System.out.println(addColumn(arr, 4)); // 0
            System.out.println(addColumn(arr, -1));// 0
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-03-16
          • 2021-05-13
          • 2016-07-09
          • 2015-10-18
          • 2011-02-04
          • 2014-12-26
          • 2014-05-16
          相关资源
          最近更新 更多