转载请注明出处:http://hi.baidu.com/jadmin/
/**
*
* @(#)ZhuanzMatrix.java
*
* @author JAdmin
* @version 1.00 2008/08/22
*/
import java.util.Scanner;
public class ZhuanzMatrix
{
public static void main(String[] args)
{
int row,col;
Scanner sr = new Scanner(System.in);
System.out.println("请输入矩阵的行和列参数:");
System.out.print("row=");
row = sr.nextInt();
System.out.print("col=");
col = sr.nextInt();
int[][] array = new int[row][col];
System.out.println("请输入矩阵各元素Matrix[" + row + "]" + "[" + col + "]");
input(array);
System.out.println("原矩阵:");
display(array);
int[][] newarr = doZhuanz(array);
System.out.println("转置后:");
display(newarr);
}
private static void input(int[][] arr)
{
Scanner sr = new Scanner(System.in);
for(int i = 0;i < arr.length;i++)
for(int j = 0;j < arr[0].length;j++)
arr[i][j] = sr.nextInt();
}
private static void display(int[][] arr)
{
for(int i = 0;i < arr.length;i++)
{
for(int j = 0;j < arr[0].length;j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
}
private static int[][] doZhuanz(int[][] arr)
{
int[][] tmparr = new int[arr[0].length][arr.length];
for(int i = 0;i < tmparr.length;i++)
for(int j = 0;j < tmparr[0].length;j++)
tmparr[i][j] = arr[j][i];
return tmparr;
}
}