Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Priority Queue doubt

 
rajeev jayappa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote a code it is throwing class cast exception
can any one correct me

package newPack;

import java.util.Iterator;
import java.util.PriorityQueue;

public class AirPlaneReservation {

public static void main(String[] args) {
PriorityQueue queue = new PriorityQueue();

Passenger passenger1 = new Passenger("Rajeev", " ", "J", "17/09/1983",
"PA", "1");
queue.add(passenger1);
Passenger passenger2 = new Passenger("Pradeep", "Tejaswi", "CD",
"17/09/1983", "PA", "2");
queue.add(passenger2);
Passenger passenger3 = new Passenger("Aravind", "C", "K", "17/09/1983",
"PA", "3");
queue.add(passenger3);
Passenger passenger4 = new Passenger("Sandeep", "S", " ", "17/09/1983",
"PA", "4");
queue.add(passenger4);
System.out.println(queue.size());
System.out.println("Passenger Status-------------------------->");
for (Iterator iter = queue.iterator(); iter.hasNext() {
Passenger element = (Passenger) iter.next();
System.out.println(" ");
System.out.println("Fully Qualified Name:" + element.getFirstName()
+ element.getMiddleName() + element.getLastName());
System.out.println("DOB:" + element.getDob());
System.out.println("Company:" + element.getCompany());
System.out.println("Ticket Number:" + element.getTicketNumber());
}
}
}

if i add one object it is not throwing anything
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check this at API
A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).


Can't see whether your Passenger objects are comparable
[ September 30, 2008: Message edited by: Vijitha Kumara ]
 
rajeev jayappa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot
i made passenger class implement comparable
i got the things i want
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How did you implement Comparable<Passenger>? Does Passenger have a "natural order?" It might have been better to pass a Comparator.
 
rajeev jayappa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya i override the compareTo method
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How? What criterion did you use?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic