Java “试图解决”;类型集合中的方法排序(List<;T>;,Comparator<;?super T>;)不适用于参数";

Java “试图解决”;类型集合中的方法排序(List<;T>;,Comparator<;?super T>;)不适用于参数";,java,comparator,Java,Comparator,我有一个任务,我必须根据紧急情况对患者进行排序,如果他们有相同的紧急情况级别,则根据到达顺序进行排序,但是当我进行排序时,我收到一个错误,说“类型集合中的方法排序(列表,比较器)不适用于参数(列表,比较器)”,有人能帮忙吗?以下是我的全部代码: import java.util.Comparator; public class Patient implements Comparable<Patient> { // attributes private String name; pri

我有一个任务,我必须根据紧急情况对患者进行排序,如果他们有相同的紧急情况级别,则根据到达顺序进行排序,但是当我进行排序时,我收到一个错误,说“类型集合中的方法排序(列表,比较器)不适用于参数(列表,比较器)”,有人能帮忙吗?以下是我的全部代码:

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;
}

@Override
public int compareTo(Patient patient) {
    // TODO Auto-generated method stub
    int total = 0;
    if (emergency < patient.emergency) {// compares patient emergency
        total = 1;
    } else if (emergency == patient.emergency) {
        if (order < patient.order) {// compares order of arrival in case emergencies are the same
            total = -1;
        } else if (order > patient.order) {
            total = 1;
        }
    } else if (emergency > patient.emergency) {
        total = -1;
    }
    return total;
}

public static class Comparators {
    public static final Comparator<Patient> EMERGENCY = (Patient p1, Patient p2) -> Integer.compare(p1.getEmergency(), p2.getEmergency());
    public static final Comparator<Patient> ORDER = (Patient p1, Patient p2) -> Integer.compare(p1.getOrder(), p2.getOrder());

}}




