Skip to content
Advertisement

Retrieve all queue data in for loop

I have the following code which works fine but I am struggling to figure out how to retrieve the last value in the queue.

JavaScript

Here is the output:

JavaScript

Advertisement

Answer

Since you’re incrementing x and decreasing the size of the queue, x will eventually become bigger than the remaining queue size before the queue is empty.

You can change your loop to while (!queue.isEmpty()) instead.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement