我正在为即将到来的考试做一些修改。我已经看过我们的讲师给我们修改的代码。
代码:
public class Employee implements Comparable<Employee>{
private String name;
public Integer idNumber;
public Employee(String name,int idNumber){
this.name = name;
this.idNumber = idNumber;
}
public String toString(){
return this.name+": "+idNumber;
}
@Override
public int compareTo(Employee o) {
return this.idNumber.compareTo(o.idNumber);
}
public static void main(String args[]){
Queue<Employee> queue = newLinkedList<Employee>();
queue.add(new Employee("Carl",2));
queue.add(new Employee("Alice",1));
queue.add(new Employee("Bob",3));
queue.add(new Employee("Danny",4));
System.out.println(queue.peek());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.remove());
System.out.println(queue.peek());
System.out.println(queue.remove());
}
}
我们被要求记下控制台输出。我的猜测是:
Danny:4
Danny:4
Bob:3
Carl:2
Alice:1
Alice:1
哪个错误,当它通过Eclipse运行时,结果是:
Carl: 2
Carl: 2
Alice: 1
Bob: 3
Danny: 4
Danny: 4
有人请帮助解释我哪里出错了。我假设:
@Override
public int compareTo(Employee o) {
return this.idNumber.compareTo(o.idNumber);
}
按0-4
的数字顺序对ID进行排序由于