2015-06-22 90 views
0

我不知道是什麼的輸出手段?有人會幫助我嗎?請解釋我是如何工作的。什麼是Java字符串compareTo方法和什麼是Java字典序

public class LastIndexOfExample{ 
    public static void main(String args[]){ 
    String s1="hello"; 
    String s2="hello"; 
    String s3="meklo"; 
    String s4="hemlo"; 
    System.out.println(s1.compareTo(s2)); 
    System.out.println(s1.compareTo(s3)); 
    System.out.println(s1.compareTo(s4)); 
    } 
} 

然後輸出爲: -5 -1

+0

把大量的'?'在你的問題中並沒有幫助你更快得到答案。一個寫得很好的解釋性問題效果最好。我儘可能地爲你整理了它。 –

回答

1

compareTo的Javadoc:

如果在這兩個字符串不同的指標,結果是 最低這類指數

在兩個字符的之間的區別

完整的Javadoc:

/** 
    * 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. 
    * If not, but the lengths of the strings differ, the result is the difference 
    * between the two strings' lengths. 
    * If the strings are the same length and every {@code char} is the same, the result is 0. 
    * 
    * @throws NullPointerException 
    *    if {@code string} is {@code null}. 
    */ 
    public native int compareTo(String string); 

而且從在線文檔(以上鍊接):

結果是負整數,如果此String對象 字典順序優先於參數字符串

+0

現在我還沒有理解........:'(索引從零開始...... –

+0

@Vino索引從零開始 - 但結果是「兩個字符之間的差異」 (在他們不同的第一個索引上),這種「差異」也可能是負面的 - 請參閱我的答案底部。 – alfasin

+0

反正tnx .......但我不明白.......我不知道如何輸出像0,-5,-1 .......... Plz請簡單地解釋我:'( –

相關問題