Results 1 to 10 of 10
  1. #1
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default PriorityQueue question

    Good day!

    Im just wondering, why does my priority queue elements doesnt sort?
    Java Code:
    import java.util.*;
    public class PriorityQueueDemo {
    public static void main(String[] args){
    Queue<String> s = new PriorityQueue<String>();
    Queue<Integer> u = new PriorityQueue<Integer>();		
    s.add("ab");
    s.add("abcd");
    s.add("abc");
    s.add("a");
    s.add("b");
    u.add(32);
    u.add(12);
    u.add(1);
    //don't use iterator which may or may not 
    //show the PriorityQueue's order
    //while( s.size() > 0)
      //     System.out.println(s.remove());	
      System.out.println (s);	
      System.out.println (u);			
    }
    
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    How do you know it doesn't sort?

  3. #3
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    When you run the code, the output is:
    [a, ab, abc, abcd, b]
    [1, 32, 12] ------ this one is not sorted

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    It is pure luck that the first one appears sorted.

    Java Code:
    System.out.println (s);
    Do you understand what that line of code does? It calls the toString method. this is what the API says:
    "Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). "

    Here is what the API says about the iterator method:
    "Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order"
    "

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by kryptonian03 View Post
    When you run the code, the output is:
    [a, ab, abc, abcd, b]
    [1, 32, 12] ------ this one is not sorted
    This output is provided by the toString() method of the PriorityQueue class. Look that up in the API docs, and keep this in mind:

    //don't use iterator which may or may not
    //show the PriorityQueue's order
    -Gary-

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default

    And what about the toString method makes you think it will print out the elements in order? Did you read the API?
    Try
    Java Code:
    System.out.println (u);
    	while( u.size() > 0)
    	       System.out.println(u.remove());	
    	 	
    	 			
    	}
    Edit: not fast enough again...

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Change that to poll and not remove.

    Ack, never mind I missed the inherited method.

  8. #8
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Thanks a lot!

    Java Code:
    import java.util.*;
    public class PriorityQueueDemo {
    public static void main(String[] args){
    Queue<String> s = new PriorityQueue<String>();
    Queue<Integer> u = new PriorityQueue<Integer>();		
    s.add("ab");
    s.add("abcd");
    s.add("abc");
    s.add("a");
    s.add("b");
    u.add(32);
    u.add(12);
    u.add(1);	
      System.out.println (s);	
      
    	while( u.size() > 0){
    	
    	       System.out.println(u.poll());	
    	}
    	System.out.println (u); 			
    	}			
    }
    How can remove the [] at output:
    [a, ab, abc, abcd, b]
    1
    12
    32
    [] ---- this one

    Thanks a lot

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    As we tried to explain, if you just print the Queue it calls the toString method and the square brackets are the default behaviour of that method. If you don't want them then you will have to implement your own printing method.

  10. #10
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Now i understand... thats why you have to used print a poll operation according to the size of each data... thank you very much

Similar Threads

  1. Pseudo-priorityQueue datastructure
    By leeple in forum Advanced Java
    Replies: 2
    Last Post: 03-01-2011, 11:30 AM
  2. PriorityQueue question
    By j2me64 in forum Advanced Java
    Replies: 4
    Last Post: 02-28-2011, 02:20 PM
  3. PriorityQueue question
    By j2me64 in forum Advanced Java
    Replies: 0
    Last Post: 02-28-2011, 09:00 AM
  4. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  5. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 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
  •