就是要打印原数组也要打印互换的二维数组
运行结果:
123
456
789
147
258
369
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入二维数组的列数");
int n1=sc.nextInt();
System.out.println("请输入二维数组的行数");
int n2=sc.nextInt();
//定义一个二维数组
//原数组
int [][]array1=new int[n1][n2];
//行列互掉的二维数组
int [][]array2=new int[n2][n1];
System.out.println("请向数组中输入元素,从左向右,从上到下");
for (int i = 0; i < n1; i++) {
System.out.println("请输入第"+(i+1)+"行数据");
for (int j = 0; j < n2; j++) {
int x= sc.nextInt();
array1[i][j]=x;
array2[j][i]=x;
}
}
//原来的二维数组
System.out.println("原来的二维数组是:");
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(array1[i][j]+" ");
}
System.out.println("");
}
//置换后的二维数组
System.out.println("置换后的二维数组为:");
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(array2[i][j]+" ");
}
System.out.println("");
}
}
}