我想排序事件的结束时间。在我的Event类中的Application类endTime由我的Time类中的小时和分钟定义。错误使用Collections.sort
为我的活动课程添加了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));
在我的时间I类使用的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'实现可比''? (不清楚为什么“时间”类是相关的) –
我的事件应该实现“可比较的”吗?它没有'compareTo'。 'compareTo'位于'Time'类中。 –
ProgrammingBeginner24
是的。 'Collections.sort'还会怎样知道如何比较它的实例?除非你告诉'endTime',否则不知道如何排序。 –