java - How to understand PriorityQueue? -


here code

 import java.util.*;     public class prioritypuzzle{         public static void main(string []args){             priorityqueue<integer> prq = new priorityqueue<integer>();               ( int = 10; > 0; i-- ){                     prq.add (i);                    system.out.print(i+" , ");             }                system.out.println ( "\n initial priority queue values are: "+ prq);             }       } 

result

enter image description here

i donnot know why after priority queue become {1,2,5,4,3,9,6,10,7,8}

the tostring() method priorityqueue inherits the 1 abstractcollection. iterates on items in collection , prints them.

from docs for iterator of priorityqueue:

the iterator provided in method iterator() not guaranteed traverse elements of priority queue in particular order. if need ordered traversal, consider using arrays.sort(pq.toarray()).

so printing queue doesn't print items in order them if retrieved them queue 1 @ time.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -