java - How to understand PriorityQueue? -
this question has answer here:
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
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
Post a Comment