class pair{
int start;
int end;
pair(int start, int end)
{
this.start = start;
this.end = end;
}
}
public class Solution {
public void intervals(ArrayList<Integer> arrive, ArrayList<Integer> depart) {
ArrayList<pair> list = new ArrayList<>();
for(int i=0;i<arrive.size();i++)
{
pair p = new pair(arrive.get(i),depart.get(i));
list.add(p);
}
Collections.sort(list, new Comparator()
{
@Override
public int compare(pair a, pair b)
{
return a.start.compareTo(b.start);
}
});
错误表明此处不允许使用.start。但是compareTo对于整数应该可以正常工作。
./Solution.java:19: error: <anonymous Solution$1> is not abstract and does not override abstract method compare(Object,Object) in Comparator
{
^
./Solution.java:20: error: method does not override or implement a method from a supertype
@Override
^
./Solution.java:23: error: int cannot be dereferenced
return (a.start).compareTo(b.start);
^
Note: ./Solution.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
Override在我看来是正确的。我不知道它为什么会出错。
原因如下:
return a.start.compareTo(b.start);
由于a被定义为整数,compareTo
是无效的,int是基元,而不是对象,因此没有为java中的方法定义compareTo
。。。
你可以选择
使用适当的运算符比较这些整数(<<><=>=(或将其替换为包装器Integer
class pair{
Integer start;
Integer end;