Java Comparator compare method() int 不能取消引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21102315/
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
Comparator compare method() int cannot be dereferenced
提问by user2291903
I'm doing some assignment work and I've struck a problem.
我正在做一些作业,但遇到了一个问题。
I've been given this example code:
我得到了这个示例代码:
public class PersonNameComparator implements Comparator<Person>{
public int compare(Person p1, Person p2) {
int retValue = p1.getName().compareTo(p2.getName());
if (retValue != 0)
return retValue;
else if (p1.getAge() < p2.getAge())
return -1;
else if (p1.getAge() > p2.getAge())
return 1;
else
return 0;
}
}
However When I try to do this, this happens:
但是,当我尝试这样做时,会发生这种情况:
public class DVDComparator implements Comparator <DVD> {
public int compare(DVD d1,DVD d2)
{
int stars1 = d1.getNoOfStars().compareTo(d2.getNoOfStars());
//ERROR - int cannot be dereferenced.
Any ideas?
有任何想法吗?
回答by rgettman
You don't need to call a method to compare primitive int
s. In fact, as you've discovered, you can't.
您不需要调用方法来比较原始int
s。事实上,正如你所发现的,你不能。
Just use the normal <
, >
, and ==
operators to compare int
s.
只需使用普通的<
、>
和==
运算符来比较int
s。
Just make sure to follow the contract of compare
-- return an int
less than 0, equal to 0, or greater than 0
, if d1
is "less than" d2
, if d1
is "equal to" d2
, or d1
is "greater than" d2
, respectively.
只要确保遵循以下约定compare
——分别返回int
小于 0、等于 0 或大于0
,如果d1
是“小于” d2
、d1
“等于”d2
或d1
“大于” d2
。
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
比较它的两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。
回答by Sinkingpoint
int
being a primitive, is not an object and thus does not have any methods, thus when you try and call .compareTo on it, you get an error.
int
作为一个原始类型,它不是一个对象,因此没有任何方法,因此当您尝试对其调用 .compareTo 时,您会收到错误消息。
To compare ints, you can use the standard comparison operators < > ==
, * or * you could wrap it in an Integer
Object and use the compare method as you are doing now. Although the former is favorable.
要比较整数,您可以使用标准比较运算符< > ==
* 或 * 您可以将其包装在一个Integer
对象中并像现在一样使用 compare 方法。虽然前者有利。
回答by Puce
You get this error message since getNoOfStars() returns a primitive int and there is no method compareTo() defined for primitive types.
由于 getNoOfStars() 返回原始 int 并且没有为原始类型定义方法 compareTo(),因此您会收到此错误消息。
If you're using Java SE 7 you can use something like this:
如果您使用的是 Java SE 7,则可以使用以下内容:
public class DVDComparator implements Comparator <DVD> {
public int compare(DVD d1,DVD d2){
return Integer.compare(d1.getNoOfStars(), d2.getNoOfStars());
}
}
回答by Arnav Dubey
// If Person.getName() returns a string then you could simply do the following to // implement Comparable:
// 如果 Person.getName() 返回一个字符串,那么您可以简单地执行以下操作来 // 实现 Comparable:
public class PersonNameComparator implements Comparator<Person>{
public int compare(Person p1, Person p2) {
return p1.getName() - p2.getName(); // It's the ninja way to do it
}
}
回答by Aadhil Rushdy
You can find the reason for above error through thislink. You can use one of following approaches which are simple to understand. (sorting in ascending order)
您可以通过此链接找到上述错误的原因。您可以使用以下易于理解的方法之一。(按升序排列)
if(d1.getNoOfStars() != d2.getNoOfStars())
return d1.d1.getNoOfStars() - d2.getNoOfStars();
or
或者
if(d1.getNoOfStars() != d2.getNoOfStars())
return d1.d1.getNoOfStars() > d2.getNoOfStars() ? 1 : -1;