I have method that is supposed to load a priority queue with keys in descending order. The for loops are implemented by highest priority to lowest. For instance, first loop is for passenger with type H aka the highest priority and the last loop implemented is the one regarding passengers with row number 5 - 10 aka the lowest.
partial Passenger class implementation:
public class Passenger implements Comparable<Passenger>{
private int key;
private int row;
private String name;
private String type;
public Passenger() {
}
public Passenger(int key, int row, String name, String type) {
this.key = key;
this.row = row;
this.name = name;
this.type = type;
}
@Override
public int compareTo(Passenger o) {
if(this.key > o.key) {
return 1;
}
else if(this.key < o.key){
return -1;
}
else {
return 0;
}
}
I want to group the passenger using this criteria:
1st Group: Type H
2nd Group: Type E AND Row 1-4
3rd Group: Type G AND Row 1-4
4th Group: Type E OR (Type G AND Row 10 or 11)
5th Group: Type G AND Row 23-26
6th Group: Type G AND Row 17-22
7th Group: Type G AND Row 11-16
8th Group: Type G AND Row 5-10
For some reason, some of passengers appear outside of their respective groups.
Here is the original list of passengers:
Whitley G 8
Knowles G 1
Rocha G 24
Boyle G 24
Wooten G 2
Forbes G 16
Vinson E 1
Valencia E 7
Lindsay E 16
Rasmussen E 5
Sargent G 11
Sosa G 23
Head G 3
Holcomb G 5
Carney G 4
Kirkland G 14
Levine E 9
Cash G 10
Kaufman G 6
Ratliff G 9
Macias G 4
Sharpe G 17
Sweet G 17
Delaney G 9
Emerson G 5
Castaneda E 9
Rutledge G 26
Stuart G 19
Rosales G 23
Baird G 2
Clemons G 8
Mcgowan G 18
Compton E 10
Albert G 15
Acevedo G 14
Mayer E 9
Fitzpatrick G 16
Chaney G 8
Jarvis G 3
Berger G 26
Britt E 11
Odonnell E 8
Levy E 9
Mullen G 6
Pollard G 22
Lott G 10
Cantrell G 15
Holder E 5
Vaughan E 11
Mccarty E 24
Wilder G 11
Mayo G 1
Pickett G 8
Sykes G 26
Bender G 13
Aguirre G 16
Bernard G 10
Hopper H 7
Melendez G 13
Macdonald H 18
Carver G 15
Gould E 26
Suarez G 6
Zamora G 15
Hinton G 13
Cabrera G 26
Dickson G 22
Salas G 24
Bentley G 13
Fuentes G 23
Terrell H 3
Holman E 7
Mcintyre G 16
Hebert G 13
Hendricks G 3
Jacobson G 14
Kline G 14
Faulkner G 5
Chan G 14
Mays G 1
Crosby G 25
Buck G 22
Maddox G 20
Buckley E 17
Kane G 10
Rivas E 26
Dudley G 22
Best G 12
Finley G 24
William G 18
Frost G 2
Ashley G 14
Mcconnell G 7
Blevins G 11
Middleton G 17
Bean G 18
Sheppard G 11
Estes E 7
Pugh G 8
Rivers E 6
Barr G 4
Landry E 10
Foley G 2
Here is my code:
public void loadOldPQ(ArrayList<Passenger> list) throws IOException {
int priorityNumber = list.size();
while (!list.isEmpty()) {
//H forLoop capture all required passengers
for (int i = 0; i < list.size(); i ) {
if (list.get(i).getType().equalsIgnoreCase("H")) {
list.get(i).setKey(priorityNumber);
oldPQ.add((list.get(i)));
list.remove(i);
priorityNumber--;
}
}
//Type E AND Row 1-4 forLoop works
for (int j = 0; j < list.size(); j ) {
if (list.get(j).getRow() >= 1 && list.get(j).getRow() <= 4
&& list.get(j).getType().equalsIgnoreCase("E")) {
list.get(j).setKey(priorityNumber);
oldPQ.add(list.get(j));
list.remove(j);
priorityNumber--;
}
}
//Type G(implied) and Row 1-4 forLoop works
for (int k = 0; k < list.size(); k ) {
if (list.get(k).getRow() >= 1 && list.get(k).getRow() <= 4) {
list.get(k).setKey(priorityNumber);
oldPQ.add(list.get(k));
list.remove(k);
priorityNumber--;
}
}
//Type E or Row 10, 11 forLoop
for (int l = 0; l < list.size(); l ) {
if (list.get(l).getType().equalsIgnoreCase("E") || list.get(l).getRow() == 10
|| list.get(l).getRow() == 11) {
list.get(l).setKey(priorityNumber);
oldPQ.add(list.get(l));
list.remove(l);
priorityNumber--;
}
}
//Row 23-26 forLoop
for (int m = 0; m < list.size(); m ) {
if (list.get(m).getRow() >= 23 && list.get(m).getRow() <= 26) {
list.get(m).setKey(priorityNumber);
oldPQ.add(list.get(m));
list.remove(m);
priorityNumber--;
}
}
//Row 17-22 forLoop
for (int n = 0; n < list.size(); n ) {
if (list.get(n).getRow() >= 17 && list.get(n).getRow() <= 22) {
list.get(n).setKey(priorityNumber);
oldPQ.add(list.get(n));
list.remove(n);
priorityNumber--;
}
}
//Row 11-16 forLoop (row 11 should've already been removed)
for (int o = 0; o < list.size(); o ) {
if (list.get(o).getRow() >= 11 && list.get(o).getRow() <= 16) {
list.get(o).setKey(priorityNumber);
oldPQ.add(list.get(o));
list.remove(o);
priorityNumber--;
}
}
//Row 5-10 forLoop (Row 10 passengers should've already be removed)
for (int p = 0; p < list.size(); p ) {
if (list.get(p).getRow() >= 5 && list.get(p).getRow() <= 10) {
list.get(p).setKey(priorityNumber);
oldPQ.add(list.get(p));
list.remove(p);
priorityNumber--;
}
}
}
oldProcedure(oldPQ);
}
Here are my results:
Name Type Row Key
Hopper H 7 103
Macdonald H 18 102
Terrell H 3 101
Vinson E 1 100
Knowles G 1 99
Wooten G 2 98
Head G 3 97
Carney G 4 96
Macias G 4 95
Baird G 2 94
Jarvis G 3 93
Mayo G 1 92
Hendricks G 3 91
Mays G 1 90
Frost G 2 89
Barr G 4 88
Foley G 2 87
Valencia E 7 86
Rasmussen E 5 85
Levine E 9 84
Castaneda E 9 83
Compton E 10 82
Mayer E 9 81
Britt E 11 80
Levy E 9 79
Lott G 10 78
Holder E 5 77
Mccarty E 24 76
Bernard G 10 75
Gould E 26 74
Holman E 7 73
Buckley E 17 72
Rivas E 26 71
Blevins G 11 70
Sheppard G 11 69
Rivers E 6 68
Rocha G 24 67
Sosa G 23 66
Rutledge G 26 65
Rosales G 23 64
Berger G 26 63
Sykes G 26 62
Cabrera G 26 61
Salas G 24 60
Fuentes G 23 59
Crosby G 25 58
Finley G 24 57
Sharpe G 17 56
Stuart G 19 55
Mcgowan G 18 54
Pollard G 22 53
Dickson G 22 52
Buck G 22 51
Dudley G 22 50
William G 18 49
Middleton G 17 48
Forbes G 16 47
Sargent G 11 46
Kirkland G 14 45
Albert G 15 44
Fitzpatrick G 16 43
Cantrell G 15 42
Wilder G 11 41
Bender G 13 40
Melendez G 13 39
Zamora G 15 38
Bentley G 13 37
Hebert G 13 36
Kline G 14 35
Chan G 14 34
Best G 12 33
Whitley G 8 32
Holcomb G 5 31
Kaufman G 6 30
Delaney G 9 29
Clemons G 8 28
Chaney G 8 27
Mullen G 6 26
Pickett G 8 25
Suarez G 6 24
Faulkner G 5 23
Kane G 10 22
Mcconnell G 7 21
Estes E 7 20
Landry E 10 19
Lindsay E 16 18
Odonnell E 8 17
Boyle G 24 16
Sweet G 17 15
Maddox G 20 14
Bean G 18 13
Acevedo G 14 12
Aguirre G 16 11
Hinton G 13 10
Jacobson G 14 9
Cash G 10 8
Emerson G 5 7
Pugh G 8 6
Vaughan E 11 5
Carver G 15 4
Ashley G 14 3
Ratliff G 9 2
Mcintyre G 16 1
You can see that although all passengers with "Type E" should have been detected and removed by the first or third loop. They much later in the queue along with other misplaced passengers.
CodePudding user response:
Run this little program and you'll understand that remove items without adjusting the index means that you skip items:
class Application {
public static void main(String[] args) throws IOException {
List<String> l = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
for (int i = 0; i < l.size(); i) {
System.out.println("Checking " l.get(i));
if ("B".equals(l.get(i))) {
l.remove(i);
}
}
}
}
You need to decrement i
after removing an item, i.e.
...
if ("B".equals(l.get(i))) {
l.remove(i);
i--;
}
...
Alternatively, use an Iterator
, as Old Dog suggested:
class Application {
public static void main(String[] args) throws IOException {
List<String> l = new ArrayList<>(Arrays.asList("A", "B", "C", "D"));
Iterator<String> i = l.iterator();
while (i.hasNext()) {
String s = i.next();
System.out.println("Checking " s);
if ("B".equals(s)) {
i.remove();
}
}
}
}
CodePudding user response:
Are you sure you're doing your assignment correctly? You get a list of Passenger
s. You are supposed to put them on a priority queue (which I'm going to assume is java.util.PriorityQueue
), to have them sorted according to some rules. Passenger
implements Comparable
.
The most natural way to approach it, would be to implement the compareTo()
method in Passenger
properly with the sorting rules - so that you can just put the passengers onto the queue and have the sorting happen without any extra code.
public class Passenger implements Comparable<Passenger>{
public Passenger(int row, String name, String type) {
this.row = row;
this.name = name;
this.type = type;
}
@Override
public int compareTo(Passenger o) {
if(this.getKey() > o.getKey()) {
return 1;
}
else if(this.getKey() < o.getKey()){
return -1;
}
else {
return 0;
}
}
private int getKey(){
if(type.equals("H"){
return 8;
} else if (type.equals("E") && row <= 4){
return 7;
} else
... // implement all your groups criteria like this
}
}
So changing key to be just an internal method that knows your sorting requirements. Then your method simplifies to just
public void loadOldPQ(ArrayList<Passenger> list) throws IOException {
oldPQ.addAll(list);
}
And PriorityQueue does it's job sorting them out for you.