int arr[][] = {{1,2,3},{4,5,6},{7,8,9}}; System.out.println("转置前的矩阵是:"); printArray(arr); int arr2[][] = new int[arr.length][arr.length]; for(int i = 0;i < arr.length;i++) { for(int j =0;j < arr[i].length;j++) { arr2[j][i] = arr[i][j]; } } System.out.println("转置后的矩阵是: "); printArray(arr2); } private static void printArray(int[][] arr) { // TODO 自动生成的方法存根 for(int i = 0;i < arr.length;i++) { for(int j = 0;j < arr.length;j++) { System.out.print(arr[i][j]+ " "); } System.out.println(); }
int jiu[][] = new int [3][3]; int a =2; int b = 3/2; for(int e = 1; e <= 9;e++) { jiu[a++][b++] = e; if(e%3 ==0) { a = a-2; b = b-1; } else { a = a%3; b = b%3; } } System.out.println("输出九宫格:"); for(int e=0;e<3;e++) { for(int f=0; f<3;f++) { System.out.print(jiu[e][f] + " "); } System.out.print("\n"); }
比着打的,木看懂。。。(>~<)
原文:http://www.cnblogs.com/baozhaba-xianchong/p/5118810.html