目前这是我的枚举(VisitorInfo)类:
public enum Ticket{
NO_TICKET(0), REG_TICKET(1), FAST_TICKET(2);
private int status;
Ticket(int status)
{
this.status = status;
}
int getStatus() {
return status;
}
}
这是目前我的队列声明:
PriorityQueue<VisitorInfo> pq = new PriorityQueue<VisitorInfo>(20, new Comparator<VisitorInfo>() {
public int compare(VisitorInfo n1, VisitorInfo n2) {
if (n1.ticket.getStatus() == n2.ticket.getStatus())
return 0;
if (n1.ticket.getStatus() > n2.ticket.getStatus())
return -1;
return 1;
}
});
我的主要方法:
public static void main(String[] args) throws FullQueueException {
RideQueue bleh = new RideQueue("hi", 20);
VisitorInfo bleh1 = new VisitorInfo("NO1", VisitorInfo.Ticket.NO_TICKET);
VisitorInfo bleh2 = new VisitorInfo("NO2", VisitorInfo.Ticket.NO_TICKET);
VisitorInfo bleh3 = new VisitorInfo("NO3", VisitorInfo.Ticket.NO_TICKET);
VisitorInfo bleh4 = new VisitorInfo("REG1", VisitorInfo.Ticket.REG_TICKET);
VisitorInfo bleh5 = new VisitorInfo("REG2", VisitorInfo.Ticket.REG_TICKET);
VisitorInfo bleh6 = new VisitorInfo("REG3", VisitorInfo.Ticket.REG_TICKET);
VisitorInfo bleh7 = new VisitorInfo("FAST1", VisitorInfo.Ticket.FAST_TICKET);
VisitorInfo bleh8 = new VisitorInfo("FAST2", VisitorInfo.Ticket.FAST_TICKET);
bleh.pq.add(bleh1);
bleh.pq.add(bleh2);
bleh.pq.add(bleh3);
bleh.pq.add(bleh4);
bleh.pq.add(bleh5);
bleh.pq.add(bleh6);
bleh.pq.add(bleh7);
bleh.pq.add(bleh8);
for (int i = 1; i<9; i++)
{
System.out.println(bleh.pq.poll().getName());
}
}
我的输出:
FAST1
FAST2
REG3
REG2
REG1
NO2
NO1
NO3
为什么排序不正确?
对不起,我忘了补充,Fast 应该先打印出来,然后是 reg,然后不是。