This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Wouter Oet wrote:But if you want to insert all of them you can use addAll()
Followed by clear(). After all, pop() removes the element.
On a side note, the for-loop looks dangerous to me, since the body changes the result of queue.size(). Consider an initial queue size of 4:
i == 0, queue.size() == 4 so execute the body. Afterwards, queue.size() == 3.
i == 1, queue.size() == 3 so execute the body. Afterwards, queue.size() == 2.
i == 2, queue.size() == 2 -> quit
If you want to keep the manual loop and poll(), change it to a while loop: