Java Sample Collection (iv)

Source: Internet
Author: User

Example 1. The transpose operation of the moment Chen

The so-called Torque Chen's transpose is the moment of the ranks of the interchange. The sample code is as follows:

 Public classArrayrowcolumnswap { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intarr[][]=New int[][]{{10,11,12},{20,21,22},{30,31,32}};//creating a two-dimensional arraySYSTEM.OUT.PRINTLN ("****** before the transfer of the moment Chen is: ******");        PrintArray (arr); //output two-dimensional arrays        intarr2[][]=New int[Arr.length][arr.length];  for(inti=0; i<arr.length;i++) {//adjust array row and column data             for(intj=0;j<arr[i].length;j++) {Arr2[j][i]=Arr[i][j]; }} System.out.println ("****** after the transfer of the moment Chen is: ******");    PrintArray (ARR2); }     Public Static voidPrintArray (int[] arr) {         for(inti=0;i<arr.length;i++) {//iterating through an array             for(intj=0;j<arr.length;j++) {System.out.print (Arr[i][j]+ "\ T");        } System.out.println (); }    }}

The results of its implementation are as follows:

The moment before the transpose is: ******10 each of the    ******,    The moment Chen is: ******10    32 off    

Example 2. The trace of Fang Chen
Fangchen traces, i.e. the Fangchen of all elements on the main diagonal. The sample code is as follows:

 Public classTrace { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intarr[][]= {//creating a two-dimensional array{5,10,15,20},                {10,15,20,25},                {15,20,25,30},                {20,25,30,35}        }; intTr=0; System.out.println ("******** Fangchen arr[][] Yes: ********");  for(inti=0;i<arr.length;i++) {//iterating through an array             for(intj=0;j<arr[i].length;j++) {System.out.print (Arr[i][j]+ "\ T");        } System.out.println (); }         for(inti=0;i<arr.length;i++) {TR+=Arr[i][i]; } System.out.println ("Fang Chen arr[][" The trace is: "+tr); }}

The execution results are:

Fangchen arr[][] is: ********5-ten    30        The traces of Fangchen arr[][] are:80

Example 3. Iterating through an array
3.1. Traverse a

 Public classGetDay { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub        intday[]= {31,28,31,30,31,30,31,31,30,31,30,31};//Create and initialize a one-dimensional array         for(inti=0;i<day.length;i++) {//using fou loops to output informationSystem.out.print ((i+1) + "month has" +day[i]+ "Day" + "\t\t"); if((i+1)%3==0) {//If the sum of i+1 is zeroSystem.out.print ("\ n");//Output Enter            }        }    }}

The execution results are shown below:

January There are 31 days in        February there are 28 days        March There are 31 days        April There are 30 days        May has 31 days        June has 30 days        July has 31 days        August has 31 days        September has 30 days        October There are 31 days in        November there are 30 days        December there are 31 days    

3.2. Iterate through the two-dimensional array, the sample code is as follows:

 Public classTrap { Public Static voidMain (string[] args) {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("* * * * * * * * * use for loop traversal of two-dimensional arrays"); intarr[][]=New int[][] {            {10,20},            {12,13,14,15,16},            {17,18,19}        }; //creating a two-dimensional array         for(inti=0;i<arr.length;i++) {             for(intj=0;j<arr[i].length;j++) {//iterate through each element in a two-dimensional arraySystem.out.print (arr[i][j]+ "");        } System.out.println (); } System.out.println (* * * * * * Use foreach statement to traverse a two-dimensional array * * * *);  for(int[] X:arr) {//the outer loop variable is a one-dimensional array             for(intY:X) {//iterate through each array elementSystem.out.print (y+ "");        } System.out.println (); }    }}

The result of the execution is:

  

Java Sample Collection (iv)

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.