java Android 队列与堆栈
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12179887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Queue vs Stack
提问by user1555863
Why does java.util.Stack
allows me to create a new Stack
in an android activity with a simple constructor like:
为什么java.util.Stack
允许我Stack
使用简单的构造函数在 android 活动中创建一个新的,例如:
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.htmlit says the Stack
has a public constructor and on http://developer.android.com/reference/java/util/Queue.htmli 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.
我不能做同样的事情java.util.Queue
?队列不应该有类似的构造函数吗?在http://developer.android.com/reference/java/util/Stack.html 上很奇怪,它说它Stack
有一个公共构造函数,在http://developer.android.com/reference/java/util/Queue.html我没有看到类似的队列构造函数..为什么会这样?什么是有路Queue
的ImageView
,例如元素?谢谢。
回答by R4j
Because Queue is an interface, you should initial it with a LinkedList
:
因为 Queue 是一个接口,所以你应该用它来初始化它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);
}