Java——String類中的compareTo方法總結


String類的定義:

   java.lang
 類 String

  java.lang.Object
      java.lang.String


所有已實現的接口:
Serializable, CharSequence, Comparable<String>
 

public final class String
      extends Objectimplements Serializable, Comparable<String>, CharSequence
String 類代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作為此類的實例實現。

字符串是常量;它們的值在創建之后不能更改。字符串緩沖區支持可變的字符串。因為 String 對象是不可變的,所以可以共享。

例如:

     String str = "abc";
 

等效於:

     char data[] = {'a', 'b', 'c'};
     String str = new String(data);

再來看看String類中的compareTo方法:

具體解釋如下:

compareTo
public int compareTo(String anotherString)
按字典順序比較兩個字符串。該比較基於字符串中各個字符的 Unicode 值。按字典順序將此 String 對象表示的字符序列與參數字符串所表示的字符序列進行比較。如果按字典順序此 String 對象位於參數字符串之前,則比較結果為一個負整數。如果按字典順序此 String 對象位於參數字符串之后,則比較結果為一個正整數。如果這兩個字符串相等,則結果為 0;compareTo 只在方法 equals(Object) 返回 true 時才返回 0。
這是字典排序的定義。如果這兩個字符串不同,那么它們要么在某個索引處的字符不同(該索引對二者均為有效索引),要么長度不同,或者同時具備這兩種情況。如果它們在一個或多個索引位置上的字符不同,假設 k 是這類索引的最小值;則在位置 k 上具有較小值的那個字符串(使用 < 運算符確定),其字典順序在其他字符串之前。在這種情況下,compareTo 返回這兩個字符串在位置 k 處兩個char 值的差,即值:

this.charAt(k)-anotherString.charAt(k)

 

如果沒有字符不同的索引位置,則較短字符串的字典順序在較長字符串之前。在這種情況下,compareTo 返回這兩個字符串長度的差,即值:
this.length()-anotherString.length()

this.charAt(k)-anotherString.charAt(k)

 

指定者:
接口 Comparable<String> 中的 compareTo
參數
anotherString - 要比較的 String。
返回
如果參數字符串等於此字符串,則返回值 0;如果此字符串按字典順序小於字符串參數,則返回一個小於 0 的值;如果此字符串按字典順序大於字符串參數,則返回一個大於 0 的值。

 N多的類中都有該compareTo方法,根本原因是因為它們都實現了接口comparable接口,並且實現了接口中的compareTo方法。

如下所示:

 

public interface Comparable<T>
此接口強行對實現它的每個類的對象進行整體排序。這種排序被稱為類的自然排序,類的 compareTo 方法被稱為它的自然比較方法。

實現此接口的對象列表(和數組)可以通過 Collections.sort(和 Arrays.sort)進行自動排序。實現此接口的對象可以用作有序映射中的鍵或有序集合中的元素,無需指定比較器。

對於類 C 的每一個 e1 和 e2 來說,當且僅當 e1.compareTo(e2) == 0 與 e1.equals(e2) 具有相同的 boolean 值時,類 C 的自然排序才叫做與 equals 一致。注意,null 不是任何類的實例,即使 e.equals(null) 返回 false,e.compareTo(null) 也將拋出 NullPointerException。

實際上,所有實現 Comparable 的 Java 核心類都具有與 equals 一致的自然排序。java.math.BigDecimal 是個例外,它的自然排序將值相等但精確度不同的 BigDecimal 對象(比如 4.0 和 4.00)視為相等。

 

 說了這么多,我們來看一下String類中是如何實現compareTo方法的:

 1     /* @param   anotherString   the <code>String</code> to be compared.
 2      * @return  the value <code>0</code> if the argument string is equal to
 3      *          this string; a value less than <code>0</code> if this string
 4      *          is lexicographically less than the string argument; and a
 5      *          value greater than <code>0</code> if this string is
 6      *          lexicographically greater than the string argument.
 7      */
 8     public int compareTo(String anotherString) {
 9         int len1 = value.length;
10         int len2 = anotherString.value.length;
11         int lim = Math.min(len1, len2);
12         char v1[] = value;
13         char v2[] = anotherString.value;
14  
15         int k = 0;
16         while (k < lim) {
17             char c1 = v1[k];
18             char c2 = v2[k];
19             if (c1 != c2) {
20                 return c1 - c2;
21             }
22             k++;
23         }
24         return len1 - len2;
25     }

 

 由源碼可以看出:

     首先取出兩個字符串的長度,比較較小的長度內,兩者是否相等。

          若不相等,則直接返回該位置字符的ASCII碼相減后的值。

          若各位置都相等,則將兩個字符串長度的差值返回。

原文:https://blog.csdn.net/qq_25827845/article/details/53870329


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM