【发布时间】:2015-08-29 07:21:54
【问题描述】:
我有一个实现 Comparable 的类 Student ,如下所示,因为我想将 Student 的对象放在优先级队列中,并让注册日期较早的学生具有更高的优先级。
public class Student implements Comparable{
private String fullName;
private Date registrationDate;
public Student(String fullname){
this.fullName = fullName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Date getRegistrationDate() {
return registrationDate;
}
public void setRegistrationDate(Date registrationDate) {
this.registrationDate = registrationDate;
}
@Override
public int compareTo(Object obj) {
Student student = (Student) obj;
if(getRegistrationDate().before(student.getRegistrationDate())){
return 1;
}else if(getRegistrationDate().after(student.getRegistrationDate())){
return -1;
}
return 0;
}
}
在主要方法中,我正在创建学生对象,设置注册日期并将它们放入优先队列中
public class School {
public static void main(String args[]) {
//list of students
Student student1 = new Student("John Doe");
Date dateStudent1Joined = new GregorianCalendar(2014, Calendar.JULY, 1).getTime();
student1.setRegistrationDate(dateStudent1Joined);
Student student2 = new Student("Mary Penn");
Date dateStudent2Joined = new GregorianCalendar(2014, Calendar.JULY, 2).getTime();
student2.setRegistrationDate(dateStudent2Joined);
Student student3 = new Student("George Coats");
Date dateStudent3Joined = new GregorianCalendar(2014, Calendar.JULY, 3).getTime();
student3.setRegistrationDate(dateStudent3Joined);
Student student4 = new Student("Tony Case");
Date dateStudent4Joined = new GregorianCalendar(2014, Calendar.JULY, 4).getTime();
student4.setRegistrationDate(dateStudent4Joined);
Student student5 = new Student("Ben Thomas");
Date dateStudent5Joined = new GregorianCalendar(2014, Calendar.JULY, 5).getTime();
student5.setRegistrationDate(dateStudent5Joined);
//create a queue data structure to hold students
PriorityQueue<Student> studentQueue = new PriorityQueue<Student>();
//add students to queue
studentQueue.offer(student1);
studentQueue.offer(student2);
studentQueue.offer(student3);
studentQueue.offer(student4);
studentQueue.offer(student5);
//print names of people in queue
for(Student student : studentQueue){
String studentName = student.getFullName();
System.out.println(studentName);
System.out.println("");
}
studentQueue.poll();
for(Student student : studentQueue){
String studentName = student.getFullName();
System.out.println(studentName);
}
}
}
在 School.java 中的第一个 for 循环中,我打印出队列中所有学生的姓名,以确保他们处于正确的优先级,即注册日期较早的学生排在队列的前面。这里的结果是我所期望的,因为 John Doe 首先加入,Ben Thomas 最后加入。
John Doe
Mary Penn
George Coats
Tony Case
Ben Thomas
但是,当我在优先级队列上调用 poll() 方法时,John Doe 被删除但优先级队列的顺序不再有意义,第二个 for 循环的输出是
Mary Penn
Tony Case
George Coats
Ben Thomas
Mary Penn 的位置正确,但如果之前的优先级队列中的顺序仍然占主导地位,George Coats 应该排在 Tony Case 之前,我在这里做错了什么?
【问题讨论】:
-
真的吗?没有
instanceof签入您的compareTo,但您执行了不安全的演员表? -
我不确定你在说什么,但它编译得很好,问题出在哪里?
-
您将
obj转换为Student,而没有if (obj instanceof Student)。 -
请使用泛型。它们存在超过 10 年。
Student implements Comparable<Student>. -
@bcsb1001 这很好,并且尊重 Comparable 的合同。如果 obj 不是 Student 的实例,他会怎么做?抛出 ClassCastException?无论如何,这就是未经检查的演员会做的事情。不过,OP 应该使用泛型。
标签: java data-structures priority-queue