Reputation: 133
As the title says I want to know if there's a way to find out how many elements there are in a queue(similar to String.length()). i'm using the weiss nonstandard ListQueue thanks in advance.
Upvotes: 0
Views: 6299
Reputation: 3584
In java, you can use size()
method to determine the number of elements in a queue. For example,
public Integer countQueueElement()
{
Queue<Integer> q = new Queue<Integer>();
q.add(1);
q.add(2);
q.add(3);
return q.size();
}
Upvotes: 0
Reputation: 115
As far as the documentation says there is no method for getting the length.
Instead you can create your own method for it, extend this ListQueue class and override or create another method(Wrapper method) for the enqueue method in which increase the counter every time you call enqueue method and declare a static variable for keeping the counter. After increasing the counter call the super class enqueue method as normal.
and declare the method where you can access the static variable for the counter. In this way you will be able to get the length of the queue.
Upvotes: 1