簡體   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