wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Priority Queue doubt" Watch "Priority Queue doubt" New topic
Author

Priority Queue doubt

rajeev jayappa
Greenhorn

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");
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

Joined: Mar 24, 2008
Posts: 3816

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 ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
rajeev jayappa
Greenhorn

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

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

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

Joined: Oct 13, 2005
Posts: 37923
    
  22
How? What criterion did you use?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Priority Queue doubt
 
Similar Threads
PriorityQueue Problem
PriorityQueue ,add and compare object
Priority Queue Ordering
poll(),peek() in PriorityQueue class
Queue in Java