java - 比较两个词(字符串)

标签 java string string-comparison

这段代码是什么意思?

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/

相关文章:

java - Zillow 是用什么编程语言编写的?

python - 如何使用正则表达式分割字符串而不保留捕获组?

string - lua中string.format函数的 boolean 插值字符是什么

java - Java String 哈希码溢出的后果

java - freemarker - 字符串比较 - 不允许使用运算符

java - 如何保护 Java/Javafx 代码不被最终用户看到?

Java音频播放错误

python - python 比较字符串中的字符

java - 测试字符串是否是字符串列表中任何字符串的子字符串的有效方法

java - Java 中 ln(N!) 的求解算法