Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 先遍历二维数组行,然后再遍历列_Java_Arrays_2d - Fatal编程技术网

Java 先遍历二维数组行,然后再遍历列

Java 先遍历二维数组行,然后再遍历列,java,arrays,2d,Java,Arrays,2d,在Java中,我正在寻找一种方法来遍历一个2d的n×m int数组(int[col][row]),首先逐行(简单的部分),然后逐列遍历。 这是逐行执行的代码,有没有一种方法可以逐列执行 for(int i = 0; i < display.length; i++){ for (int j = 0; j < display[i].length; j++){ if (display[i][j] == 1)

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

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;
            }
        }
for(int i=0;i
这不是一件很“自然”的事情,因为Java数组是嵌套的,而不是矩形多维的。例如,以下是可能的:

[ ][ ][ ]
[ ][ ]
[ ][ ][ ][ ][ ]
其中,
[]
是一个元素

垂直穿越这一区域不是一个非常“自然”或高效的操作。但是,您可以通过遍历最大数组长度的列来做到这一点,通过显式检查避免数组越界问题,或者(更糟糕的是)静默地删除
ArrayOutOfBounds
异常


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

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

//因此,可以将display[0].length视为列数。

对于(int col=0;col,这里有一种方法,如果行有那么多列,它将按列打印

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++;
}
static void columns first(列表矩阵){
int max=0;
对于(int i=0;ii)
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 
/*
假设所有列的列[0]长度相同。
注意:我们正在为j的每次迭代访问[j],而
[i] 一模一样。
*/
public void printColsValues(){
对于(int i=0;i
可变宽度列?如果将显示[i][j]更改为显示[j][i],该怎么办在循环内部?不是可变宽度的collumns。如果你改变它,因为它是n乘m的网格,它会改变方向,这是不可取的。如果下面的答案之一对你有效,请接受它。这似乎是有意义的。谢谢!是的,正如Hexafrance提到的,Java不会将多维数组视为矩形。上面的代码只有当你可以安全地假设你的数组是一个矩阵时,才能使用。我明白你的意思,但在这个特定的例子中,我处理的是一个矩形2d数组:)Alex。如果它是一个已知的长度,那么只需反转for循环即可。您始终可以根据第一列的长度执行此操作。见加里夫的答案。我的回答处理hex所指的情况。
{{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 
/* 
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]);
        }
    }
}