String中Comparable的compareTo使用及釋義

歸納總結

  1. 字符串A(6位)、B(8位),A.compareTo(B)
    首先將A與B字符串1-6位做比較,
    第一種情況:比如第4位上分別爲a、b,unicode相差1則return -1,後面的位置不做比較;
    第二種情況:1-6位都相等,則return A.length-B.lengh;
  2. 如果字符串A、B lenght相同,並且每一位都一樣,則return 0;

測試代碼

/**
 * @author :renpan
 * @version :v1.0
 * @class :com.luomo.addressselected
 * @date :2016-08-15 11:41
 * @description:
 */
public class Test {
    public static void main(String[] args) {
        System.out.println("\"a\".compareTo(\"b\"):"+"a".compareTo("b"));
        System.out.println("\"b\".compareTo(\"a\"):"+"b".compareTo("a"));

        System.out.println("\"ab\".compareTo(\"abc\"):"+"ab".compareTo("abc"));
        System.out.println("\"ab\".compareTo(\"abd\"):"+"ab".compareTo("abd"));
        System.out.println("\"ab\".compareTo(\"abe\"):"+"ab".compareTo("abe"));

        System.out.println("\"ab\".compareTo(\"acdef\"):"+"ab".compareTo("acdef"));
        System.out.println("\"ab\".compareTo(\"abcdef\"):"+"ab".compareTo("abcdef"));

        System.out.println("\"ab\".compareTo(\"ab\"):"+"ab".compareTo("ab"));
    }
}

輸出結果

//a、b兩個字符相差1
"a".compareTo("b"):-1
"b".compareTo("a"):1
//前幾個字符相同,則返回相差的長度。ab、abc長度相差1
"ab".compareTo("abc"):-1
"ab".compareTo("abd"):-1
"ab".compareTo("abe"):-1
"ab".compareTo("abcdef"):-4
//第二個字符不同,則返回unicode相差的值
"ab".compareTo("acdef"):-1
//完全相同,返回0
"ab".compareTo("ab"):0

源碼釋義

/**
  * Compares this string to the given string.
  * 比較兩個字符串
  * <p>The strings are compared one {@code char} at a time.
  * 每次比較一個一個字符
  * In the discussion of the return value below, note that {@code char} does not
  * mean code point, though this should only be visible for surrogate pairs.
  * 比較結果如下,
  * <p>If there is an index at which the two strings differ, the result is
  * the difference between the two {@code char}s at the lowest such index.
  * 兩個字符串的char不同(對於短的字符串從第一個到最後一個),則返回這兩個char之間相差的字符個數(如:"c".compareTo("a")返回2)
  * If not, but the lengths of the strings differ, the result is the difference
  * between the two strings' lengths.
  * 兩個字符串一樣,但是長度不同,則返回字符串的長度差(如:"ad".compareTo("a")返回1)
  * If the strings are the same length and every {@code char} is the same, the result is 0.
  * 兩個字符串長度相同,並且每個char相同則返回0
  * @throws NullPointerException
  *             if {@code string} is {@code null}.
  */
 public native int compareTo(String string);

JDK API釋義

Compares two strings lexicographically(字典順序). The comparison(比較) is based on the Unicode value of each character in the strings.
The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. 
當前字符串和參數字符串做比較
The result is a negative integer if this String object lexicographically precedes the argument string.
結果爲負數,表示當前字符串在參數字符串之前,如a,b(參考源碼來看lexicographically 類似的就是所有字符串的一個大集合,而且是有序的)
 The result is a positive integer if this String object lexicographically follows the argument string. 
 結果爲正數,表示當前字符串在參數字符串之後,如b,a;
The result is zero if the strings are equal;  compareTo returns 0 exactly when the equals(Object) method would return true.
結果爲0表示兩個字符串相等;返回0時,兩個字符串使用equals會返回true
This is the definition of lexicographic ordering. 
If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both.
如果兩個字符串不同,包括從左到右某一位char不同,或者說長度不同。如ab、abc;abc、def
 If they have different characters at one or more index positions, let k be the smallest such index;
 如果兩個字符串有多個位置不一樣,找到索引最小的位置
 then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. 
 然後比較當前位置那個的unicode值小,小多少
 In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
在這種情況下,compareTo返回這兩個字符的差
 this.charAt(k)-anotherString.charAt(k)
 If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
 this.length()-anotherString.length()
 如果位置上的字符都一樣,短字符串在長字符串之前。這種情況下,會返回字符串長度的差
Specified by:
compareTo in interface Comparable<String>
Parameters:
anotherString - the String to be compared.
Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.
發佈了46 篇原創文章 · 獲贊 19 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章