compareTo 在 Java 中有什麼用


Java 中的 compareTo() 方法按字母順序比較兩個字串。

示例

即時演示

public class Test {
   public static void main(String args[]) {
      String str1 = "Strings are immutable";
      String str2 = new String("Strings are immutable");
      String str3 = new String("Integers are not immutable");

      int result = str1.compareTo( str2 );
      System.out.println(result);

      result = str2.compareTo( str3 );
      System.out.println(result);
   }
}

輸出

0
10

更新於:2020 年 2 月 26 日

129 次瀏覽

點亮您的 事業

完成課程獲取認證

開始
廣告
© . All rights reserved.