4
[Java] Priority Queue
Author Summary
pds12345 is
in
Java
Post Body
I am implementing a registrars office simulation in where there are four different lines that students can get into. The four lines are as follows...
LinkedList
Priority Queue
Stack
And an ArrayList that students randomly get into
I wanted to ask how I can implement the Comparator in my Priority queue... In my Student class I implemented Comparable and my compareTo method recieves a Student object where I return 1 if this has more credit hours than the Student object. and -1 if the Student object has more credit hours than this.
public int compareTo(Student arg) {
if (creditHours > arg.getCreditHours()){
return 1;
}
else if (creditHours < arg.getCreditHours()){
return -1;
}
else if (creditHours == arg.getCreditHours()){
if (inTime > arg.getInTime()){
return 1;
}
else
return -1;
}
return 0;
}
That is the compareTo method and here is my code where I need to implement adding students to this priority queue based on my compareTo method...
else if (nextLineAdd == 1){
if (((Deque<Student>) second).peek() == null){
((Deque<Student>) second).add(genius);
}
else{
for (int x = 0; x < second.size(); x ){
second[x].compareTo(genius);
}
}
}
Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
8,878
Link Karma
399
Comment Karma
8,080
Profile updated: 1 month ago
Posts updated: 11 months ago
Subreddit
Post Details
Location
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 10 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/learnprogra...