Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By JosAH

Thread: difference of add method between Queue class and Stack class

  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default difference of add method between Queue class and Stack class

    Java Code:
    import java.util.*;
    
    public class PriorityQueueTest {
    
    	Queue<Number> queue = new PriorityQueue<Number>();
    	Number a;
    	
    	public PriorityQueueTest(){
    		queue.offer(1000);
    		queue.offer(33.23);
    		queue.offer(100.2);
    		queue.offer(92);
    		queue.offer(-21);
    		
    		while ( (a = queue.poll()) !=null )
    		{
    			System.out.println(a);
    		}
    	}
    	
    	
    	public static void main(String[] args) {
    		new PriorityQueueTest();
    	}
    
    }
    error information:

    Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
    at java.lang.Double.compareTo(Unknown Source)
    at java.util.PriorityQueue.siftUpComparable(Unknown Source)
    at java.util.PriorityQueue.siftUp(Unknown Source)
    at java.util.PriorityQueue.offer(Unknown Source)
    at PriorityQueueTest.<init>(PriorityQueueTest.java:10 )
    at PriorityQueueTest.main(PriorityQueueTest.java:24)

    however, when i use Stack<Number> a = new Stack<Number>(); i could add all the Double, Integer and Float. So what's the diference between them? how can i figure out the difference in api doc?
    Last edited by oszc; 10-06-2011 at 01:50 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: difference of add method between Queue class and Stack class

    Quote Originally Posted by oszc View Post
    however, when i use Stack<Number> a = new Stack<Number>(); i could add all the Double, Integer and Float. So what's the diference between them? how can i figure out the difference in api doc?
    Stacks and Queues can store objects only so autoboxing is applied when you attempt to store primitives (ints, longs etc.) A Queue needs its members to implement the Comparable interface because it has to store the members in a certain order; a Stack doesn't do that. The Number, Integer, Double etc. hierarchy is a bit stupid: Integers can only be compared to Integers, Doubles to Doubles etc. You put Integers and Doubles in your Queue and tries to compare them. You saw the result in your stack trace. A cheap hack would be to store doubles in your Queue only.

    kind regards,

    Jos
    oszc likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. Nested List class for Simulation Queue
    By Cheddz in forum Advanced Java
    Replies: 2
    Last Post: 02-19-2010, 01:19 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. Replies: 6
    Last Post: 02-11-2009, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •