【问题标题】:Traversing 2d array row first and then column first先遍历二维数组行,然后先遍历列
【发布时间】:2014-01-29 18:32:55
【问题描述】:

我正在寻找一种方法来遍历 2d n x m int 数组 (int[col][row]) 第一行(简单部分)然后逐列,在 Java 中。 这是逐行执行的代码,有没有办法逐列执行col?

for(int i = 0; i < display.length; i++){
            for (int j = 0; j < display[i].length; j++){
                if (display[i][j] == 1)
                    display[i][j] = w++;
                else w = 0;
            }
        }

【问题讨论】:

  • 可变宽度列?
  • 如果在循环内将 display[i][j] 更改为 display[j][i] 会怎样?
  • 非可变宽度列。如果你改变它,因为它是 n x m 网格,它会改变方向,这是不可取的
  • 如果以下答案之一对您有用,请接受。

标签: java arrays 2d


【解决方案1】:

由于您使用二维数组作为矩阵,我们可以假设每行的长度在整个矩阵中都是相同的(即每行的列数相同)。

//So, you can treat display[0].length as the number of columns.

for(int col=0; col<display[0].length; col++)
{
   for(int row=0; row<display.length; row++)
   {
      //your code to access display[row][col]
   }
}

希望这会有所帮助!

【讨论】:

  • 这似乎有道理。谢谢!
  • 是的,正如 hexafraction 提到的,Java 不会将多维数组视为矩形。只有当您可以安全地假设您的数组一个矩阵时,才能使用上面的代码。
【解决方案2】:

如果该行有那么多列,这是一种按列打印的方法。

String[][] twoDArray = new String[][] {
        new String[] {"Row1Col1", "Row1Col2", "Row1Col3"},
        new String[] {"Row2Col1", "Row2Col2"},
        new String[] {"Row3Col1", "Row3Col2", "Row3Col3", "Row3Col4"}
};

boolean recordFound = true;
int colIndex = 0;
while(recordFound) {
    recordFound = false;
    for(int row=0; row<twoDArray.length; row++) {
        String[] rowArray = twoDArray[row];
        if(colIndex < rowArray.length) {
            System.out.println(rowArray[colIndex]);
            recordFound = true;
        }
    }
    colIndex++;
}

输出是:

Row1Col1
Row2Col1
Row3Col1
Row1Col2
Row2Col2
Row3Col2
Row1Col3
Row3Col3
Row3Col4

【讨论】:

    【解决方案3】:

    这不是一件很“自然”的事情,因为 Java 数组是嵌套的,而不是矩形多维数组。例如,以下是可能的:

    [ ][ ][ ]
    [ ][ ]
    [ ][ ][ ][ ][ ]
    

    [ ] 是一个元素。

    垂直遍历这不是一个非常“自然”或有效的操作。但是,您可以通过将列遍历到数组长度的最大值来做到这一点,通过显式检查避免数组越界问题或(更邪恶)静默丢弃 ArrayOutOfBounds 异常。

    编辑:在矩形情况下,只需切换两个循环。您使用哪一行的长度无关紧要。

    【讨论】:

    • 我明白你在说什么,但在这种特殊情况下,我正在处理一个矩形二维数组 :)
    • 亚历克斯。如果它是已知长度,那么只需反转你的 for 循环。您始终可以根据第一列的长度来执行此操作。请参阅 garyF 的回答。我的回答处理十六进制所指的情况。
    【解决方案4】:
    static void columnFirst(List<List<Integer>> matrix) {
        int max = 0;
        for (int i = 0; i < matrix.size(); i++) {
            max = Math.max(max, matrix.get(i).size());
        }
    
    
        for (int i = 0; i < max; i++) {
            for (int j = 0; j < matrix.size(); j++) {
                if (matrix.get(j).size() > i)
                    System.out.print(matrix.get(j).get(i) + " ");
            }
            System.out.println();
        }
    }
    

    输入数组

    {{1, 2, 3, 4, 5, 6},
     {1, 2, 3},
     {1, 2, 3, 4, 5},
     {1},
     {1, 2, 3, 4, 5, 6, 7, 8, 9}}
    

    输出:

    1 1 1 1 1 
    2 2 2 2 
    3 3 3 3 
    4 4 4 
    5 5 5 
    6 6 
    7 
    8 
    9 
    

    【讨论】:

      【解决方案5】:
      /* 
      Assume that the length of the col[0] will be the same for all cols. 
      Notice: that we are access salaries[j] for each iteration of j while
      [i] same the same. 
      */
      
      public void printColsValues() {
          for(int i = 0; i < array[0].length; i++) {
              for(int j = 0; j < array.length; j ++) {
                  System.out.println(arr[j][i]);
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-01-15
        • 2016-09-04
        • 2013-04-17
        • 1970-01-01
        • 2013-03-14
        相关资源
        最近更新 更多