Java implementation-Three swords per day offre (2-4)

Source: Internet
Author: User

2. Implement a single case mode
1 /**2 * Face question 2: Implementing a singleton pattern3  * 4  * @authorQiuyong a hungry man type5  */6  Public classSingleton01 {7     PrivateSingleton01 () {8     }9 Ten     Private StaticSingleton01 instance =NewSingleton01 (); One  A      Public StaticSingleton01 getinstance () { -         returninstance; -      the     } - } -  - /** + * Face question 2: lazy type -  * @authorQiuyong +  */ A  Public classSINGLETON02 { at      -      Public StaticSINGLETON02 instance; -      -      Public synchronized StaticSingleton02 getinstance () { -         if(NULL==instance) { -Instance=NewSingleton02 (); in         }         -         returninstance; to     } +  - } the  * /** $ * Face question 2: using internal classesPanax Notoginseng  * @authorQiuyong -  */ the  Public classSingleton03 { +      A      Public StaticSingleton03 getinstance () { the         returninnerclass.instatnce; +     } -      $     Private Static classinnerclass{ $          Public StaticSingleton03 instatnce=NewSingleton03 (); -     } -}
3, two-dimensional array lookup

  Title Description: A two-dimensional array with each row incrementing from left to right and each column incrementing from top to bottom. Enter a two-dimensional array and an integer to determine if the array contains integers.

 Public classTest { Public Static BooleanFindint[] Arry,intnum) {        intColumn=arry[0].length-1; intRow=0;  while(row<arry.length&&column>=0){            if(num==Arry[row][column]) {                return true; }            if(arry[row][column]>num) {Column--; }Else{row++; }                    }        return false; }         Public Static voidMain (string[] args) {int[] arry=New int[3] [3];  for(inti=0;i<arry.length;i++){             for(intj=0;j<arry[0].length;j++) {Arry[i][j]= (i+1) * (j+1); }} System.out.println ("See if there is element 9:" +find (arry,9)); System.out.println ("See if there is element 9:" +find (arry,0)); }}
4. Replace Space

  Title Description: Implement a function that replaces each space in the string with "%20".

1  Public classReplaceblank {2      Public Static voidMain (string[] args) {3String str= "I am happy";4Str=Replaceblank (str);5 System.out.println (str);6     }7     8     9      Public Staticstring Replaceblank (String str) {Ten         if(NULL==str) { One             return NULL; A         } -StringBuffer sb=NewStringBuffer (); -          for(intI=0;i<str.length (); i++){ the             if(Str.charat (i) = = "){ -Sb.append ("%20"); -}Else{ - Sb.append (Str.charat (i)); +             } -         } +         returnsb.tostring (); A     } at  -}

Java implementation-Three swords per day offre (2-4)

Related Article

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.