对齐二维阵列打印输出[Java]
我用一些值声明了一个2D int数组,需要在控制台上反转行/列。 第一行必须垂直显示为第一列,第二行必须垂直显示为第二列,依此类推。 因此,例如,[0][0]、[0][1]、[0][2]的值应垂直打印,第二行的后续值也应垂直向右对齐 这是到目前为止我的代码对齐二维阵列打印输出[Java],java,arrays,printing,console,invert,Java,Arrays,Printing,Console,Invert,我用一些值声明了一个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
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,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");
}
for(int row=0;row您只需更改扫描行数的限制:您可以创建一个预设数字(正如我所做的):
这样,您就可以更改数组的大小,而无需更改任何其他内容
我在随机数生成器上有点笨,但这将在并排数组中打印出4999组5000个数字。谢谢!但是如果我声明了一个大的2d数组(比如说超过5000行),我在第一个for循环(行)上该怎么办?差不多了!现在如果我声明int firstarray[]={8,9,10,11},{12,13,14,15},{1,2,3},{15,88,99};我不会显示输出的最后两行。我想我需要使用某种动态变量,因为我以后必须使用不同的数组(具有不同的声明)。非常感谢!Kudos(Y)
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");
}
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();
}
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");
}
}
myBigInt = (int) (Math.random() * (500));