Java:实现可比较问题

时间:2016-04-06 18:42:17

标签: java object arraylist override compareto

我是新来的,正在寻找有关我的代码的一些建议。我试图让arrayList ArrayList<patient> heartPatientArray = new ArrayList<patient>(); //heart patients能够在patient's arrivalTime上排序。

代码:

    class patient implements Comparable
{
    int typeSickness; //1 for heart 2 for gastro 3 for bleeding
    double arrivalTime; //what time they arrived   SORT BY
    int deathTime; // what time they are set to balk or die
    int status; //0 for being cared for, 1 to n for location in line, -1 if dead
    int personalNumberInLine; //personal number in line updated everytime someone enters the line
    double timeSpentInQueue; //total time before they either died or were able to be treated
    int idOfPerson; //unique id the person gets when entering the queue
    boolean isAlive;

    public patient(int sickness, double arrival, int ID) { //sets the patients sickness, time arrived, and ID
        typeSickness=sickness;
        arrivalTime=arrival;
        idOfPerson = ID;
    }
    public int getTypeSickness() {
        return typeSickness;
    }
    public void setTypeSickness(int typeSickness) {
        this.typeSickness = typeSickness;
    }
    public double getArrivalTime() {
        return arrivalTime;
    }
    public void setArrivalTime(double arrivalTime) {
        this.arrivalTime = arrivalTime;
    }
    public int getDeathTime() {
        return deathTime;
    }
    public void setDeathTime(int deathTime) {
        this.deathTime = deathTime;
    }

    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public int getNumberInLine() {
        return personalNumberInLine;
    }
    public void setNumberInLine(int numberInLine) {
        this.personalNumberInLine = numberInLine;
    }
    @Override
    public int compareTo(patient one) {
        if (this.getArrivalTime() < one.getArrivalTime()) {
            return -1;
        }
        else if(this.getArrivalTime() > one.getArrivalTime()){
            return 1;
        }

        return 0;
    }

some code edited out for time's sake

我在网上找到的一些代码之后尝试使用样式,但我在删除覆盖表示法的compareTo(patient one)上收到错误,class patient implements Comparable上的错误告诉我必须使课堂抽象化。

在我正确实现compareTo之后,我将如何对heartPatientArray进行排序?

3 个答案:

答案 0 :(得分:2)

您正在实施Comparable界面的原始形式。因此,compareTo需要Object,这就解释了为什么在尝试实现界面时出错。

相反,将您的类作为类型参数传递。

class patient implements Comparable<patient>

然后,您的compareTo方法将正确实施Comparable<patient>

通常,Java命名约定会说明您的类名称。

class Patient implements Comparable<Patient>

您可以使用以下方式对列表进行排序:

Collections.sort(heartPatientArray);

如果您想以相反的顺序对其进行排序,可以使用以下命令对其进行指定:

Collections.sort(heartPatientArray, Comparator.reverseOrder());

通常,您可以通过传递Comparator<Patient>的实例,实现Comparator的{​​{1}}方法对其进行排序,并注意将类型参数传递给{{1就像我们现在为compare做的那样。然后将Comparable的实例作为第二个参数传递给Comparable

答案 1 :(得分:0)

替换

class patient implements Comparable

class patient implements Comparable<patient>

否则您需要在班级中实施compareTo(Object o)

当您拥有Comparable课程的实例时,您只需将其添加到ArrayList并使用方法Collections.sort

答案 2 :(得分:0)

所以,基本上你想要的是能够比较2个patient个对象。现在的方式是,您可以在patient和其他对象之间进行比较。为了确保您只是比较patient个对象,您需要更改

class patient implements Comparable

class patient implements Comparable<patient>

你已经失败的方式没有任何错误(compareTo param除外)。当然,您可以将patient与其他对象进行比较。如果你想这样做,

@Override
public int compareTo(Object obj) {
    // you would want to see if the `Object` is of `patient` type
    if( !(obj instanceof patient) )
        return -1;    // `obj` IS NOT OF TYPE `patient`; PICK YOUR RETURN VALUE; IDEALLY THROW AN EXCEPTION
    patient one = (patient) o;
    if (this.getArrivalTime() < one.getArrivalTime()) {
        return -1;
    }
    else if(this.getArrivalTime() > one.getArrivalTime()){
        return 1;
    }

    return 0;
}

然后,您可以通过调用Collections.sort(..)或其他内容对其进行排序。