Java String 类 compareTo() 方法返回什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18798890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the String classes compareTo() method return
提问by cupojava
In the Java API on oracles website: "compareTo Returns: "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument." "
在 oracles网站上的 Java API 中:“compareTo Returns:”如果参数字符串等于此字符串,则值为 0;如果此字符串按字典顺序小于字符串参数,则为小于 0 的值;如果此字符串按字典顺序大于字符串参数,则为大于 0 的值。" "
Here is an if statement:
这是一个 if 语句:
String a = "abd";
String b = "abc";
if(a.compareTo(b) >= 1)
returns true since string a is greater, lexicographically.
返回 true,因为字符串 a 更大,按字典顺序排列。
My question is, does the compareTo always return a 0, 1, or -1? or does it return the actual amountthat the string is greater than or less than the string argument.
我的问题是,compareTo 是否总是返回 0、1 或 -1?或者它是否返回字符串大于或小于字符串参数的实际数量。
So in the above if statement, since "abd" is one greater than "abc" is it returning 1?
那么在上面的 if 语句中,由于“abd”比“abc”大1,它是否返回1?
采纳答案by chrylis -cautiouslyoptimistic-
As far as you're concerned, there's no telling what the magnitude of the compareTo
return value is, just the sign. In practice, most compareTo
implementations will return -1, 0, or 1, but the contract specifically says positive or negative, and you should write your code accordingly (e.g., using int compare = a.compareTo(b); if(compare > 0) {...} else...
).
就您而言,不知道compareTo
返回值的大小,只有符号。在实践中,大多数compareTo
实现将返回 -1、0 或 1,但契约明确规定为正或负,您应该相应地编写代码(例如,使用int compare = a.compareTo(b); if(compare > 0) {...} else...
)。
回答by Diego C Nascimento
According to http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
根据http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#compareTo%28java.lang.String%29
In this case, compareTo returns the difference of the two character values at position k >in the two string -- that is, the value:
this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()
在这种情况下, compareTo 返回两个字符串中位置 k > 处的两个字符值的差——即值:
this.charAt(k)-anotherString.charAt(k)
如果没有它们不同的索引位置,则较短的字符串按字典顺序排在较长的字符串之前。在这种情况下, compareTo 返回字符串长度的差值——即值:
this.length()-anotherString.length()
For the last case, for the lengths of the String
, by documentation that seems it can return other than -1, 0, 1
对于最后一种情况,对于 的长度String
,根据文档,它似乎可以返回 -1, 0, 1 以外的值
回答by CrippledTable
Falmarrifully answered this question; as opposed to only indicating the conditions in which the return value would be positive, negative or zero.
Falmarri完全回答了这个问题;与仅指示返回值为正、负或零的条件相反。
"This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value:
“这是词典排序的定义。如果两个字符串不同,那么它们要么在某个索引处具有不同的字符,该索引对两个字符串都是有效索引,要么它们的长度不同,或者两者都有。如果它们在一个或多个处具有不同的字符更多的索引位置,让 k 为最小的这样的索引;然后使用 < 运算符确定在位置 k 的字符具有较小值的字符串,按字典顺序排在另一个字符串之前。在这种情况下,compareTo 返回两者的差值两个字符串中位置 k 处的字符值——即值:
this.charAt(k)-anotherString.charAt(k)
this.charAt(k)-anotherString.charAt(k)
If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
如果没有它们不同的索引位置,则较短的字符串按字典顺序排在较长的字符串之前。在这种情况下, compareTo 返回字符串长度的差值——即值:
this.length()-anotherString.length()"
this.length()-anotherString.length()"