我声明了带有某些值的2D int数组,并且需要在控制台上反转行/列。
第一行必须垂直显示为第一列,第二行必须垂直显示为第二列,依此类推。
因此,例如,[0] [0],[0] [1],[0] [2]的值应垂直打印,第二行的即将出现的值也应垂直对齐。

到目前为止,这是我的代码!

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


电流输出(未正确对齐):

This is the inverted array
8
9
10
11

12
13
14
15


正确的输出应为:

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

最佳答案

只需更改您的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");
    }


到下面,即使您有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();
      }

10-02 00:19
查看更多