5

我必须按日期和时间订购约会。我有一个约会的 ArrayList,并试图创建一个比较器来比较他们的日期和时间。我正在尝试使用 Collections.sort 方法,将 ArrayList of Appointments 和我创建的 AppointmentComparator 传递给它。编译时我得到一个“没有合适的排序方法”。这是编译器生成的完整错误消息的链接:http: //prntscr.com/7y4qb

比较器:

public class AppointmentComparator implements Comparator<Appointment>
{
public int compare(Appointment a, Appointment b)
{
    if (a.getDay() < b.getDay())
        return -1;

    if (a.getDay() == b.getDay())
    {
        if (a.getStart() < b.getStart())
            return -1;
        if (a.getStart() > b.getStart())
            return 1;
        return 0;
    }

    return 1;
}

有语法错误的行:

Collections.sort(book, new AppointmentComparator());

变量 book 是约会的 ArrayList。ArrayList<Appointment>

预约书类:

import java.util.ArrayList;
import java.util.Collections;

public class AppointmentBook
{
private ArrayList<Appointment> book;

public AppointmentBook()
{
    book = new ArrayList<Appointment>();
}

public void addAppointment(Appointment appt)
{
    book.add(appt);
    Collections.sort(book, new AppointmentComparator());
}

public String printAppointments(int day)
{
    String list = "";

    for (int i = 0; i < book.size(); i++)
    {
        if (book.get(i).getDay() == day)
        {
            list = list + "Appointment description: " + book.get(i).getDescription() + "\n" + "Date of Appointment: " +
            book.get(i).getDay() + "\n" + "Time: " + book.get(i).getStart() + " - " + book.get(i).getEnd() + "\n" + "\n";
        }
    }

    return list;
}

预约班:

public class Appointment
{
private String desc;
private int day; //in format mmddyyyy
private int start; //in format hhmm
private int end; //in format hhmm

public Appointment(String description, int aptDay, int startTime, int endTime)
{
    desc = description;
    day = aptDay;
    start = startTime;
    end = endTime;
}

public String getDescription()
{
    return desc;
}

public int getDay()
{
    return day;
}

public int getStart()
{
    return start;
}

public int getEnd()
{
    return end;
}

}

4

4 回答 4

3

从错误消息中,您似乎忘记将比较器声明为实现接口:

public class AppointmentComparator implements Comparator<Appointment> {}

它需要具有实现部分,而不仅仅是包含方法。

于 2012-04-10T01:52:40.680 回答
1

您需要投射新的 AppointmentComparator

Collections.sort(book, new (Comparator)AppointmentComparator());
于 2013-10-10T15:07:19.910 回答
1

我们也可以在某些情况下使用内部类:

public int indexOfLargest(ArrayList<QuakeEntry> Data) {
    Comparator<QuakeEntry> cmtr = new Comparator<QuakeEntry>() {
        @Override
        public int compare(QuakeEntry t, QuakeEntry t1) {
            if (t.getMagnitude() < t1.getMagnitude())
                return -1;
            if (t.getMagnitude() == t1.getMagnitude()) 
                return 1;
            if (t1.getMagnitude() > t1.getMagnitude())
                return 0;
        return 1;
        }
    };

    QuakeEntry max = Collections.max(Data, cmtr);
    int maxIndex = Data.indexOf(max);
    //----------------------------------------------------------------------
    System.out.println("//---------------------------------------------------");
    System.out.println("ArrayList sorted by Magnitude using inner class with Comparator");
    System.out.println("//---------------------------------------------------");
    Collections.sort(Data, cmtr);
    for (QuakeEntry qe : Data) {
        System.out.println(qe);
    }


    return maxIndex;
}

所有类的代码: https ://github.com/Evegen55/Java_DukeEdu_Coursera_2/blob/master/Earthquakes_Programming%20and%20Interfaces/src/earthquakes_programming_and_interfaces/QuakeEntry.java

于 2015-12-29T10:50:55.903 回答
0

似乎您还没有实现 AppointmentComparator 的比较器接口

于 2012-04-10T01:55:18.510 回答