繁体   English   中英

您将如何通过向下遍历列来遍历二维数组? 我想要这个用于参差不齐的数组,但首先在常规数组中查看可能会起作用

[英]How would you iterate through a 2D array by going down through columns? I want this for a ragged array but seeing in a regular array first might work

我希望我的数组像这样从一个索引到另一个索引,例如:(0,0)、(1,0)、(2,0) 等。我已经尝试过看起来应该是正确的方法,但是我的循环在第一列之后停止,我得到一个索引越界异常。

这是我所做的:

int[][] array2d = 
            {
                {4,5, 3,8},
                {8,3,99,6},
                {5,7, 9,1}
                
            }; 
int currentRow = 0;
        for (int currentColumn = 0; currentColumn < (array2d[currentRow].length); currentColumn++) 
        { 
            for(currentRow = 0; currentRow < array2d.length; currentRow++) 
            {
                System.out.println(array2d[currentRow][currentColumn]);
            }
        }

首先,让我们解决代码中的错误。 考虑这个循环:

int i;
for (i = 0; i < 10; i++) { /* loop body */ }

循环一直运行直到i < 10的计算结果为false 在此示例中,当循环退出时, i的值为10

考虑代码中的内部循环:

 for (currentRow = 0; currentRow < array2d.length; currentRow++) 

当此循环退出时, currentRow将等于array2d.length 然后,控制将返回到外循环。 当它这样做时,在控制表达式currentColumn < (array2d[currentRow].length)中,这部分array2d[currentRow]将抛出IndexOutOfBoundsException

如您所知,按列顺序逐行遍历二维数组的标准方法如下:

  for (int row = 0; row < array2d.length; row++) { 
      for (int column = 0; column < array2d[row].length; column++ { 
        // things to do in inner loop
      }

如果要按列顺序处理矩形二维数组,代码可能如下所示:

 for (int column = 0; column < array2d[0].length; column++) { 
    for (int row = 0; row < array2d.length; row++) {
       // loop body
    }
 }

但是,如果数组参差不齐怎么办?

首先在开头找到列数最多的行:

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

然后,您的代码可能如下所示:

  for (int column = 0; column < maxColumns; column++) { 
     for (int row = 0; row < array2d.length; row++) { 
        if (column < array2d[row].length) {
           // do something with array2d [row][column]
        } else { 
           // do something for the case where 
           // array2d [row] doesn't have a value in "column" 
        }
     }
  }

另一种方法可能是尝试捕获ArrayIndexOutOfBoundsException 然而,这可能被认为是一种糟糕的编程习惯。

        int[][] array2d =
                {
                    {1,2,3,4},
                    {5,6,7,8},
                    { 9,10,11,12}
                };
        int r=array2d.length,c=array2d[0].length;
        for (int i=0;i<c;i++)
        {
            for(int j=0;j<r;j++)
            {
                System.out.print(array2d[j][i]+" ");
            }
            System.out.println();
        }

您已经在行之前运行了列,因为它是一个长度为 3x4 的二维数组,它在遍历数组时给出了 indexOutOfBoundsException。

您需要在打印前获得最大列数。

public static void main(String[] args) {
    int[][] array2d = {
        {4, 5, 3, 8},
        {},
        {8, 6},
        {2},
        {5, 7, 9, 1, 0}
    };
    int maxRow = array2d.length;
    int maxColumn = array2d[0].length;
    for (int r = 1; r < maxRow; ++r)
        maxColumn = Math.max(maxColumn, array2d[r].length);
    for (int c = 0; c < maxColumn; ++c) {
        for (int r = 0; r < maxRow; ++r)
            if (c < array2d[r].length)
                System.out.print(array2d[r][c] + " ");
            else
                System.out.print("* ");
        System.out.println();
    }
}

output:

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

或者您可以通过再次扫描该行来摆脱它。

public static void main(String[] args) {
    int[][] array2d = {
        {4, 5, 3, 8},
        {},
        {8, 6},
        {2},
        {5, 7, 9, 1, 0}
    };
    int rowSie = array2d.length;
    for (int c = 0; true; ++c) {
        StringBuilder sb = new StringBuilder();
        boolean out = false;
        for (int r = 0; r < rowSie; ++r)
            if (c < array2d[r].length) {
                sb.append(array2d[r][c]).append(" ");
                out = true;
            } else
                sb.append("* ");
        if (!out)
            break;
        System.out.println(sb);
    }
}

output:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM