2015-06-22 67 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请简单地解释我:'( –