java编写一个将A 3*3矩阵进行转置的方法,并用相应的参数验证程序的正确性

来源:百度知道 编辑:UC知道 时间:2024/05/29 06:13:41

你的转置意思是 行列交换?

public static Object[][] turnA(Object[][] a){
Object[][] b=new Object[a[0].length][a.length];
for(int i=0;i<a.length; i++)
for(int j=0;j<a[0].length;j++)
b[j][i]=a[i][j];
return b;
}

这个方法转置的是对象矩阵,因此不能传入基本变量的数组,需要用类型包装器包装。

e.g:
Integer[][] i=new Integer[3][5];
//int[][] i=new int[4][3];
//Can't use here; Integer,Double,String,byte and so on..
Random r=new Random();
for(int j=0;j<i.length;j++){
Arrays.fill(i[j], r.nextInt(100));
System.out.println(Arrays.toString(i[j]));
}
Object[][] o=turnA(i);
for(int j=0;j<o.length;j++){
System.out.println(Arrays.toString(o[j]));

这是static void main里的一段验证程序,程序最前面需要
import java.util.*;

既然有人回答了 我就算了吧