编写一个程序,将二维数组a转置后存入数组b(所谓转置就是行列互换)例如:
1 2 3
4 5 6
7 8 9 的转置就是:
1 4 7
2 5 8
3 6 9
public class trans {
public static void main(String[] args) {
int[][]a={ {1,2,3} , {4,5,6}, {7,8,9} };
int rows=a.length;
int cols=a[0].length;
int[][]b=new int[cols][rows];
for (int i=0;i<rows;i++){
for (int j=0;j<cols;j++){
b[j][i]=a[i][j];
}
}
System.out.println("数组b为");
for (int i=0;i<cols;i++){
for (int j=0;j<rows; j++){
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
}