Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中无法正确排序到达时间_Java_Sorting_Linked List - Fatal编程技术网

在Java中无法正确排序到达时间

在Java中无法正确排序到达时间,java,sorting,linked-list,Java,Sorting,Linked List,向大家问好。我目前正在做一个项目,对患者的急诊人数进行分类(护士在患者进入急诊室时分配的人数,这个数字也决定了患者病情的严重程度)。但是,如果有1名以上的患者持有相同的急救号码(例如:2名患者持有急救号码1),则较早到达的患者应首先接受治疗。由于这个原因,我有两种排序,一种是按升序排列紧急号码,另一种是按升序排列时间。但不幸的是,第二次分拣无法正常工作。以下是紧急号码类型的说明: 紧急电话:1-立即危及生命 紧急电话:2-紧急,但不立即危及生命 紧急电话:3-不太紧急 现在是编码部分(请注意,这

向大家问好。我目前正在做一个项目,对患者的急诊人数进行分类(护士在患者进入急诊室时分配的人数,这个数字也决定了患者病情的严重程度)。但是,如果有1名以上的患者持有相同的急救号码(例如:2名患者持有急救号码1),则较早到达的患者应首先接受治疗。由于这个原因,我有两种排序,一种是按升序排列紧急号码,另一种是按升序排列时间。但不幸的是,第二次分拣无法正常工作。以下是紧急号码类型的说明:

紧急电话:1-立即危及生命 紧急电话:2-紧急,但不立即危及生命 紧急电话:3-不太紧急

现在是编码部分(请注意,这是一个linkedlist)

接口:

public interface ListInterface<T> {

  public boolean add(T newEntry);

  public boolean add(int newPosition, T newEntry);

  public T remove(int givenPosition);

  public void clear();

  public boolean replace(int givenPosition, T newEntry);

  public T getEntry(int givenPosition);

  public boolean contains(T anEntry);

  public int getLength();

  public boolean isEmpty();

  public boolean isFull();
} 
EmergencyCmp(Comparator)-->用于排序患者的紧急编号

import java.util.Comparator;

public class EmergencyCmp implements Comparator<Patient>
{
    @Override
    public int compare(Patient p1, Patient p2) 
    {
        if(p1.getEmergencyNo() > p2.getEmergencyNo())
        {
            return 1;
        } 

        else
        {
            return -1;
        }
     }
}
import java.util.Comparator;

public class QueueCmp implements Comparator<Patient>
{
    @Override
    public int compare(Patient p1, Patient p2) 
    {
        if(p1.getQueueTime() > p2.getQueueTime())
        {
             return 1;
        } 

        else
        {
             return -1;
        }
    }   
}
当大家看到注释时,这意味着前面的代码没有问题,大家可以跳过它,下面是我之前得到的结果的附件:

所以,从图中你们都可以看到,到达时间的排序是不正确的。我希望我能知道为什么会出现这个问题,因为我自己无法解决它。首先感谢大家

因此,在采纳@Scott Hunter的建议后,我对EmergencyCmp进行了以下修改:

    @Override
    public int compare(Patient p1, Patient p2) 
    {
        int value = 0;

        if(p1.getEmergencyNo() > p2.getEmergencyNo())
        {
            value = 1;
        } 

        else if(p1.getEmergencyNo() < p2.getEmergencyNo())
        {
            value = -1;
        }

        else if(p1.getEmergencyNo() == p2.getEmergencyNo())
        {
            if(p1.getQueueTime() > p2.getQueueTime())
            {
                 return 1;
            } 

            else
            {
                 return -1;
            }
        }

        return value;
     }
@覆盖
公共整数比较(患者p1、患者p2)
{
int值=0;
如果(p1.getEmergencyNo()>p2.getEmergencyNo())
{
数值=1;
} 
else if(p1.getEmergencyNo()p2.getQueueTime())
{
返回1;
} 
其他的
{
返回-1;
}
}
返回值;
}

