Java 实现优先级队列辅助

Java 实现优先级队列辅助,java,priority-queue,Java,Priority Queue,这里的典型学生,为作业的最后一步寻找一些输入。非常感谢任何能为我指明正确方向的帮助或提示 我们的部分任务是实施急诊室优先队列。我一直关注的部分的细节如下: 编写一个名为PatientQueue的类,使用: a。默认值,无参数 建造师 b。两种公共方法: 我公共无效登记患者(患者p) 二,。公共患者getNextPatient() c。PriorityQueue和PatientCompariator的内部使用 以下是我目前掌握的情况: 在Patient.java中: package blah;

这里的典型学生,为作业的最后一步寻找一些输入。非常感谢任何能为我指明正确方向的帮助或提示

我们的部分任务是实施急诊室优先队列。我一直关注的部分的细节如下:

编写一个名为PatientQueue的类,使用:

a。默认值,无参数 建造师

b。两种公共方法:
我公共无效登记患者(患者p)
二,。公共患者getNextPatient()

c。PriorityQueue和PatientCompariator的内部使用

以下是我目前掌握的情况:

在Patient.java中:

package blah;

import java.util.Date;

public class Patient {

// data fields
protected String name;
protected int category;
protected Date timeArrived;

// accessors and mutators
public String getName() {
    return name;
}
public void setName(String nameIn) {
    this.name = nameIn;
}
public int getCategory() {
    return category;
}
public void setCategory(int categoryIn) {
    this.category = categoryIn;
}

public java.util.Date getTimeArrived() {
    return timeArrived;
}

// default constructor
public Patient() {
    this.name = "Default Name";
    this.category = 5; // unclassified Patients go to the end of the queue
    this.timeArrived = new Date();
}

// overloaded constructor
public Patient(String nameIn, int categoryIn) {
    this.name = nameIn;
    this.category = categoryIn;
    this.timeArrived = new Date();
}

} // end Patient class
在PatientComparator.java中:

package blah;

import java.util.Comparator;

public class PatientComparator implements Comparator<Patient> {

public int compare(Patient p1, Patient p2) {
    if (p1.getCategory() < p2.getCategory())
        return -1;
    if (p1.getCategory() > p2.getCategory())
        return 1;
    else { if (p1.getTimeArrived().before(p2.getTimeArrived()))
        return -1;
           if (p1.getTimeArrived().after(p2.getTimeArrived()))
        return 1;
    }
    return 0;
}

} // end PatientComparator class
我得到的错误是:

线程“main”java.lang.NullPointerException中出现异常

在第一次
doctorvisions()
调用main。我知道我缺少向列表中添加和/或从列表中删除对象的正确方法,但我看不到在
PatientQueue
类中需要发生什么才能真正触发用于添加或“获取”下一个患者的
PriorityQueue


再次感谢您的建议。谢谢

您的
PatientQueue
未使用
PatientComparator
。您应该使用超类的构造函数来注册它


您的
PatientQueue
不会将
Patient
对象添加到队列中。将它们添加到registerPatient方法中。

我甚至不会制作一个单独的比较器。您只需在Patient类上实现Comparable接口,并实现必要的compareTo方法。 快速实施:

@Override
public int compareTo(Object o) {
    Patient p = (Patient) o;
    if (this.getCategory() < p.getCategory())
        return -1;
    if (this.getCategory() > p.getCategory())
        return 1;
    else { if (this.getTimeArrived().before(p.getTimeArrived()))
        return -1;
        if (this.getTimeArrived().after(p.getTimeArrived()))
           return 1;
    }
    return 0;
} 
在EmergencyRoomSimulator类中,只需将字段声明更改为


PatientQueue pq=new PatientQueue()

