The program code is as follows:
Implementation of package chapter02;import java.util.arrays;/* * matrix transpose (simple version) */public class Ch02_04 {public static void main (String[] args) {int[][] arr = {{ 1, 2, 3},{4, 5, 6},{7, 8, 9}};int rows = arr.length;int cols = arr[0].length;int[][] arrresult = new int[cols][rows];//fill result array for (int i = 0; i < arrresult.length; i++) {Arrays.fill (arrResult[i], 0);} Matrix for (int i = 0; i < arr.length; i++) {System.out.println before the output matrix is transpose ( Arrays.tostring (Arr[i]));} Transpose The matrix for (int i = 0; i < rows; i++) {//outer loop, starting with the first line of the original matrix for (int j = 0; j < cols; j++) {//inner loop, transpose matrix Arrresult[j][i] = arr[i][j];}} System.out.println ();//output Transpose matrix for (int i = 0; i < arrresult.length; i++) {System.ouT.println (Arrays.tostring (Arrresult[i]);}}}
The results of the implementation are as follows:
[1, 2, 3] [4, 5, 6] [7, 8, 9] [1, 4, 7] [2, 5, 8] [3, 6, 9]
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1976528
Array application: Matrix transpose Java Edition