• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Priority Queue doubt

 
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
 
Bartender
Posts: 4101
63
Hibernate Fedora Chrome
  • 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
 
Marshal
Posts: 65411
248
  • 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
Marshal
Posts: 65411
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How? What criterion did you use?
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!