Why does java.util.Stack
allows me to create a new Stack
in an android activity with a simple constructor like:
Stack < ImageView> stack = new Stack< ImageView>();
and i cannot do the same with java.util.Queue
? Shouldn't a queue have a similar constructor? Strange enough on http://developer.android.com/reference/java/util/Stack.html it says the Stack
has a public constructor and on http://developer.android.com/reference/java/util/Queue.html i don't see a similar constructor for a queue.. why is that? what is the way to have a Queue
of ImageView
elements for example?
Thanks.
Both queue and stack seem fine for this purpose, as the order in which they are taken out does not matter. The question is: is any of them significantly faster than the other? Answer: most probably not. Best answer: try both and test it.
Because Queue is an interface, you should initial it with a LinkedList
:
Queue<String> qe = new LinkedList<String>();
qe.add("b");
qe.add("a");
qe.add("c");
//Traverse queue
Iterator it = qe.iterator();
System.out.println("Initial Size of Queue :" + qe.size());
while(it.hasNext())
{
String iteratorValue = (String) it.next();
System.out.println("Queue Next Value :" + iteratorValue);
}
It is because Queue is just an interface. To create a Queue object you need a class, what implements the methods of a Queue.
Some actual implementations of a Queue: link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With