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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:54:02  来源:igfitidea点击:

Android Queue vs Stack

javaandroid

提问by user1555863

Why does java.util.Stackallows me to create a new Stackin 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 Stackhas 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 Queueof ImageViewelements 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我没有看到类似的队列构造函数..为什么会这样?什么是有路QueueImageView,例如元素?谢谢。

回答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);
}

回答by Balázs édes

It is because Queue is just an interface. To create a Queue object you need a class, what implements the methods of a Queue.

这是因为 Queue 只是一个接口。要创建 Queue 对象,您需要一个类,它实现了 Queue 的方法。

Some actual implementations of a Queue: link

队列的一些实际实现:链接