为什么不在您的registerPatient方法中使用
pq.add(p)
,如果我只是在中添加了那一行,那么在该方法中“pq”就不会被识别(解析)。理论上我同意这更接近需要发生的事情,但我仍然在断开pq(主要)与注册患者的关系。谢谢你的回复!在您的第一条评论中,您是说我应该使用super类(PriorityQueue)的构造函数来创建优先级队列的新实例?与此类似:“PatientComparator pc;”作为一个数据字段?关于第二个语句:这正是我所处的位置。队列本身是在main方法中创建的,当我引用“pq”(在main中创建的对象)时,它不会解析。使用“this.add(p);”会导致NullPointerException。我的意思是您必须找到一种方法,为PriorityQueue提供比较器。由于您的PatientQueue扩展了PriorityQueue,您应该使用超级构造函数。谢谢。我将进行更多阅读,并找出如何将比较器传递给在main中创建的PriorityQueue。谢谢-正在创建的这4个类是在作业中指定的,因此我们的手在使用的类和层次结构方面有点卡滞。但是非常感谢您的回复。@Darkater我编辑了答案,也许这会更有帮助。这很好,除了作业的另一个要求是编写一个名为PatientComparator的类,该类实现用于比较两名患者的Comparator接口。a) 应首先看到类别编号较低的患者。b) 如果两名患者的分类相同,则应首先看到较早到达的患者“--因此,我不得不使用单独的类。但您的方式肯定更精简,更容易理解。同样,非常感谢这些提示。@Darkater您可以使用PatientComparator,而不是我在PatientQueue类中制作的比较器。据此编辑。
import java.util.Random; 

public class EmergencyRoomSimulator { 

private static final int WAIT_LIMIT = 3000; // 1000 = 1 second 

private PatientQueue pq = new PatientQueue(); 

private void t() { 
    try {  Thread.sleep(new Random().nextInt(WAIT_LIMIT));
    } catch (InterruptedException e) {
    } 
} // end t method

private void patientArrives(Patient p) { 
    pq.registerPatient(p); 
    System.out.println(" ARRIVAL: " + p.getName()); 
    System.out.println(" time arrived: " + p.getTimeArrived()); 
    System.out.println(" category: " + p.getCategory()); 
    System.out.println("------------------------------------"); 
    t(); 
} // end patientArrives method

private void doctorVisits() { 
    Patient p = pq.getNextPatient(); 
    System.out.println(" VISIT: " + p.getName()); 
    System.out.println(" time arrived: " + p.getTimeArrived()); 
    System.out.println(" category: " + p.getCategory()); 
    System.out.println("------------------------------------"); 
    t(); 
} // end doctorVisits method

private void simulate() { 
    System.out.println("------------------------------------"); 
    System.out.println("ER OPEN"); 
    System.out.println("------------------------------------"); 
    patientArrives(new Patient("John Paul Jones", 3)); 
    patientArrives(new Patient("Thomas Paine", 1)); 
    patientArrives(new Patient("Joseph Brant", 2)); 
    doctorVisits(); 
    patientArrives(new Patient("Ethan Allen", 2)); 
    patientArrives(new Patient("Henry Knox", 4)); 
    patientArrives(new Patient("Patrick Henry", 2)); 
    doctorVisits(); 
    doctorVisits(); 
    patientArrives(new Patient("Mary Draper", 1)); 
    patientArrives(new Patient("Samuel Adams", 3)); 
    doctorVisits(); 
    doctorVisits(); 
    doctorVisits(); 
    doctorVisits(); 
    doctorVisits(); 
    System.out.println("------------------------------------"); 
    System.out.println("ER CLOSED"); 
    System.out.println("------------------------------------"); 
} // end simulate method

public static void main(String[] args) { 
    EmergencyRoomSimulator er = new EmergencyRoomSimulator(); 
    er.simulate(); 
} // end main 

} // end class
@Override
public int compareTo(Object o) {
    Patient p = (Patient) o;
    if (this.getCategory() < p.getCategory())
        return -1;
    if (this.getCategory() > p.getCategory())
        return 1;
    else { if (this.getTimeArrived().before(p.getTimeArrived()))
        return -1;
        if (this.getTimeArrived().after(p.getTimeArrived()))
           return 1;
    }
    return 0;
} 
import java.util.Comparator;
import java.util.PriorityQueue;

public class PatientQueue {
    PriorityQueue pq;

    // default constructor
    public PatientQueue() {
        this.pq = new PriorityQueue<Patient>(1, new PatientComparator());
    }

    public void registerPatient(Patient p) {
        this.pq.add(p);
    } // end registerPatient method

    public Patient getNextPatient() {
        return (Patient) this.pq.poll();
    } // end getNextPatient method

} // end PatientQueue class