简体   繁体   English

对齐2d阵列打印输出[Java]

[英]Aligning 2d Array Print Ouput [Java]

I declared a 2D int Array with some values, and I need to invert rows/columns on the Console. 我声明了带有某些值的2D int数组,并且需要在控制台上反转行/列。 The first row has to be shown vertically as the first column, the 2nd row would have to be shown vertically as the 2nd column, and so on. 第一行必须垂直显示为第一列,第二行必须垂直显示为第二列,依此类推。 So for example, the values of [0][0] , [0][1] , [0][2] should be printed vertically, and the upcoming values of the 2nd row, should be aligned also vertically just to the right. 因此,例如,[0] [0],[0] [1],[0] [2]的值应垂直打印,而第二行的即将出现的值也应垂直向右对齐。

Here's my code so far! 到目前为止,这是我的代码!

public class Print {
    public static void main(String[] args) {

        int firstarray[][] = {{8,9,10,11},{12,13,14,15}};

        System.out.println("This is the inverted array");
        display(firstarray);

    }

public static void display (int x[][]){
    for(int row=0; row<x.length; row++){
        for(int column = 0; column<x[row].length; column++){
            System.out.println(x[row][column] + "\t");
        }
        System.out.print("\n");
    }   
}   
}

Current Output(Not aligned properly): 电流输出(未正确对齐):

This is the inverted array
8   
9   
10  
11  

12  
13  
14  
15  

Correct output should be: 正确的输出应为:

This is the inverted array
8,12    
9,13    
10,14   
11,15   

Just change your for loop, 只需更改您的for循环,

    for(int row=0; row<1; row++){
        for(int column = 0; column<x[row].length; column++){
            System.out.println(x[row][column] + "\t"+x[row+1][column] );
        }
        System.out.print("\n");
    } 

into below this will work even you have 5000 rows, 到下面,即使您有5000行,也可以使用,

     for(int row=0; row<x.length-1; row++){
        for (int column = 0; column < x.length; column++) {
            System.out.print(x[column][row] + "\t" );   
            }
            System.out.println();       
      }

You just change the limit on how many rows you're scanning through: You can create a preset number, (as I have) : 您只需更改要扫描的行数的限制即可:您可以创建一个预设编号(如我所知):

public class Print {

public static int myBigInt;

public Print() {
}

public static void main(String[] args) {
    myBigInt = 5000;
    int myArray[][] = new int[myBigInt][myBigInt];

    for (int i = 0; i < myBigInt - 1; i++) {
        for (int j = 0; j < myBigInt - 1; j++) {
            int x = (int) (Math.random() * (50));
            int y = (int) (Math.random() * (30) / 2);
            myArray[i][j] = (x + ((x) + (y * 4)) / 2) + (y + ((x * 7) + (y)) / 2);
        }
    }
    System.out.println("This is the inverted array");
    display(myArray);
}

public static void display(int x[][]) {
    for (int row = 0; row < myBigInt - 1; row++) {
        for (int column = 0; column < x[row].length; column++) {
            System.out.println(x[row][column] + "\t" + x[row + 1][column]);
        }
        System.out.print("\n");
    }
}

} }

or even go so far as to generate a random number for the limit, like: 甚至可以为该限制生成一个随机数,例如:

myBigInt = (int) (Math.random() * (500));

This way, you can change the size of your array, without having to change anything else. 这样,您可以更改阵列的大小,而无需更改其他任何内容。

I got a little stupid on the random number generator, but this will print out 4999 sets of 5000 numbers in a side by side array. 我对随机数生成器有些愚蠢,但这会在并排数组中打印出4999套5000个数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM