• Post Reply Bookmark Topic Watch Topic
  • New Topic

PriorityQueue.size()  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i am displaying the elements in the queue using poll method
for this i have used for loop like this

i think there is a problem with PriorityQueue.size() inside the loop
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API documentation of PriorityQueue.poll(). It says:

Retrieves and removes the head of this queue...

When you remove an element from the queue, its size isn't 10 anymore.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everytime you call poll(), the size decreases with one. Thus by the time i = 5, pq3.size() = 5 and the loop stops.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!