I'm new here and was looking for some advise on my code. I'm trying to make it possible for an arrayList ArrayList<patient> heartPatientArray = new ArrayList<patient>(); //heart patients
ArrayList<patient> heartPatientArray = new ArrayList<patient>(); //heart patients
to be able to be sorted on a patient's
arrivalTime
.
Code:
class patient implements Comparable
{
int typeSickness; //1 for heart 2 for gastro 3 for bleeding
double arrivalTime; //what time they arrived SORT BY
int deathTime; // what time they are set to balk or die
int status; //0 for being cared for, 1 to n for location in line, -1 if dead
int personalNumberInLine; //personal number in line updated everytime someone enters the line
double timeSpentInQueue; //total time before they either died or were able to be treated
int idOfPerson; //unique id the person gets when entering the queue
boolean isAlive;
public patient(int sickness, double arrival, int ID) { //sets the patients sickness, time arrived, and ID
typeSickness=sickness;
arrivalTime=arrival;
idOfPerson = ID;
}
public int getTypeSickness() {
return typeSickness;
}
public void setTypeSickness(int typeSickness) {
this.typeSickness = typeSickness;
}
public double getArrivalTime() {
return arrivalTime;
}
public void setArrivalTime(double arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getDeathTime() {
return deathTime;
}
public void setDeathTime(int deathTime) {
this.deathTime = deathTime;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getNumberInLine() {
return personalNumberInLine;
}
public void setNumberInLine(int numberInLine) {
this.personalNumberInLine = numberInLine;
}
@Override
public int compareTo(patient one) {
if (this.getArrivalTime() < one.getArrivalTime()) {
return -1;
}
else if(this.getArrivalTime() > one.getArrivalTime()){
return 1;
}
return 0;
}
some code edited out for time's sake
I've tried styling it after some code I found online but I'm getting an error on compareTo(patient one)
of Remove Override Notation, and an error on class patient implements Comparable
telling me I must make the class abstract.
After I implement the compareTo correctly, how would I go about sorting the heartPatientArray
?
You are implementing the raw form of the Comparable
interface. Because of this, compareTo
takes an Object
, which explains why you are getting an error attempting to implement the interface.
Instead, pass your class as the type parameter.
class patient implements Comparable<patient>
Then your compareTo
method, as is, will implement Comparable<patient>
properly.
Typically, Java naming conventions would say to capitalize your class name.
class Patient implements Comparable<Patient>
You can sort your list with:
Collections.sort(heartPatientArray);
If you'd like to sort it in reverse order, you can specify that with:
Collections.sort(heartPatientArray, Comparator.reverseOrder());
In general, you can sort it however you want by passing an instance of Comparator<Patient>
, implementing Comparator
's compare
method, taking care to pass a type parameter to Comparable
like we are now doing for Comparable
. Then pass an instance of Comparator<Patient>
as the second argument to Collections.sort
.
Replace
class patient implements Comparable
with
class patient implements Comparable<patient>
otherwise you need to implement compareTo(Object o)
in your class.
When you have instances of a Comparable
class, you can just add them to an ArrayList
and use the method Collections.sort
.
So, basically what you want is to be able to compare between 2 patient
objects. The way it is now, you are being able to compare between a patient
and some other object. To ensure, that you are only comparing patient
objects, you would need to change
class patient implements Comparable
to
class patient implements Comparable<patient>
The way you have down already is not wrong in any way (except for the compareTo
param). You can, of course, compare a patient
to some other object. If you wish to pursue that way,
@Override
public int compareTo(Object obj) {
// you would want to see if the `Object` is of `patient` type
if( !(obj instanceof patient) )
return -1; // `obj` IS NOT OF TYPE `patient`; PICK YOUR RETURN VALUE; IDEALLY THROW AN EXCEPTION
patient one = (patient) o;
if (this.getArrivalTime() < one.getArrivalTime()) {
return -1;
}
else if(this.getArrivalTime() > one.getArrivalTime()){
return 1;
}
return 0;
}
Then, you can sort it by calling Collections.sort(..)
or something.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.