我正在寻找一种方法来遍历 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;
}
}
Run Code Online (Sandbox Code Playgroud)
由于您使用二维数组作为矩阵,我们可以假设整个矩阵中每行的长度相同(即每行的列数相同)。
//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]
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!