Java 先遍历二维数组行,然后先遍历列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21439619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Traversing 2d array row first and then column first
提问by Alex Kornhauser
I am looking for a way to traverse a 2d n by m int array (int[col][row]) first row by row (simple part) and then column by column, in Java. Here is the code for doing row by row, is there a way to do col by col?
我正在寻找一种方法来遍历 2d n by 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;
}
}
采纳答案by JustinKSU
Here is one approach that will print by column if the row has that many columns column.
如果行有那么多列,这是一种按列打印的方法。
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++;
}
Output is:
输出是:
Row1Col1
Row2Col1
Row3Col1
Row1Col2
Row2Col2
Row3Col2
Row1Col3
Row3Col3
Row3Col4
回答by nanofarad
It's not a very "natural" thing to happen, due to the way Java arrays are nested and not rectangular multidimensional. For example, the following is possible:
由于 Java 数组的嵌套方式而不是矩形多维数组,这不是一件非常“自然”的事情。例如,以下是可能的:
[ ][ ][ ]
[ ][ ]
[ ][ ][ ][ ][ ]
where [ ]
is an element.
哪里[ ]
是一个元素。
Traversing this vertically is not a very "natural" or efficient operation. You could, however, do so by traversing columns up to the maximum of the array lengths, avoiding array out of bound issues with explicit checks or (being more evil) silently dropping ArrayOutOfBounds
exceptions.
垂直遍历这不是一个非常“自然”或有效的操作。但是,您可以通过遍历列到数组长度的最大值来实现,通过显式检查避免数组越界问题或(更邪恶)静默丢弃ArrayOutOfBounds
异常。
Edit: In the rectangular case just switch the two loops. It will not matter which row's length you use.
编辑:在矩形情况下,只需切换两个循环。您使用哪一行的长度并不重要。
回答by garyF
Since you used a two dimensional array as a matrix, we can assume that the length of each row is the same throughout the matrix (i.e. number of columns of each row is the same).
由于您使用二维数组作为矩阵,我们可以假设整个矩阵中每行的长度相同(即每行的列数相同)。
//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]
}
}
Hope this helps!
希望这可以帮助!
回答by Abhishek
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();
}
}
Input array
输入数组
{{1, 2, 3, 4, 5, 6},
{1, 2, 3},
{1, 2, 3, 4, 5},
{1},
{1, 2, 3, 4, 5, 6, 7, 8, 9}}
Output:
输出:
1 1 1 1 1
2 2 2 2
3 3 3 3
4 4 4
5 5 5
6 6
7
8
9