新创建一个数组,利用双层for循环遍历数组,把新数组与原数组的行列互换,然后输出新数组。
1 public class Hanshu { 2 public static void sun(int a[][]) { 3 for(int i=0;i<a.length;i++) { 4 for(int j=0;j<a[i].length;j++) { 5 System.out.print(a[i][j]+" "); 6 } 7 System.out.println(); 8 } 9 } 10 public static void main(String []args) { 11 int i,j; 12 int [][]a= new int [][]{{2,3,4},{5,6,7},{1,4,5},{2,4,5}}; 13 System.out.println("行列互换之前"); 14 sun(a); 15 int a1[][]=new int[a[0].length][a.length]; 16 for(i=0;i<a[0].length;i++) { 17 for(j=0;j<a.length;j++) { 18 a1[i][j]=a[j][i]; 19 } 20 } 21 System.out.println("行列互换之后"); 22 sun(a1); 23 } 24 }
运算结果为:
原文:https://www.cnblogs.com/mianyang0902/p/10702269.html