1 Importjava.util.Arrays;2 3 /**4 * Created by Stefango at 9:54 on 2018/7/225 */6 Public classSolution {7 Public Static int[] Transpose (int[] A) {8 intR = a.length, C = a[0].length;9 int[] ans =New int[c][r];Ten for(intR = 0; R < R; r++) One for(intc = 0; c < C; C++) AANS[C][R] =A[r][c]; - returnans; - } the - Public Static voidMain (string[] args) { - int[] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; - for(int[] i:a) { + for(intj:i) -System.out.print (j + ""); + System.out.println (); A } atSYSTEM.OUT.PRINTLN ("---transpose---"); - int[] B =transpose (A); - - //method One: traditional for loop - //for (int i = 0; i < b.length; i++) { - //For (int j = 0; J < B[i].length; J + +) in //System.out.print (B[i][j] + ""); - //System.out.println (); to // } + - //method Two: for Each loop the //For (int[] i:b) { * //for (int j:i) $ //System.out.print (j + "");Panax Notoginseng //System.out.println (); - // } the + //method Three: Take advantage of the ToString method in the arrays class (slightly different from the output of the first two methods) A for(inti=0;i<b.length;i++) the System.out.println (arrays.tostring (b[i)); + //method Three outputs are - //[1, 4, 7] $ //[2, 5, 8] $ //[3, 6, 9] - } -}
Java implements three output methods for two-dimensional array transpose (IntelliJ idea 2017.2.6 x64)