[英]bad operand types for binary operator '<='
I can't seem to figure out why I'm getting this error.我似乎无法弄清楚为什么我会收到此错误。 I've tried putting everything in parenthesis and that helped the problem a little bit.
我试过把所有东西都放在括号里,这对这个问题有一点帮助。 It would be great if I could get some help.
如果我能得到一些帮助就太好了。
Code :代码 :
public void merge(String[] result, String[] nameA, String[] nameB)
{
int i1 = 0; // index into nameA array
int i2 = 0; // index into nameB array
for (int i = 0; i < result.length; i++)
{
if (i2 >= nameB.length || (i1 < nameA.length && nameA[i1] <= nameB[i2]))
{
result[i] = nameA[i1]; // take from nameA
i1++;
}
else
{
result[i] = nameB[i2]; // take from nameB
i2++;
}
}
}
error: bad operand types for binary operator '<='
错误:二元运算符“<=”的错误操作数类型
In order to compare String
s in Java you need to call the method compareTo
.为了在 Java 中比较
String
,您需要调用方法compareTo
。 Have a look at the Comparable interface that String
implements.看看
String
实现的Comparable接口。
<=
and >=
operators are for numeric primitive types like int
or double
. <=
和>=
运算符用于数字原始类型,如int
或double
。 To compare String
s, use compareTo
method.要比较
String
,请使用compareTo
方法。
nameA[i1].compareTo(nameB[i2]) < 0
If you want to compare String
s by length, then use <=
operator on String#length
instead:如果
String
长度比较String
s,请在String#length
上使用<=
运算符:
nameA[i1].length() <= nameB[i2].length()
除了 Luiggi Mendoza 和 Ivaylo Strandjev 的答案之外,我想指出的是,如果您只想确保字符串不同,您可以像这样使用 equals:
if (i2 >= nameB.length || (i1 < nameA.length && !nameA[i1].equals(nameB[i2])))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.