问题描述
我正在引用我的Java版本JDK 1.8,但仍然出现错误.这种引用有什么问题(六年后编写Java)?或任何其他更简单的方法来做到这一点?我进行了一些搜索,这些功能在更高的Java版本中可用. Eclipse是Oxygen
I am referencing my java version JDK 1.8 but I am still getting error. What is wrong with this referencing (writing Java after 6 years)? or any other simpler way to do this? I did some search and these functions are available in later java versions. Eclipse is Oxygen
对于Long类型,方法compareTo()未定义
The method compareTo() is undefined for the type Long
import java.util.Comparator;
import java.lang.Long;
public class MyComparator<Long> implements Comparator<Long>{
@Override
public int compare(Long long1, Long long2) {
//Long.compareTo()
return Long.valueOf(long1).compareTo(Long.valueOf(long2));
}
}
和JDK指向
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
推荐答案
您在MyComparator
的声明中声明了名称为Long
的通用类型,然后又阴影 java.lang.Long
.您的课程不应该是通用的.另外,您不需要Long.valueOf
,因为您已经具有Long
实例.更改它以删除通用名称,例如
Your declaration of MyComparator
declares a generic type of name Long
, and that then shadows java.lang.Long
. Your class shouldn't be generic. Also, you don't need Long.valueOf
since you already have Long
instance. Change it to remove the generic, like
public class MyComparator implements Comparator<Long> {
@Override
public int compare(Long long1, Long long2) {
return long1.compareTo(long2);
}
}
这篇关于Java Long Compare和ValueOf方法未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!