Java 遍历二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25798958/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:10:55  来源:igfitidea点击:

Iterate through 2 dimensional array

javaarraysmultidimensional-array

提问by Peter111

I have a "connect four board" which I simulate with a 2d array (array[x][y] x=x coordinate, y = y coordinate). I have to use "System.out.println", so I have to iterate through the rows.

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

I need a way to iterate this way [0,0] [1,0] [2,0] [3,0] [0,1] [1,1] [2,1] etc

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

If i use the normal procedure:

如果我使用正常程序:

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)

}

it doesnt work because it iterates this way [0,0] [0,1] [0,2] [0,3] etc

它不起作用,因为它以这种方式迭代 [0,0] [0,1] [0,2] [0,3] 等

The normal procedure stays in x and increments y until the end of the column, but i need to say in y and increment x until the end of the row.

正常过程停留在 x 并增加 y 直到列的末尾,但我需要在 y 中说并增加 x 直到行的末尾。

回答by Mateusz Dymczyk

Simple idea: get the lenght of the longest row, iterate over each column printing the content of a row if it has elements. The below code might have some off-by-one errors as it was coded in a simple text editor.

简单的想法:获取最长行的长度,迭代每一列,如果它有元素,则打印一行的内容。下面的代码可能有一些逐一错误,因为它是在一个简单的文本编辑器中编码的。

  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]);
      }
    }
  }

回答by jcstar

Just invert the indexes' order like this:

只需像这样反转索引的顺序:

for (int j = 0; j<array[0].length; j++){
     for (int i = 0; i<array.length; i++){

because all rows has same amount of columns you can use this condition j < array[0].lengt in first for condition due to the fact you are iterating over a matrix

因为所有行都具有相同数量的列,您可以首先使用此条件 j < array[0].lengt 作为条件,因为您正在迭代矩阵

回答by Bharat

Consider it as an array of arrays and this will work for sure.

将其视为一个数组数组,这肯定会起作用。

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]);
        }
    }

回答by BadNews

Just change the indexes. i and j....in the loop, plus if you're dealing with Strings you have to use concat and initialize the variable to an empty Strong otherwise you'll get an exception.

只需更改索引。i 和 j.... 在循环中,此外,如果您正在处理字符串,则必须使用 concat 并将变量初始化为空的 Strong,否则您将收到异常。

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)

回答by Niamatullah Bakhshi

 //This is The easiest I can Imagine . 
 // You need to just change the order of Columns and rows , Yours is printing columns X rows and the solution is printing them rows X columns 
for(int rows=0;rows<array.length;rows++){
    for(int columns=0;columns <array[rows].length;columns++){
        System.out.print(array[rows][columns] + "\t" );}
    System.out.println();}