------- android training, java training, looking forward to speaking with you! ----------
1 / * 2 Find the longest string of the same two strings . 3 * / . 4 . 5 class the Compare { . 6 // Find the longest After the same string by subscript . 7 public String lastCompare (STR String, String str2) throws {a NullPointerException . 8 return Compare (ifAndToString (STR, str2), "Last" ); . 9 } 10 // Find the subscript front longest substring same . 11 public String firstCompare (STR String, String str2) throws a NullPointerException { 12 is return Compare (ifAndToString (STR, str2), "First" ); 13 is } 14 // string length of the teaching is processed to identify all of the same string two strings 15 Private String ifAndToString (STR String, String str2) throws a NullPointerException { 16 String Str3 = null ; . 17 String = LONGCHAR " " ; 18 // If the incoming data is illegal 19 IF (str == null && str2 == null && str ==" "&& str2 ==" " ) 20 the throw new new NullPointerException (" Sorry illegal data into "! ) ; 21 // length of the string at a position below the operation easy exchange 22 is IF (str.length () < str2.length()){ 23 is String TEMP = STR; 24 STR = str2; 25 str2 = TEMP; 26 is } 27 IF (! Str.indexOf (str2) = -1 ) { 28 return str2; 29 } the else { 30 // short cycle of characters indexOf operation 31 is for ( int I = 0; I <str2.length (); I ++ ) { 32 for ( int J = str2.length () -. 1; J> I; J, ) { 33 is IF (str.indexOf (Str3 str2.substring = (I, J))! = -1 ) { 34 // the same string into a string separated by commas, the array directly I thought, but not define the length 35 LONGCHAR + = "," + Str3; 36 } 37 [ } 38 is } 39 } 40 return LONGCHAR; 41 is } 42 is // removed longest string 43 is Private String Compare (LONGCHAR String, String Order) { 44 is IF ( "" .equals (LONGCHAR)) 45 return LONGCHAR; 46 is String [] = ARR LONGCHAR. Split ( "," ); 47 int index = 0; 48 //判断取靠前字串还是靠后的 49 if("first".equals(order)){ 50 for(int i = 1; i<arr.length; i++){ 51 if(arr[index].length() < arr[i].length()) 52 index = i; 53 } 54 }else{ 55 for(int i = arr.length - 1; i>0; i--){ 56 if(arr[index].length() < arr[i].length()) 57 index = i; 58 } 59 } 60 return arr[index]; 61 } 62 } 63 64 class XiangTongZiChuan{ 65 public static void main(String[] args){ 66 String str = "sdfasfdshelloewfwfeeeee"; 67 String str2 = "ssfdfhelloe"; 68 try{ 69 Compare cm = new Compare(); 70 System.out.println(cm.lastCompare(str,str2)); 71 }catch(NullPointerException e){ 72 e.printStackTrace(); 73 } 74 } 75 }
Essay experience: 1 if there is a direct throw an exception return to the special circumstances of the election may determine such special circumstances do not write else optimized code in the method.
2. In the above I judge the value of the array maximum length of time is to create a variable of type String constantly receiving new values so that if the heap memory has also been created behind the String object I changed to record with a variable int under this standard, although it will continue to change the value of int variables but it does not create a lot of objects
Reproduced in: https: //www.cnblogs.com/wan-to-fly/archive/2013/01/30/2883366.html