Java Queue<Integer> q = new LinkedList<Integer>()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21727873/
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
Queue<Integer> q = new LinkedList<Integer>()
提问by user3301555
Here is an answer to "How do I instantiate a Queue object in java?",
Queue
is an interface. You can't instantiate an interface directly except via an anonymous inner class. Typically this isn'twhat you want to do for a collection. Instead, choose an existing implementation. For example:Queue q = new LinkedList();
or
Queue q = new ArrayDeque();
Typically you pick a collection implementation by the performance and concurrency characteristics you're interested in.
Queue
是一个接口。除非通过匿名内部类,否则不能直接实例化接口。通常,这不是您想要为集合执行的操作。相反,选择一个现有的实现。例如:队列 q = new LinkedList();
或者
队列 q = new ArrayDeque();
通常,您会根据您感兴趣的性能和并发特性来选择集合实现。
How does this work? What does it mean for Queue, an interface, to take on the implementation of LinkedList?
这是如何运作的?Queue这个接口承担LinkedList的实现意味着什么?
Does that mean objects can be dequeued in a FIFO (first-in-first-out order), and calling linked list methods on q
would work?
这是否意味着对象可以在 FIFO(先进先出顺序)中出列,并且调用链表方法q
会起作用吗?
采纳答案by Bernhard Barker
To explain with a (perhaps somewhat flawed) metaphor - think of a LinkedList
as a piece of paper. Think of the assignment to a Queue
as covering all but a little part of this piece of paper which reveals that it's a Queue
.
用一个(可能有些有缺陷的)比喻来解释——把 a 想象LinkedList
成一张纸。把对 a 的赋值Queue
看作是覆盖了这张纸的所有部分,只有一小部分表明它是一个Queue
.
If you call a Queue
method on it, it will still do things like a LinkedList
normally would, since that that's what the piece of paper is.
如果你Queue
在它上面调用一个方法,它仍然会像LinkedList
往常一样做事情,因为那是一张纸。
Given that most of it is covered, you can't see it's a LinkedList
, all you can see is that it's a Queue
, so you can only call the Queue
methods on it.
鉴于大部分内容LinkedList
都已涵盖,您看不到它是 a ,您只能看到它是 a Queue
,因此您只能调用其Queue
上的方法。
The whole piece of paper is still there - you could simply remove the covering, which would be synonymous to casting it back to a LinkedList
, which would then allow you to call any LinkedList
method on it again.
整张纸仍然存在 - 您可以简单地移除覆盖物,这与将其转换回 a 是同义词LinkedList
,然后您LinkedList
可以再次调用它的任何方法。
Now for a bit more technical details:
现在了解更多技术细节:
By the way a LinkedList
"normally would" do things I mentioned above, I mean a LinkedList
is always a linked-list, even if you assign it to a Queue
- it doesn't suddenly start using an array as the underlying implementation, as an ArrayDeque
would (which also implements Queue
), or something else.
顺便说一句,LinkedList
“通常会”做我上面提到的事情,我的意思是 aLinkedList
总是一个链表,即使你将它分配给 a Queue
- 它不会突然开始使用数组作为底层实现,就像ArrayDeque
(它也实现Queue
) 或其他东西。
Queue
doesn't actually need to be FIFO (see the docs) (if it needed to be, a LinkedList
would also need to be), so LinkedList
would have quite a bit of freedom in this regard, so let's continue this explanation using Deque
- it has methods to support addition and removal from either the front or the back.
Queue
实际上并不需要是 FIFO(参见文档)(如果需要,aLinkedList
也需要是),所以LinkedList
在这方面会有相当多的自由,所以让我们继续使用这个解释Deque
- 它有方法支持从前面或后面添加和删除。
Because LinkedList
implements Deque
, it needs to implement the addFirst
function. Based on the docs, the addFirst
function needs to add to the front of the Deque
, and indeed, with a LinkedList
, it will add to the front of the LinkedList
(while the front of the LinkedList
doesn't need to be the front of the Deque
, by looking at the Deque
methods implemented in LinkedList
, we see that the front of the Deque
is the front of the LinkedList
, and all the methods add to / remove from one of the sides, do so from the correct side).
因为LinkedList
implements Deque
,所以需要实现addFirst
功能。根据文档,该addFirst
函数需要添加到 的前面Deque
,实际上,使用 a LinkedList
,它将添加到 the 的前面LinkedList
(而 the 的前面LinkedList
不需要是 的前面Deque
,通过查看在 中Deque
实现的方法LinkedList
,我们看到 的前面Deque
是 的前面LinkedList
,并且所有方法从一侧添加到/删除,从正确的一侧执行)。
Now for an important albeit somewhat confusing note - LinkedList
can, for example, implement Deque
and have an addFirst
that doesn't do what it's supposed to - it can just, for example, print some random text. There's nothing in the language itself that prevents this - since, as far as the compiler is concerned, implementing Deque
just requires that you define a bunch of methods - there's no enforcement of what these methods are supposed to do. In terms of the Java API, and any decent library, it should be safe to assume that every class implementing an interface will conform to what that interface claims to do, but just keep in mind that there isn't something stopping it from not conforming when it comes to more shady libraries or less experienced programmers.
现在是一个重要但有点混乱的注释——LinkedList
例如,可以实现Deque
并拥有一个addFirst
不做它应该做的事情——它可以只是,例如,打印一些随机文本。语言本身没有任何东西可以阻止这一点——因为就编译器而言,实现Deque
只需要你定义一堆方法——没有强制执行这些方法应该做什么。就 Java API 和任何体面的库而言,假设实现接口的每个类都将符合该接口声称要做的事情应该是安全的,但请记住,没有什么可以阻止它不符合当涉及到更多阴暗的库或经验不足的程序员时。
回答by Smutje
The line Queue q = new LinkedList();
is the only one knowing which concrete implementation of list lies behind the variable q
, which behaves like a Queue
- each consecutively code can only use the API of Queue
when working with q
.
该行Queue q = new LinkedList();
是唯一知道 list 的具体实现位于变量后面的行q
,它的行为就像一个Queue
- 每个连续的代码只能使用Queue
时使用的 API q
。
回答by Thom
Everything that implements an interface agrees to adhere to the contract of an interface. This means that everything that is said about a queue in the javadocs will be true of all implementing classes liked LinkedList. It will provide the same functionality in the same way as defined by the interface.
实现接口的一切都同意遵守接口的契约。这意味着 javadoc 中关于队列的所有内容都将适用于所有实现类,例如 LinkedList。它将以与接口定义的相同方式提供相同的功能。
回答by Thom
Like you said, Queue is an interface, an Abstract Data Type, it specifies operations and their semantics (namely, FIFO discipline).
就像你说的,队列是一个接口,一种抽象数据类型,它指定了操作及其语义(即 FIFO 规则)。
LinkedList is a class provided by the Java.util library, meaning it implementsthe methods of all interfaces it declares to... implement. The fact that it's name is "Linkedlist" means that it uses a... LinkedList (nodes containing the elements allocated in the heap with pointers from a node to the next one). It's an ordered data structure.
LinkedList 是 Java.util 库提供的一个类,这意味着它实现了它声明的所有接口的方法......实现。它的名字是“Linkedlist”这一事实意味着它使用一个... LinkedList(包含在堆中分配的元素的节点,带有从一个节点到下一个节点的指针)。这是一个有序的数据结构。
If you declare a Queue (left-hand, interface) and instantiate it with a LinkedList (right-hand, class), you'll only have the Queue operations, not the List methods.
如果您声明一个 Queue(左手,接口)并使用 LinkedList(右手,类)实例化它,您将只有 Queue 操作,而不是 List 方法。
回答by Sahil Alipuria
It basically translated to creation of LinkedList object which adheres to Queue operations. All the nodes will be connected with each other but you can add/remove/size on the list.
它基本上转化为遵守队列操作的 LinkedList 对象的创建。所有节点都将相互连接,但您可以在列表中添加/删除/调整大小。