1.1.13编写一段代码,打印出一个M行N列的二维数组的转置(交换行和列)。
public class Test
{
public static void main(String[] args)
{
//初始化
int M=Integer.parseInt(args[0]);
int N=Integer.parseInt(args[1]);
String[][] array=new String[M][N];
for (int row=0;row<M;row++)
for(int col=0;col<N;col++)
array[row][col]=row+","+col;
//打印未转置的元素
int width=Integer.toString(M).length()+ Integer.toString(N).length()+1+4;
String format="%"+Integer.toString(width)+"s";
StdOut.printf(format," ");
for (int col=0;col<N;col++)
StdOut.printf(format,col);
StdOut.printf(" ");
//
for (int row=0;row<M;row++)
{
StdOut.printf(format,row);
for (int col=0;col<N;col++)
StdOut.printf(format,array[row][col]);
StdOut.printf(" ");
}
//打印转置后的元素
StdOut.printf(" ");
StdOut.printf(format," ");
for (int row=0;row<M;row++)
StdOut.printf(format,row);
StdOut.printf(" ");
//
for (int col=0;col<N;col++)
{
StdOut.printf(format,col);
for (int row=0;row<M;row++)
StdOut.printf(format,array[row][col]);
StdOut.printf(" ");
}
}//end main
}//end class
public class Test
{
public static void main(String[] args)
{
//初始化
int M=Integer.parseInt(args[0]);
int N=Integer.parseInt(args[1]);
String[][] array=new String[M][N];
for (int row=0;row<M;row++)
for(int col=0;col<N;col++)
array[row][col]=row+","+col;
//打印未转置的元素
int width=Integer.toString(M).length()+ Integer.toString(N).length()+1+4;
String format="%"+Integer.toString(width)+"s";
StdOut.printf(format," ");
for (int col=0;col<N;col++)
StdOut.printf(format,col);
StdOut.printf(" ");
//
for (int row=0;row<M;row++)
{
StdOut.printf(format,row);
for (int col=0;col<N;col++)
StdOut.printf(format,array[row][col]);
StdOut.printf(" ");
}
//打印转置后的元素
StdOut.printf(" ");
StdOut.printf(format," ");
for (int row=0;row<M;row++)
StdOut.printf(format,row);
StdOut.printf(" ");
//
for (int col=0;col<N;col++)
{
StdOut.printf(format,col);
for (int row=0;row<M;row++)
StdOut.printf(format,array[row][col]);
StdOut.printf(" ");
}
}//end main
}//end class