使用Comparator命令ArrayList Java

时间:2012-04-10 01:48:16

标签: java sorting arraylist comparator

我必须按日期和时间订购约会。我有一个约会的ArrayList,并试图创建一个比较器来比较他们的日期和时间。我正在尝试使用Collections.sort方法,将其传递给Appointments的ArrayList和我创建的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>

AppointmentBook类:

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 个答案:

答案 0 :(得分:3)

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

public class AppointmentComparator implements Comparator<Appointment> {}

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

答案 1 :(得分:1)

您需要投射新的约会比较器

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

答案 2 :(得分: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

答案 3 :(得分:0)

好像你没有实现AppointmentComparator的比较器接口