Java 快速導覽 - 關於 compareTo() 的補充範例說明

補充範例如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class CompareTest {
    public static void main(String[] args) {
        String a = "apple";
        String b = "air";
        String c = "fish";
     
        System.out.println("apple-air :" + a.compareTo(b));
        System.out.println("air-apple :" + b.compareTo(a));
        System.out.println("apple-fish:" + a.compareTo(c));
        System.out.println("fish-apple:" + c.compareTo(a));
        System.out.println("air-fish  :" + b.compareTo(c));
        System.out.println("fish-air  :" + c.compareTo(a));
         
        String d = "Abba";
        String e = "abet";
        String f = "water";
 
        System.out.println("Abba-abet :" + d.compareToIgnoreCase(e));
        System.out.println("abet-Abba :" + e.compareToIgnoreCase(d));
        System.out.println("Abba-water:" + d.compareToIgnoreCase(f));
        System.out.println("water-Abba:" + f.compareToIgnoreCase(d));
        System.out.println("abet-water:" + e.compareToIgnoreCase(f));
        System.out.println("water-abet:" + f.compareToIgnoreCase(e));
         
        Integer g = 10;
        Integer h = 10;
        Integer i = 23;
        Double j = 20.2;
        Double l = 20.2;
        Double k = 33.33;
 
        System.out.println("10-10     :" + g.compareTo(h));
        System.out.println("10-23     :" + g.compareTo(i));
        System.out.println("23-10     :" + i.compareTo(g));
        System.out.println("20.2-20.2 :" + j.compareTo(l));
        System.out.println("20.2-33.33:" + l.compareTo(k));
        System.out.println("33.33-20.2:" + k.compareTo(l));
    }
}
 
/* 《程式語言教學誌》的範例程式
    檔名:CompareTest.java
    功能:示範 Java 程式
    作者:張凱慶
    時間:西元 2011 年 8 月 */


編譯後執行,結果如下



"apple" 與 "air" 進行比較,差別在第二個字元 'p' 與 'i' ,結果回傳 7 或 -7
7
8
System.out.println("apple-air :" + a.compareTo(b));
System.out.println("air-apple :" + b.compareTo(a));


"apple" 與 "fish" 進行比較,差別在第一個字元 'a' 與 'f' ,結果回傳 5 或 -5
9
10
System.out.println("apple-fish:" + a.compareTo(c));
System.out.println("fish-apple:" + c.compareTo(a));


"air" 與 "fish" 進行比較,差別在第一個字元 'a' 與 'f' ,結果回傳 5 或 -5
11
12
System.out.println("air-fish  :" + b.compareTo(c));
System.out.println("fish-air  :" + c.compareTo(a));


"Abba" 與 "abet" 不分大小寫進行比較,差別在第三個字元 'b' 與 'e' ,結果回傳 3 或 -3
18
19
System.out.println("Abba-abet :" + d.compareToIgnoreCase(e));
System.out.println("abet-Abba :" + e.compareToIgnoreCase(d));


"Abba" 與 "water" 不分大小寫進行比較,差別在第一個字元 'A' 與 'w' ,結果回傳 22 或 -22
20
21
System.out.println("Abba-water:" + d.compareToIgnoreCase(f));
System.out.println("water-Abba:" + f.compareToIgnoreCase(d));


"abet" 與 "water" 不分大小寫進行比較,差別在第一個字元 'a' 與 'w' ,結果回傳 22 或 -22
22
23
System.out.println("abet-water:" + e.compareToIgnoreCase(f));
System.out.println("water-abet:" + f.compareToIgnoreCase(e));


10 與 10 進行比較,並無差別,結果回傳 0
32
System.out.println("10-10     :" + g.compareTo(h));


10 與 23 進行比較,有差別,結果回傳 1 或 -1
33
34
System.out.println("10-23     :" + g.compareTo(i));
System.out.println("23-10     :" + i.compareTo(g));


20.2 與 20.2 進行比較,並無差別,結果回傳 0
35
System.out.println("20.2-20.2 :" + j.compareTo(l));


20.2 與 33.33 進行比較,有差別,結果回傳 1 或 -1
36
37
System.out.println("20.2-33.33:" + l.compareTo(k));
System.out.println("33.33-20.2:" + k.compareTo(l));


本篇文章由花生狼網友的指正,於 2011, 8, 26 新增加的範例說明文章。


您可以繼續參考
Number 類別
String 類別


相關目錄
Java 快速導覽
Java 教材目錄
首頁


參考資料
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#compareToIgnoreCase(java.lang.String)
http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#compareTo(java.lang.Integer)
http://download.oracle.com/javase/6/docs/api/java/lang/Double.html#compareTo(java.lang.Double)

沒有留言: