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)