Java 遍历二维数组

Java 遍历二维数组,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个“连接四块板”,我用一个2d阵列(阵列[x][y]x=x坐标,y=y坐标)模拟它。我必须使用“System.out.println”,所以我必须遍历行 我需要一种迭代方式[0,0][1,0][2,0][3,0][0,1][1,1][2,1]等 如果我使用正常程序: for (int i = 0; i<array.length; i++){ for (int j = 0; j<array[i].length; j++){ string += array

我有一个“连接四块板”,我用一个2d阵列(阵列[x][y]x=x坐标,y=y坐标)模拟它。我必须使用“System.out.println”,所以我必须遍历行

我需要一种迭代方式[0,0][1,0][2,0][3,0][0,1][1,1][2,1]等

如果我使用正常程序:

for (int i = 0; i<array.length; i++){
     for (int j = 0; j<array[i].length; j++){
        string += array[i][j];
     } System.out.println(string)

}

for(int i=0;i简单的想法:获取最长行的长度,迭代每一列,打印行的内容(如果行中有元素)。下面的代码可能会有一些错误,因为它是在简单的文本编辑器中编码的

  int longestRow = 0;
  for (int i = 0; i < array.length; i++) {
    if (array[i].length > longestRow) {
      longestRow = array[i].length;
    }
  }

  for (int j = 0; j < longestRow; j++) {
    for (int i = 0; i < array.length; i++) {
      if(array[i].length > j) {
        System.out.println(array[i][j]);
      }
    }
  }
int longestRow=0;
for(int i=0;ilongestRow){
longestRow=数组[i]。长度;
}
}
对于(int j=0;jj){
System.out.println(数组[i][j]);
}
}
}

只需颠倒索引的顺序,如下所示:


for(int j=0;j将其视为一个数组数组,这肯定会起作用

int mat[][] = { {10, 20, 30, 40, 50, 60, 70, 80, 90},
                {15, 25, 35, 45},
                {27, 29, 37, 48},
                {32, 33, 39, 50, 51, 89},
              };


    for(int i=0; i<mat.length; i++) {
        for(int j=0; j<mat[i].length; j++) {
            System.out.println("Values at arr["+i+"]["+j+"] is "+mat[i][j]);
        }
    }
int mat[][]={{10,20,30,40,50,60,70,80,90},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50, 51, 89},
};

对于(int i=0;i只需更改循环中的索引.i和j…,另外,如果处理字符串,则必须使用concat并将变量初始化为空强,否则将出现异常

String string="";
for (int i = 0; i<array.length; i++){
    for (int j = 0; j<array[i].length; j++){
        string = string.concat(array[j][i]);
    } 
}
System.out.println(string)
String=”“;
for(inti=0;i
//这是我能想象到的最简单的方法。
//您只需要更改列和行的顺序,您的是打印列X行,解决方案是打印它们行X列

对于(int rows=0;rows这些函数应该可以工作

// First, cache your array dimensions so you don't have to
//  access them during each iteration of your for loops.
int rowLength = array.length,       // array width (# of columns)
    colLength = array[0].length;    // array height (# of rows)

//     This is your function:
// Prints array elements row by row
var rowString = "";
for(int x = 0; x < rowLength; x++){     // x is the column's index
    for(int y = 0; y < colLength; y++){ // y is the row's index
        rowString += array[x][y];
    } System.out.println(rowString)
}

//      This is the one you want:
// Prints array elements column by column
var colString = "";
for(int y = 0; y < colLength; y++){      // y is the row's index
    for(int x = 0; x < rowLength; x++){  // x is the column's index
        colString += array[x][y];
    } System.out.println(colString)
}
//首先,缓存数组维度,这样就不必
//在for循环的每次迭代期间访问它们。
int rowLength=array.length,//数组宽度(#个列)
colLength=数组[0]。长度;//数组高度(#行)
//这是您的功能:
//逐行打印数组元素
var rowString=“”;
for(intx=0;x
在第一个块中,内部循环在移动到下一列之前对行中的每个项进行迭代。

在第二个块(您想要的块)中,内部循环在移动到下一行之前遍历所有列。

tl;dr:
本质上,两个函数中的
for()
循环都是切换的。就是这样。

我希望这能帮助您理解迭代二维数组背后的逻辑。


此外,无论是字符串[,]还是字符串[][]

行的长度是否不同?然后使用大小为y*x的数组,这将更符合逻辑…使用数组[j][i]。
因为所有行的列数都相同,这可能不是真的。如果他使用
新int[]{1,2,3},{4,5};
// First, cache your array dimensions so you don't have to
//  access them during each iteration of your for loops.
int rowLength = array.length,       // array width (# of columns)
    colLength = array[0].length;    // array height (# of rows)

//     This is your function:
// Prints array elements row by row
var rowString = "";
for(int x = 0; x < rowLength; x++){     // x is the column's index
    for(int y = 0; y < colLength; y++){ // y is the row's index
        rowString += array[x][y];
    } System.out.println(rowString)
}

//      This is the one you want:
// Prints array elements column by column
var colString = "";
for(int y = 0; y < colLength; y++){      // y is the row's index
    for(int x = 0; x < rowLength; x++){  // x is the column's index
        colString += array[x][y];
    } System.out.println(colString)
}