Java Letter Plus number combination comparison size _java

Source: Internet
Author: User

For strings that are combination of numbers and letters, such as "A20" and "A9", comparisons result in "A20" > "A9". This situation directly calls the string's CompareTo method and the result is not what we want. This time you need to write your own class and implement the Comarable interface for comparison. This was a source interview question, but there was only one thought.

  Ideas:

1. Convert the string to a list containing substring and integers. (Here is a string spit method to split directly.) Don't know what to do? )

2. Compare the data in the list.

3. If the comparison results in the list are equal, then the CompareTo of string is called.

  Code implementation:

Package Testsource; 
Import java.util.ArrayList; 
 
Import java.util.List; /** * * @author waston Xu * @date 2011-4-15 a.m. 10:48:21/public class MyString implements 
  > {private final string string; 
 
  Private list<object> List; 
    Public MyString (String string) {this.string = string; 
  Trimstring2list (); 
    private void Trimstring2list () {list = new arraylist<object> (); 
    Integer num = 0; 
    StringBuffer sb = new StringBuffer (); 
      for (int i = 0; i < this.string.length (); i++) {char c = this.string.charAt (i); 
          if (C > && C <) {if (sb.length ()!= 0) {list.add (SB); 
        SB = new StringBuffer (); 
        num = num * + (C-' 0 '); 
      Continue 
          else {if (num!= 0) {list.add (num); 
        num = 0; 
        } sb.append (c); 
      Continue } if (Sb.length ()!= 0) {List.add (SB); 
    SB = new StringBuffer (); 
      else if (num!= 0) {list.add (num); 
    num = 0; 
   } * * After careful viewing of the string's CompareTo method, it was intended not to invoke its API for programming, * but consider situations such as "A02" and "A2" or use the way in their APIs to resolve them. 
    * * Private int comparetolikestring (String s) {int len1 = String.Length (); 
    int len2 = S.length (); 
    int n = math.min (len1, len1); 
      if (n > 0) {int k = 0; 
        while (K < n) {char C1 = String.charat (k); 
        char C2 = S.charat (k); 
        if (c1!= C2) return C1-C2; 
      k++; 
  } return len1-len2; 
    @Override public int CompareTo (MyString anotherstring) {int len1 = List.size (); 
    int len2 = AnotherString.list.size (); 
 
    int n = math.min (len1, len2); 
    int mark = 0; 
      if (n > 0) {int i = 0; 
        while (I < n) {Object O1 = List.get (i); 
        Object O2 = AnotherString.list.get (i); if (O1 instanceof INteger && O2 instanceof integer) {mark = (integer) O1-(integer) O2; 
        else {mark = O1.tostring (). CompareTo (O2.tostring ()); 
        If (Mark!= 0) return to mark; 
      i++; 
    } return Comparetolikestring (anotherstring.string); 
    Return String.CompareTo (anotherstring.string); 
  * * If this writing will have a disadvantage is the emergence of "A02" and "A2" such a situation, must be the front of small * * *//return string.length ()-anotherString.string.length (); 
    public static void Main (string[] args) {String S1 = "b9c"; 
    String s2 = "b09c"; 
    MyString m1 = new MyString (S1); 
 
    MyString m2 = new MyString (s2); 
  System.out.println (M1.compareto (m2)); 

 } 
}

See more Java syntax, you can focus on: "Thinking in the Java Chinese manual", "JDK 1.7 reference manual in English," "JDK 1.6 API Java Chinese Reference manual", "JDK 1.5 API Java Chinese Reference manual", also hope that the big A lot of home support cloud habitat community.

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.