Java如何在更新值时在arraylist中维护扫描对象的序列

Java如何在更新值时在arraylist中维护扫描对象的序列,java,sorting,arraylist,collections,Java,Sorting,Arraylist,Collections,我有一个包含患者姓名和紧急级别的类,它实现了comparable: class Patient implements Comparable<Patient>{ private String name; private int emergencyLvl; public Patient(String name, int emergencyLvl) { this.name = name; this.emergencyLvl

我有一个包含患者姓名和紧急级别的类,它实现了comparable:

class Patient implements Comparable<Patient>{
    private String name;
    private int emergencyLvl;

    public Patient(String name, int emergencyLvl)
    {
        this.name = name;
        this.emergencyLvl = emergencyLvl;

    }

    public String getName(){
        return this.name;

    }
    public int getEmergencyLvl(){
        return this.emergencyLvl;

    }
    public  void  setEmergencyLvl(int updateLvl){
        this.emergencyLvl = updateLvl;

    }
    public void setName(String newName){
        this.name = newName;

    }
    public int compareTo(Patient p){

        int compareEmergency = ((Patient) p).getEmergencyLvl();

        return compareEmergency - this.emergencyLvl;

    **//I believe my error comes from here**




        }
}
*3=查询数量,0=要添加到arraylist的新患者,1=更新紧急级别(示例30+30=60)。2=从降序和名称打印紧急级别**

然而,我的程序成功地扫描并将新患者正确地添加到arraylist中,但每当它需要更新紧急级别时,顺序就错了

例如,在更新david紧急级别之前。顺序为(5月60日,david 30日) 但在更新大卫紧急级别后,因为大卫首先到达,所以正确的顺序应该是(大卫60,5月60)

不幸的是,我无法正确更新序列。 这是我的主要节目:

UpdateEmergencyLvl:

void UpdateEmergencyLvl(String patientName, int incEmergencyLvl) {

        for(Patient p: a1){
            if(p.getName().equals(patientName)){
                int newEmergencyLvl = p.getEmergencyLvl() +
                incEmergencyLvl;
                p.setEmergencyLvl(newEmergencyLvl);

                }

            }

            Collections.sort(a1);

    }

首先,您需要为类Patient添加一个新属性,新的公共Patient应如下所示:

public Patient(String name, int emergencyLvl, int patIndx)
        {
            this.name = name;
            this.emergencyLvl = emergencyLvl;
            this.patIndx = patIndx;

        }
完成后,为索引创建一个getter;在你的阅读方法中,你可以在这个索引中添加一个int。对于比较方法,由于到达索引比共享相同的emergencyLvl更重要,我会这样做:

public int compareTo(Patient p){

        int compareEmergency = ((Patient) p).getEmergencyLvl();

        int aux= compareEmergency - this.emergencyLvl;

        int compareIndex= ((Patient) p).getIndex();
        if(compareIndex>this.patIndx) aux=compareIndex-this.patIndx;
        return aux;    



        }
使用的扫描仪:

 void run() throws Exception {


    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    int numCMD = Integer.parseInt(br.readLine()); // note that numCMD is >= N
    int index=0;//starting index at 0
    while (numCMD-- > 0) {
      StringTokenizer st = new StringTokenizer(br.readLine());
      index++;
      int command = Integer.parseInt(st.nextToken());
      switch (command) {
        case 0: ArriveAtHospital(st.nextToken(), Integer.parseInt(st.nextToken())); 
               // you will need a index setter for this, assuming this is the moment when you add the patient to the patient list:
               ArriveAtHospìtalOrder(index);//as I cant see how the method ArriveAtHospital works, I showed you a possible method that takes the index.
               break;
        case 1: UpdateEmergencyLvl(st.nextToken(), Integer.parseInt(st.nextToken())); break;

        case 3: pr.println(Query()); break;
      }


    }
    pr.close();

这是一种可能的方法,ArriveAtHospitalIndex只是为了说明,因为我不知道如何将参数传递给ArriveAtHospital方法,这只是为了让您知道何时应该传递索引。您对这些行有一些评论。

首先,您需要为类Patient添加一个新属性,您的新公共Patient应该如下所示:

public Patient(String name, int emergencyLvl, int patIndx)
        {
            this.name = name;
            this.emergencyLvl = emergencyLvl;
            this.patIndx = patIndx;

        }
完成后,为索引创建一个getter;在你的阅读方法中,你可以在这个索引中添加一个int。对于比较方法,由于到达索引比共享相同的emergencyLvl更重要,我会这样做:

public int compareTo(Patient p){

        int compareEmergency = ((Patient) p).getEmergencyLvl();

        int aux= compareEmergency - this.emergencyLvl;

        int compareIndex= ((Patient) p).getIndex();
        if(compareIndex>this.patIndx) aux=compareIndex-this.patIndx;
        return aux;    



        }
使用的扫描仪:

 void run() throws Exception {


    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    int numCMD = Integer.parseInt(br.readLine()); // note that numCMD is >= N
    int index=0;//starting index at 0
    while (numCMD-- > 0) {
      StringTokenizer st = new StringTokenizer(br.readLine());
      index++;
      int command = Integer.parseInt(st.nextToken());
      switch (command) {
        case 0: ArriveAtHospital(st.nextToken(), Integer.parseInt(st.nextToken())); 
               // you will need a index setter for this, assuming this is the moment when you add the patient to the patient list:
               ArriveAtHospìtalOrder(index);//as I cant see how the method ArriveAtHospital works, I showed you a possible method that takes the index.
               break;
        case 1: UpdateEmergencyLvl(st.nextToken(), Integer.parseInt(st.nextToken())); break;

        case 3: pr.println(Query()); break;
      }


    }
    pr.close();

这是一种可能的方法,ArriveAtHospitalIndex只是为了说明,因为我不知道如何将参数传递给ArriveAtHospital方法,这只是为了让您知道何时应该传递索引。您对这些行有一点意见。

无需在比较中对患者进行p运算-根据SignatureEyes的方法,它已经是一个患者,但这并不能解决我的问题,因为我想确定首先出现的患者顺序是正确的。因为在我的情况下,大卫是第一位,但紧急情况会在以后更新。但是,更新后的紧急级别与5月相同。但是David是第一位的,所以David应该先打印出来,而不是May。也许添加一个到达索引可以避免这个问题。例如,获取输入文件的行作为索引。当compareTo返回0时,所比较的两个元素的顺序保持不变。-Collections.sort(List)
此排序保证稳定:相等的元素不会因排序而重新排序如果使用bufferedReader添加行,只需在Bucket前创建一个int,例如int index=0,用于读取行,并在读取Bucket中的每行后执行index+,然后将该索引添加到用于患者的信息中,并添加一个新属性,例如,您有name和emergencyclvl,您可以添加一个新的arrivalIndx属性来存储该属性。无需在compareTo中对患者强制转换p-根据SignatureEyes方法,它已经是一个患者,但这并不能解决我的问题,因为我希望首先输入的患者顺序是正确的。因为在我的情况下,大卫是第一位,但紧急情况会在以后更新。但是,更新后的紧急级别与5月相同。但是David是第一位的,所以David应该先打印出来,而不是May。也许添加一个到达索引可以避免这个问题。例如,获取输入文件的行作为索引。当compareTo返回0时,所比较的两个元素的顺序保持不变。-Collections.sort(List)
此排序保证稳定:相等的元素不会因排序而重新排序如果使用bufferedReader添加行,只需在Bucket前创建一个int,例如int index=0,用于读取行,并在读取Bucket中的每行后执行index+,然后将该索引添加到用于患者的信息中,并添加一个新属性,例如,您有name和emergencyclvl,您可以添加一个新的arrivalIndx属性来存储该属性。