First question code:
Parsing: Each string in a character array is identified with the longest of the characters in the array, compared to the length in the character arrays:
A, when the length of the longest string is greater than or equal to the length of the character array, the direct output
b, when the length of the longest string is less than the length of the character array, find the string that has the largest integer value at that length, in front of the 0 output
Package Com.njupt;public class Heia {public String MaxLength (string[] string) {int max=integer.valueof (string[0]); int index=0;for (int i=1;i<string.length;i++) {int temp=integer.valueof (string[i]), if (max<temp) {max=temp;index=i ;}} if (String[index].length () >=string.length) {return string[index];} Else{string str= ""; int tem=string.length-string[index].length (); for (int j=0;j<tem;j++) {str + = "0";} STR +=string[index];return str;}} public static void Main (string[] args) {string[] string={"", "2", "0"}; Heia h=new Heia (); System.out.println (H.maxlength (String));}}
The second code: (The code may be a bit of a problem, wait for modification)
Import Java.util.arraylist;public class Test {public void Qunaer (string string) {arraylist<string> Alist=new Arraylist<> (); String=string.trim (); String str=string.valueof (String.charat (0)), for (int i=1;i<string.length (); i++) {if (String.charat (i) = = '-') {str= Str.trim (); Alist.add (str); str= "-";} Else{if (String.charat (i)! = ") {str +=string.charat (i);} Else{if (String.charat (i-1)! = ") {str + =" ";}}}} Str=str.trim (); Alist.add (str); System.out.println (alist);} public static void Main (string[] args) {//TODO auto-generated method stubstring string= "-name Lily -age School \ "Zhao yan\";//string String = " Zhao yan "; Test t=new test (); T.qunaer (string);}}
Third question code:
Analysis: Because the goal of the topic is how to make string a by adding or reducing the character, and finally get the string b
Note: string a= "BC", String b= "CB"; to perform +c,-c operation
Specific practices:
1, traversal string A, (the current position of a is I);
2. Determine the character of the first position in the string (A. CharAt (i)) is present in B;
A, if present, starts with the first character of B (j=0) to find the first character that is equal to the current position I of string A, and b precedes the character B, which +b the corresponding character. CharAt (j), and finally delete the characters before the j+1 position in B
b, if it does not exist, execute-A. CharAt (i) operation.
3, when the completion of the string a, determine if there are characters in string B, one time to execute +b.charat (j).
Package Com.njupt;public class Cont {public string diff (string a,string b) {string string= "", for (int i=0;i<a.length (); i++) {String str=string.valueof (A.charat (i)), if (B.contains (str)) {int j=0;//system.out.println (B.charat (j)! = A.charat (i)); while (B.charat (j)!=a.charat (i) {string + = "+" +b.charat (j) + ","; ++j;} B=b.substring (j+1);} Else{string + = "-" +a.charat (i) + ",";}} for (int i=0;i<b.length (); i++) {string + = "+" +b.charat (i) + ",";} return string;} /** * @param args */public static void main (string[] args) {//TODO auto-generated method stubstring string1= "abaced"; String string2= "DABABABACDB"; Cont c= new Cont (); System.out.println (C.diff (string1, string2));//system.out.println (string1.substring (1));}}
Where to go 2015 questions Java implementation