Java implements three output methods for two-dimensional array transpose (IntelliJ idea 2017.2.6 x64)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.