【发布时间】:2017-04-18 14:03:18
【问题描述】:
这几天我遇到了麻烦,无法解决。我有一个可以存储的arrayList。
public class Application {
Time startTime = new Time(hour,minute);
Time endTime = new Time((startTime.getHour()), (startTime.getMinute() + duration));
Event e1 = new Event("Lecture", title, startTime, endTime);
我想根据我的 endTime 对我的事件进行排序,我遇到的问题是 endTime 是基于 startTime 的,它使用我的 Time 课程中的小时和分钟。当我尝试使用compareTo 时,我感到很困惑。我把它放在我的Time 类中,但后来意识到它必须在我的Event 类中才能比较实例。我将如何使用 compareTo 来比较 endTimes,以及如何输出排序。当我把 compareTo 放在我的事件中时,我得到了
The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<Event>)
Collections.sort(events);
public Event(String type, String title, Time startTime, Time endTime) {
this.type = type;
this.title = title;
this.startTime = startTime;
this.endTime = endTime;
}
private int minute;
public Time(int hour, int minute) {
this.minute = hour * 60 + minute;
}
我也可以extend 一个类,或者我必须实现它才能使用compareTo
【问题讨论】:
标签: java sorting oop arraylist compareto