This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Priority Queue doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Priority Queue doubt" Watch "Priority Queue doubt" New topic

Priority Queue doubt

rajeev jayappa

Joined: Sep 15, 2008
Posts: 8
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");
Passenger passenger2 = new Passenger("Pradeep", "Tejaswi", "CD",
"17/09/1983", "PA", "2");
Passenger passenger3 = new Passenger("Aravind", "C", "K", "17/09/1983",
"PA", "3");
Passenger passenger4 = new Passenger("Sandeep", "S", " ", "17/09/1983",
"PA", "4");
System.out.println("Passenger Status-------------------------->");
for (Iterator iter = queue.iterator(); iter.hasNext() {
Passenger element = (Passenger);
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

Joined: Mar 24, 2008
Posts: 3900

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 ]

[How to ask questions] [Twitter]
rajeev jayappa

Joined: Sep 15, 2008
Posts: 8
thanks a lot
i made passenger class implement comparable
i got the things i want
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45338
How did you implement Comparable<Passenger>? Does Passenger have a "natural order?" It might have been better to pass a Comparator.
rajeev jayappa

Joined: Sep 15, 2008
Posts: 8
ya i override the compareTo method
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45338
How? What criterion did you use?
I agree. Here's the link:
subject: Priority Queue doubt
It's not a secret anymore!