import java.util.*;
public class PatientManager{
private PriorityQueue<Patient> waitingList;

public PatientManager() {
    waitingList = new PriorityQueue<Patient>();
}

// Starter method
public void start() {
    String choice;
    Scanner in = new Scanner(System.in);
    String name;
    int emergency = 0;
    int order = 0;
    List<Object> list = null;
    Patient patient;
    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("-------------------------------");

    while (true) {
        //clear screen
        System.out.print("* Choose an item from the menu: ");
        choice = in.next();
        switch (choice) {
        case "1":
            System.out.print("Enter patient's name: ");
            name = in.next();
            System.out.print("Enter emergency [1 (low) to 5 (life-and-death)]: ");

            while (emergency < 1 || emergency > 5) {//testing for emergency and if any wrong values are given clears the scanner and tries again
                try {
                    emergency = in.nextInt();
                    if (emergency < 1 || emergency > 5)
                        System.out.print("(x) Wrong value. Try again: ");
                }
                catch (Exception e) {
                    System.out.print("(x) Wrong value. Try again: ");
                    in.next();
                }
            }
            order++;//Sets the number of the arrival.
            patient = new Patient(order, name, emergency);
            waitingList.add(patient);
            System.out.println("Patient added to the waiting list.");
            emergency = 0;//resetting value of emergency otherwise all patients will have the same one and loop will not run a second time
            Arrays.sort(waitingList.toArray());
            break;
        case "2":
            if (waitingList.isEmpty()) {
                System.out.println("No more patients.");
            } else {
                patient = waitingList.remove();
                System.out.println(patient.getName() + " is treated.");
            }
            break;
        case "3":
            if (waitingList.size() == 0) {
                System.out.println("No patients in the list.");
            } else {
                System.out.println("Waiting list includes:");
                list = new ArrayList<Object>(waitingList);
                Collections.sort(list, Patient.Comparators.EMERGENCY);
                for (int i=0;i<waitingList.size();i++) {
                    System.out.println("- " + list.get(i));
                }
            }
            break;
        case "4":
            System.out.println("Program terminated. Good bye!!");
            in.close();
            System.exit(0);
            break;
        // print list name and emergency
        default:
            System.out.println("(x) Wrong choice.");
            break;
        }
    }
}}
import java.util.Comparator;
公共类病人{
//属性
私有字符串名称;
private int order;//到达顺序
私人int紧急;//1为正常,5为生死情况
//建造师
公共患者(整数顺序、字符串名称、整数优先级){
这个。顺序=顺序;
this.name=名称;
紧急情况=优先;
}
//接球手和接球手
public int getOrder(){
退货单;
}
公共无效设置顺序(整数顺序){
这个。顺序=顺序;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共紧急情况{
紧急返回;
}
公共紧急情况(国际紧急情况){
这是紧急情况;
}
公共字符串toString(){
返回名称;
}
@凌驾
公共内部比较(患者){
//TODO自动生成的方法存根
int-total=0;
if(急诊<病人急诊){//比较病人急诊
总数=1;
}否则如果(紧急情况==患者紧急情况){
如果(医嘱<病人医嘱){//比较紧急情况下的到达顺序相同
总数=-1;
}否则如果(医嘱>患者医嘱){
总数=1;
}
}否则,如果(紧急>患者紧急){
总数=-1;
}
返回总数;
}
公共静态类比较器{
公共静态最终比较器EMERGENCY=(患者p1,患者p2)->Integer.compare(p1.getEmergency(),p2.getEmergency());
公共静态最终比较器顺序=(患者p1,患者p2)->Integer.compare(p1.getOrder(),p2.getOrder());
}}
导入java.util.*;
公共类PatientManager{
私有优先级队列等待列表;
公共病人管理者(){
waitingList=new PriorityQueue();
}
//起动器法
公开作废开始(){
字符串选择;
扫描仪输入=新扫描仪(系统输入);
字符串名;
int紧急情况=0;
整数阶=0;
List=null;
病人;
System.out.println(“------------------------------------”;
System.out.println(“(1)新患者”);
System.out.println(“(2)下一位患者”);
System.out.println(“(3)等待列表”);
System.out.println(“(4)退出”);
System.out.println(“------------------------------------”;
while(true){
//清屏
System.out.print(“*从菜单中选择一项:”);
choice=in.next();
开关(选择){
案例“1”:
系统输出打印(“输入患者姓名:”);
name=in.next();
系统输出打印(“进入紧急状态[1(低)至5(生死)]:”;
而(紧急情况<1 | |紧急情况>5){//测试紧急情况,如果给出任何错误值,则清除扫描仪并重试
试一试{
紧急情况=in.nextInt();
如果(紧急情况<1 | |紧急情况>5)
System.out.print(“(x)值错误。请重试:”;
}
捕获(例外e){
System.out.print(“(x)值错误。请重试:”;
in.next();
}
}
order++;//设置到达的次数。
患者=新患者(医嘱、姓名、急诊);
等待列表。添加(患者);
System.out.println(“患者已添加到等待名单”);
emergency=0;//重置emergency的值,否则所有患者都有相同的值,循环不会再次运行
sort(waitingList.toArray());
打破
案例“2”:
if(waitingList.isEmpty()){
System.out.println(“不再有患者”);
}否则{
patient=waitingList.remove();
System.out.println(patient.getName()+“正在治疗”);
}
打破
案例“3”:
if(waitingList.size()==0){
System.out.println(“列表中没有患者”);
}否则{
System.out.println(“等待列表包括:”);
列表=新的ArrayList(等待列表);
收集。分类(列表、患者、比较者、紧急情况);

对于(int i=0;i而言,问题在于您正在排序的列表已声明为可包含任意对象的列表。您不能将患者比较器应用于可能不是患者的对象

要解决此问题,请将您的列表声明为患者列表:

List<Patient> list = new ArrayList<>(waitingList)
List List=newarraylist(waitingList)