Results 1 to 10 of 10
Thread: PriorityQueue question
- 03-10-2011, 01:37 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
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); } }
- 03-10-2011, 01:42 AM #2
How do you know it doesn't sort?
- 03-10-2011, 02:05 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
When you run the code, the output is:
[a, ab, abc, abcd, b]
[1, 32, 12] ------ this one is not sorted
- 03-10-2011, 02:14 AM #4
It is pure luck that the first one appears sorted.
Do you understand what that line of code does? It calls the toString method. this is what the API says:Java Code:System.out.println (s);
"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"
"
- 03-10-2011, 02:15 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
- 03-10-2011, 02:16 AM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
And what about the toString method makes you think it will print out the elements in order? Did you read the API?
Try
Edit: not fast enough again...Java Code:System.out.println (u); while( u.size() > 0) System.out.println(u.remove()); }
- 03-10-2011, 02:19 AM #7
Change that to poll and not remove.
Ack, never mind I missed the inherited method.
- 03-10-2011, 02:51 AM #8
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Thanks a lot!
How can remove the [] at output: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); } }
[a, ab, abc, abcd, b]
1
12
32
[] ---- this one
Thanks a lot
- 03-10-2011, 03:35 AM #9
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.
- 03-10-2011, 03:44 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Pseudo-priorityQueue datastructure
By leeple in forum Advanced JavaReplies: 2Last Post: 03-01-2011, 11:30 AM -
PriorityQueue question
By j2me64 in forum Advanced JavaReplies: 4Last Post: 02-28-2011, 02:20 PM -
PriorityQueue question
By j2me64 in forum Advanced JavaReplies: 0Last Post: 02-28-2011, 09:00 AM -
Question concerning question marks and colons
By jim01 in forum New To JavaReplies: 17Last Post: 01-14-2011, 12:05 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks