Priority Que 在 java 中返回 null 以及如何使用它的 comparable
Priority Que returning null in java and how to use comparable for it
我是第一次使用 priorityQue,我很困惑为什么当我尝试打印它时它返回 null。这是一个作业,我正在制作一个简单的菜单来存储显示和删除(只是 head 值)。另外,如果有人可以解释可比较的 class 如何对 priorityQue 进行排序,因为我很确定我做错了。感谢任何帮助
主要Class
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());
}
}
getter setter class
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;
}
}
对于 Comparable,您需要这样做:
如果相同则 0 => 相同的紧急情况
if current > second = 1 current patient has higher priority/emergency
if current < second = -1 current patient has lower priority/emergency
在代码中:
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.
再读一遍民意调查的内容,因为我认为你误解了。
我是第一次使用 priorityQue,我很困惑为什么当我尝试打印它时它返回 null。这是一个作业,我正在制作一个简单的菜单来存储显示和删除(只是 head 值)。另外,如果有人可以解释可比较的 class 如何对 priorityQue 进行排序,因为我很确定我做错了。感谢任何帮助
主要Class
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());
}
}
getter setter class
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;
}
}
对于 Comparable,您需要这样做:
如果相同则 0 => 相同的紧急情况
if current > second = 1 current patient has higher priority/emergency
if current < second = -1 current patient has lower priority/emergency
在代码中:
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.
再读一遍民意调查的内容,因为我认为你误解了。