java中返回null的优先级以及如何对其使用Compariable

java中返回null的优先级以及如何对其使用Compariable,java,priority-queue,Java,Priority Queue,我是第一次使用priorityQue,我不明白为什么在打印它时它会返回null。这是一个分配,我正在做一个简单的菜单来存储显示和删除(只是头值)。还有,如果有人能解释比较类是如何排列优先级的,因为我很确定我做错了。谢谢你的帮助 主类 import java.util.PriorityQueue; import java.util.Scanner; public class PatientManager{ Patient p =new Patient(0, null, 0);

我是第一次使用priorityQue,我不明白为什么在打印它时它会返回null。这是一个分配,我正在做一个简单的菜单来存储显示和删除(只是头值)。还有,如果有人能解释比较类是如何排列优先级的,因为我很确定我做错了。谢谢你的帮助

主类

import java.util.PriorityQueue;
import java.util.Scanner;

public class  PatientManager{
    Patient p =new Patient(0, null, 0);

    PriorityQueue<Patient> waitingList= new PriorityQueue<Patient>();
    int order=0; 

    public static void main(String[] args){
        PatientManager pm=new PatientManager();
        pm.start();
    }
    void start(){
        boolean go=true;
        try{
            while(go){

                System.out.println("------------------");
                System.out.println("(1) New Patient.");
                System.out.println("(2) Next Patient.");
                System.out.println("(3) Waiting List.");
                System.out.println("(4) Exit.");
                System.out.println("------------------");
                System.out.print("Chose an item from the menu: ");
                Scanner input=new Scanner(System.in);
                int ask= input.nextInt();
                switch (ask) {
                case 1:  optionOne();
                         break;
                case 2:  optionTwo();
                         break;
                case 3:  optionThree();
                         break;
                case 4:  go=false;
                         break;
                default: System.out.print("(x) Wrong choice: ");
                         break;
                }
            }
        }catch(Exception e){
            System.out.print("(x) Wrong choice");
            e.printStackTrace();
            start();
        }
    }
    void optionOne(){
        System.out.print("Enter Patients name ");
        Scanner reader=new Scanner(System.in);
        String name= reader.next();
        p.setName(name);
        System.out.print("Enter Emergency[1 (low) to 5 (life-and-death)] ");
        boolean a=true;
        while(a){
            try{
                int b=reader.nextInt();
                if(1<=b && b<=5){
                    p.setEmergency(b);
                    a=false;
                }else System.out.print("(x) Wrong choice: ");
            }catch(Exception e){
                System.out.print("(x) Wrong choice");
                a=false;
            }
        }
        System.out.println(waitingList.add(new Patient(order,p.getName(),p.getEmergency())));
        System.out.println(waitingList.poll());
        order++;
    }
    void optionTwo(){
        System.out.println(waitingList.peek());
        waitingList.remove();
    }
    void optionThree(){
        System.out.println(waitingList.peek());
    }
}
import java.util.Comparator;

public class Patient implements Comparable<Patient>{
    //attributes
    private String name;
    private int order;      //order of arrival
    private int emergency; //1 is normal, 5 is life-and-death situation

    //constructor
    public Patient(int order, String name, int priority) {
        this.order = order;
        this.name = name;
        this.emergency = priority;
    }

    //getters and setters
    public int getOrder() {
        return order;
    }
    public void setOrder(int order) {
        this.order = order;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getEmergency() {
        return emergency;
    }

    public void setEmergency(int emergency) {
        this.emergency = emergency;
    }

    public String toString() {
        return name;
    }
    public int compareTo(Patient a){
        if(emergency!=a.emergency && emergency>a.emergency) return emergency;
        else if(emergency!=a.emergency && emergency<a.emergency) return a.emergency;
        else if(emergency==a.emergency){
            if(order<a.order)return order;
            else return a.order;
        }
        return emergency=0;
    }
}
import java.util.PriorityQueue;
导入java.util.Scanner;
公共类PatientManager{
患者p=新患者(0,空,0);
PriorityQueue waitingList=新建PriorityQueue();
整数阶=0;
公共静态void main(字符串[]args){
PatientManager pm=新的PatientManager();
pm.start();
}
void start(){
布尔go=true;
试一试{
while(go){
System.out.println(“------------------------”;
System.out.println(“(1)新患者”);
System.out.println(“(2)下一位患者”);
System.out.println(“(3)等待列表”);
System.out.println(“(4)退出”);
System.out.println(“------------------------”;
System.out.print(“从菜单中选择一项:”);
扫描仪输入=新扫描仪(System.in);
int ask=input.nextInt();
开关(ask){
案例1:optionOne();
打破
案例2:optionTwo();
打破
案例3:OptionTree();
打破
案例4:go=false;
打破
默认值:System.out.print(“(x)错误选择:”);
打破
}
}
}捕获(例外e){
系统输出打印(“(x)错误选择”);
e、 printStackTrace();
start();
}
}
void optionOne(){
系统输出打印(“输入患者姓名”);
扫描仪阅读器=新扫描仪(System.in);
字符串名称=reader.next();
p、 集合名(名称);
系统输出打印(“进入紧急状态[1(低)至5(生死)]”;
布尔值a=真;
while(a){
试一试{
int b=reader.nextInt();

如果(1对于可比较的,您需要执行以下操作:

如果相同,则0=>相同的紧急情况

如果当前>秒=1当前患者具有更高的优先级/紧急情况

如果当前<秒=-1当前患者优先级较低/紧急

代码:

 public int compareTo(Patient a){
    if(emergency==a.emergency)
        return 0;       
    else if(emergency>a.emergency)
        return 1;
    else
        return -1;          
}
您是否真的希望在optionOne()中执行下面的操作。获取null的原因是,一旦将其添加到队列中,您就会在下一行中将其删除

  System.out.println(waitingList.add(new Patient(order,p.getName(),p.getEmergency())));
  System.out.println(waitingList.poll()); //poll() Retrieves and removes the head of this queue, or returns null if this queue is empty.

再次阅读poll正在做的事情,因为我认为您误解了。

查看文档:特别注意您正在使用的方法。啊,我看到poll删除并阅读了它,peek just readsim在可比方面仍然存在问题,尽管这
return emergency=0;
是错误的,您将0分配给emerg在返回之前,它应该是
紧急情况
0
哦,我知道我必须重新编写该方法,使其变得不那么有趣…但我看到你的点菜鸟错误非常感谢你的帮助Yeah Jorge Campos在评论中帮助了我,但这对以后的使用很好