这段代码是什么意思?
if( item.compareTo(root.element) < 0 ){
}
我读到:
"Compares two strings lexicographically. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument."
但我不明白。有人可以举例说明吗?
最佳答案
查看 Comparable
的文档接口(interface),它定义了 compareTo()
方法放在首位。 String
中此接口(interface)的实现遵循相同的约定:
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
这意味着:如果当前字符串小于作为参数接收的字符串(在lexicographical order 下)返回一个负整数值。如果当前字符串大于作为参数接收的字符串,则返回一个正整数值。否则,字符串相等并返回 0
。
关于java - 比较两个词(字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446075/