二维数组将行打印为列java

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

2 dimensional arrays printing rows as columns java

javaarrays

提问by CyberHydra

We have been asked to print this 2D Array with the columns as rows

我们被要求打印这个二维数组,列作为行

For example: the first column is 20,11,27 and it has to be printed:

例如:第一列是 20,11,27 并且它必须被打印:

20
11
27

20
11
27

Here's my code so far and I can't even get it printing the columns normally, do any of you guys know what the problem is and if you can help me find a solution to the problem?

到目前为止,这是我的代码,我什至无法正常打印列,你们中的任何人都知道问题是什么,如果您能帮我找到问题的解决方案吗?

public class TwoDimensionalArrays
{
public static void main (String args[])
{
    final int size1 = 2, size2 = 4, size3 = 5;
    int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
    int row = 0, col = 0;

        for(row = 0; row <= size1; row++); //loops through rows
        {
            for(col = 0; col <= size2; col++); //loops through columns
            {
                System.out.println(numbers[row][col]);
            }
        System.out.print("\n"); //takes a new line before each new print
        }
    }
 }

回答by NeplatnyUdaj

You shouldn't rely on some predefined sizes of the multidimensional arrays(better name is array of arrays). Always use the real size of the array like numbers.lengthand numbers[0].lengthor use for-each like this:

您不应该依赖多维数组的某些预定义大小(更好的名称是数组数组)。始终像这样使用数组的实际大小numbers.lengthnumbers[0].length或像这样使用 for-each:

int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        

for (int[] row: numbers){
    for (int num: row){
        System.out.print(num+" ");
    }
    System.out.println();
}

Result is this:

结果是这样的:

20 25 34 19 33 
11 17 15 45 26 
27 22 9 41 13 

If you want that transposed, you could do it like this:

如果你想要转置,你可以这样做:

    for(int i = 0; i < numbers[0].length; i++) {
        for(int j = 0; j < numbers.length; j++) {
            System.out.print(numbers[j][i]+"\t");
        }
        System.out.println();
    }

Now the result is:

现在的结果是:

20  11  27  
25  17  22  
34  15  9   
19  45  41  
33  26  13  

Note: There's nothing like rows and columns in the array of arrays, only dimensions.

注意:数组数组中没有行和列,只有维度。

回答by Alya'a Gamal

Delete ;in the end of the loops

;在循环末尾删除

like this :

像这样 :

for (row = 0; row <= size1; row++) //loops through rows
 {
   for (col = 0; col <= size2; col++) //loops through columns
    {
       System.out.print(numbers[row][col]+" ");
     }
    System.out.print("\n"); //takes a new line before each new print
  }


The Output :

输出 :

20 25 34 19 33 
11 17 15 45 26 
27 22 9 41 13 

回答by Thomas

You don't need to provide the sizes directly but you might have to calculate (or, to make it simpler, provide) the maximum length of an element.

您不需要直接提供尺寸,但您可能必须计算(或者,为了更简单,提供)元素的最大长度。

Printing could then look like this:

打印可能如下所示:

int maxDigits = 2; //provided or calculated
int [][] numbers = {{20,25,34,19,33}, {11,17,15,45,26}, {27,22,9,41,13}};        
for( int[] row : numbers ) {
  for( int n : row) {
    System.out.print( String.format("%" + maxDigits + "d ", n) ); //creates the format string "%2d" for 2 digits maximum
  }
  System.out.println(); //takes a new line before each new print
}

Note that you should use System.out.print()for printing withoutlinebreaks.

请注意,您应该System.out.print()用于没有换行符的打印。

The output would then look like this:

输出将如下所示:

20 25 34 19 33 
11 17 15 45 26 
27 22  9 41 13 

回答by user5862132

public class rwr {

    public static void main(String[] args){ 

final int size1=2,size2=4,size3=5;

        int[][] number={{34,34,33,33,44},{22,23,24,23,24},{23,44,55,66,66}};

        int row=0,col=0;

        for(row=0;row<=size1;row++){

            for(col=0;col<=size2;col++){

                System.out.print(number[row][col]+"\t");

            }
            System.out.print(" \n");
        }
    }

}
run:
34  34  33  33  44   
22  23  24  23  24   
23  44  55  66  66   
BUILD SUCCESSFUL (total time: 0 seconds)