但是,时间排序仍然会产生错误的结果。

据我所知(我可能不这么认为;您提供了许多无关的内容),看起来您正在尝试执行两个不同的排序,一个接一个,这样第二个排序将取消第一个排序的工作。相反,您应该定义一个
比较器
,该比较器比较紧急号码,并且仅当它们相同时,才比较到达时间。

我刚才做了一些修改,但它显示了相同的结果。您声称已更改
EmergencyCmp
,但
main
正在使用
QueueCmp
import java.util.Comparator;

public class QueueCmp implements Comparator<Patient>
{
    @Override
    public int compare(Patient p1, Patient p2) 
    {
        if(p1.getQueueTime() > p2.getQueueTime())
        {
             return 1;
        } 

        else
        {
             return -1;
        }
    }   
}
import java.util.Calendar;
import java.util.Scanner;
import java.util.Arrays;
import java.util.*; 

public class DSA {

    public DSA() {
    }

    public static void main(String[] args) {

        //patient's attributes
        int emergencyNo;
        int queueTime;
        String patientName;
        String patientIC;
        String patientGender;
        String patientTelNo;
        String patientAdd;
        String visitDate;

        //counter
        int j = 0;
        int x = 0;
        int y = 0;
        int z = 0;
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int countEnteredPatient = 1;
        int totalCount = 0;

        //calendar
        int nowYr, nowMn, nowDy, nowHr, nowMt, nowSc;

        //others
        boolean enterNewPatient = true;
        String continueInput;
        boolean enterNewPatient1 = true;
        String continueInput1;
        boolean continueEmergencyNo;

        Scanner scan = new Scanner(System.in);
        ListInterface<Patient> patientList = new LList<Patient>();
        ListInterface<Patient> newPatientList = new LList<Patient>();
        Patient[] patientArr1 = new Patient[10000];
        Patient[] patientArr2  = new Patient[10000];
        Patient[] patientArr3  = new Patient[10000];
        Patient tempoPatient;

        do{
            //do-while loop for entering new patient details after viewing patient list

            System.out.println("Welcome to Hospital Ten Stars!\n");

            do{
                //do-while loop for entering new patient details
                System.out.println("Entering details of patient " + countEnteredPatient);
                System.out.println("===================================\n");

                Calendar calendar = Calendar.getInstance();
                nowYr = calendar.get(Calendar.YEAR);
                nowMn = calendar.get(Calendar.MONTH);
                nowDy = calendar.get(Calendar.DAY_OF_MONTH);
                nowHr = calendar.get(Calendar.HOUR);
                nowMt = calendar.get(Calendar.MINUTE);
                nowSc = calendar.get(Calendar.SECOND);
                queueTime = calendar.get(Calendar.MILLISECOND);

                visitDate = nowDy + "/" + nowMn + "/" + nowYr + ", " + nowHr + ":" + nowMt + ":" + nowSc;

                //input emergency number
                do{
                    tempoPatient = new Patient();

                    continueEmergencyNo = false;
                    int EmergencyNoOption;

                    try
                    {
                        do{
                            System.out.print("Please select 1 – Immediately life threatening, 2 – Urgent, but not immediately life threatening or 3 – Less urgent(Eg: 1) : ");
                            EmergencyNoOption = scan.nextInt();
                            scan.nextLine();
                            System.out.print("\n");

                          }while(tempoPatient.setEmergencyNo(EmergencyNoOption) == false);
                    }

                    catch(InputMismatchException ex)
                    {
                        System.out.print("\n");
                        System.out.println("Invalid input detected.");
                        scan.nextLine();
                        System.out.print("\n");

                        continueEmergencyNo = true;
                    }
                  }while(continueEmergencyNo);

                  //input patient name
                  do{
                        System.out.print("Patient name(Eg: Christine Redfield) : ");
                        patientName = scan.nextLine();
                        System.out.print("\n");
                    }while(tempoPatient.setPatientName(patientName) == false);

                  //input patient ic no
                  do{
                        System.out.print("Patient IC number(Eg: 931231124567) : ");
                        patientIC = scan.nextLine();
                        System.out.print("\n");
                     }while(tempoPatient.setPatientIC(patientIC) == false);

                  //input patient gender    
                  do{
                        System.out.print("Patient gender(Eg: M) : ");
                        patientGender = scan.nextLine();
                        System.out.print("\n");
                    }while(tempoPatient.setPatientGender(patientGender) == false);

                  //input patient tel. no
                  do{
                        System.out.print("Patient tel.No(without'-')(Eg: 0162345678/0342980123) : ");
                        patientTelNo = scan.nextLine();
                        System.out.print("\n");
                    }while(tempoPatient.setPatientTelNo(patientTelNo) == false);

                  //input patient address
                  do{
                        System.out.print("Patient address(Eg: 4-C9 Jln Besar 123, Taman Besar, 56000 Kuala Lumpur) : ");
                        patientAdd = scan.nextLine();
                        System.out.print("\n");
                    }while(tempoPatient.setPatientAdd(patientAdd) == false);

                  tempoPatient.setQueueTime(queueTime);
                  tempoPatient.setVisitDate(visitDate);             
                  patientList.add(tempoPatient);

                  //decide whether want to enter a new patient or not   
                  do{
                        System.out.print("Do you want to enter another new patient?(Eg: Y/N) : ");
                        continueInput = scan.nextLine();

                        if(continueInput.equals("Y") || continueInput.equals("y"))
                        {
                            enterNewPatient = true;

                            System.out.print("\n");
                        }

                        else if(continueInput.equals("N") || continueInput.equals("n"))
                        {
                            enterNewPatient = false;
                        }

                        else
                        {   
                            System.out.println("\n");
                            System.out.println("Please enter Y/N only.\n");
                        }

                    }while(!continueInput.equals("Y") && !continueInput.equals("y") && !continueInput.equals("N") && !continueInput.equals("n"));

                countEnteredPatient++;
            }while(enterNewPatient);    //end do-while loop for entering new patient details

            System.out.println("\nWaiting list of patient will be displayed soon.\n");

            try{
                Thread.sleep(1000);

            }
            catch (Exception e)
            {

            }

            System.out.println("Waiting list of patients");
            System.out.println("========================\n");
            System.out.println("Number\t\tEmergency number\t\tPatient name\t\t ArrivalTime");
            System.out.println("============================================================================");

            for(int i = 1; i <= patientList.getLength(); i++)
            {
                System.out.println(i + "\t\t\t" + patientList.getEntry(i).anotherToString());
            }

            do{
                    System.out.print("\nSo, now do you want to enter another new patient?(Eg: Y/N) : ");
                    continueInput1 = scan.nextLine();

                    if(continueInput1.equals("Y") || continueInput1.equals("y"))
                    {
                        enterNewPatient1 = true;

                        System.out.print("\n");
                    }

                    else if(continueInput1.equals("N") || continueInput1.equals("n"))
                    {
                        enterNewPatient1 = false;
                    }

                    else
                    {
                        System.out.println("\n");
                        System.out.println("Please enter Y/N only.\n");
                    }

              }while(!continueInput1.equals("Y") && !continueInput1.equals("y") && !continueInput1.equals("N") && !continueInput1.equals("n"));

          }while(enterNewPatient1);//end do-while loop for entering new patient details after viewing patient list

        System.out.println("\nNow rearranging the list based on the seriouness and their arrival time.");

        try{
                Thread.sleep(1000);

        }
        catch (Exception e)
        {

        }

        //create an unsorted array
        Patient[] tempoPatientArr = new Patient[patientList.getLength()];

        //copy the contents of patientList into tempoPatientArr
        for(int i = 1; i <= patientList.getLength(); i++ )
        {
            tempoPatientArr[i-1] = patientList.getEntry(i);
        }

        //sort tempoPatientArr
        Arrays.sort(tempoPatientArr, new EmergencyCmp());
        //the above part until this comment line does not have problem

        //check the emergency no and then categorise accordingly
        for(int i = 0; i < tempoPatientArr.length; i++)
        {
            if(tempoPatientArr[i].getEmergencyNo() == 1)
            {
                patientArr1[x] = tempoPatientArr[i];

                x++;
            }

            else if(tempoPatientArr[i].getEmergencyNo() == 2)
            {
                patientArr2[y] = tempoPatientArr[i];

                y++;
            }

            else if(tempoPatientArr[i].getEmergencyNo() == 3)
            {
                patientArr3[z] = tempoPatientArr[i];

                z++;
            }
        }

        //to check how many !null elements by using count for 3 sub-arrays
        for(int i = 0; i < patientArr1.length; i++)
        {
            if(patientArr1[i] != null)
            {
                count1++;
            }

            else
            {
                break;
            }
        }

        for(int i = 0; i < patientArr2.length; i++)
        {
            if(patientArr2[i] != null)
            {
                count2++;
            }

            else
            {
                break;
            }
        }

        for(int i = 0; i < patientArr3.length; i++)
        {
            if(patientArr3[i] != null)
            {
                count3++;
            }

            else
            {
                break;
            }
        }

        //new array with elimination of null values
        Patient[] newPatientArr1 = new Patient[count1];
        Patient[] newPatientArr2 = new Patient[count2];
        Patient[] newPatientArr3 = new Patient[count3];

        //copy the contents of old sub arrays(the arrays with null values) into the new sub arrays(without null values)
        for(int i = 0; i < newPatientArr1.length; i++)
        {
            newPatientArr1[i] = patientArr1[i];
        }

        for(int i = 0; i < newPatientArr2.length; i++)
        {
            newPatientArr2[i] = patientArr2[i];
        }

        for(int i = 0; i < newPatientArr3.length; i++)
        {
            newPatientArr3[i] = patientArr3[i];
        }

        totalCount = count1 + count2 + count3;

        //array that used to combine all the sub-arrays
        Patient[] newPatientArr = new Patient[totalCount];

        //sort all sub new arrays
        Arrays.sort(newPatientArr1, new QueueCmp());
        Arrays.sort(newPatientArr2, new QueueCmp());
        Arrays.sort(newPatientArr3, new QueueCmp());

        //combine the contents of sub new arrays into the newPatientArr array
        do{
            for (int i = 0; i < count1; i++)
            {
                newPatientArr[j] = newPatientArr1[i];

                j++;
            }

            for (int b = 0; b < count2; b++)
            {
                newPatientArr[j] = newPatientArr2[b];

                j++;
            }

            for (int c = 0; c < count3; c++)
            {
                newPatientArr[j] = newPatientArr3[c];

                j++;
            }

        }while(j < totalCount);

        //relink the nodes
        for(int i = 0; i < newPatientArr.length; i++)
        {
            newPatientList.add(newPatientArr[i]);
        }

        System.out.println("\nSorted waiting list of patients");
        System.out.println("===============================\n");
        System.out.println("Number\t\tEmergency number\t\tPatient name\t\t ArrivalTime");
        System.out.println("============================================================================");

        for(int i = 1; i <= newPatientList.getLength(); i++)
        {
            System.out.println(i + "\t\t\t" + newPatientList.getEntry(i).anotherToString());
        }
    }
}
//the above part until this comment line does not have problem
    @Override
    public int compare(Patient p1, Patient p2) 
    {
        int value = 0;

        if(p1.getEmergencyNo() > p2.getEmergencyNo())
        {
            value = 1;
        } 

        else if(p1.getEmergencyNo() < p2.getEmergencyNo())
        {
            value = -1;
        }

        else if(p1.getEmergencyNo() == p2.getEmergencyNo())
        {
            if(p1.getQueueTime() > p2.getQueueTime())
            {
                 return 1;
            } 

            else
            {
                 return -1;
            }
        }

        return value;
     }