【发布时间】:2017-04-17 03:14:00
【问题描述】:
我正在尝试对事件的结束时间进行排序。在我的应用程序类中,endTime 位于我的 Event 类中,由我的 Time 类中的小时和分钟定义。
在我的事件类中添加了implements Comparable<Event>,虽然我得到了The type Event must implement the inherited abstract method Comparable<Event>.compareTo(Event)。我尝试了快速修复add unimplemented methods,但收效甚微。
ArrayList<Event> events = new ArrayList <Event>();
Time endTime = new Time((startTime.getHour()), (startTime.getMinute() + duration));
在我的时间课程中,我使用 compareTo
public class Time implements Comparable<Time> {
@Override
public int compareTo(Time time){
if (this.getHour() > time.getHour())
return 1;
else if (this.getHour() == time.getHour())
return 0;
else
return -1;
}
当我尝试在我的应用程序类中对 arrayList 进行排序时,我得到了
The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<Event>)
Collections.sort(events);
【问题讨论】:
-
Event是否实现了Comparable<Event>? (不清楚为什么Time类是相关的) -
我的事件应该实现
Comparable<Event>吗?它没有compareTo。compareTo位于Time类中。 -
是的。
Collections.sort怎么知道如何比较它的实例?它不知道对endTime进行排序,除非你告诉它。
标签: java sorting oop arraylist collections