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

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

Queue<Integer> q = new LinkedList<Integer>()

javadata-structuresqueue

提问by user3301555

Here is an answer to "How do I instantiate a Queue object in java?",

这是“如何在 Java 中实例化队列对象?”的答案。,

Queueis 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 qwould work?

这是否意味着对象可以在 FIFO(先进先出顺序)中出列,并且调用链表方法q会起作用吗?

采纳答案by Bernhard Barker

To explain with a (perhaps somewhat flawed) metaphor - think of a LinkedListas a piece of paper. Think of the assignment to a Queueas 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 Queuemethod on it, it will still do things like a LinkedListnormally 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 Queuemethods 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 LinkedListmethod 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 LinkedListis 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 ArrayDequewould (which also implements Queue), or something else.

顺便说一句,LinkedList“通常会”做我上面提到的事情,我的意思是 aLinkedList总是一个链表,即使你将它分配给 a Queue- 它不会突然开始使用数组作为底层实现,就像ArrayDeque(它也实现Queue) 或其他东西。

Queuedoesn't actually need to be FIFO (see the docs) (if it needed to be, a LinkedListwould also need to be), so LinkedListwould 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 LinkedListimplements Deque, it needs to implement the addFirstfunction. Based on the docs, the addFirstfunction 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 LinkedListdoesn't need to be the front of the Deque, by looking at the Dequemethods implemented in LinkedList, we see that the front of the Dequeis the front of the LinkedList, and all the methods add to / remove from one of the sides, do so from the correct side).

因为LinkedListimplements Deque,所以需要实现addFirst功能。根据文档,该addFirst函数需要添加到 的前面Deque,实际上,使用 a LinkedList,它将添加到 the 的前面LinkedList(而 the 的前面LinkedList不需要是 的前面Deque,通过查看在 中Deque实现的方法LinkedList,我们看到 的前面Deque是 的前面LinkedList,并且所有方法从一侧添加到/删除,从正确的一侧执行)。

Now for an important albeit somewhat confusing note - LinkedListcan, for example, implement Dequeand have an addFirstthat 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 Dequejust 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 Queuewhen 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 对象的创建。所有节点都将相互连接,但您可以在列表中添加/删除/调整大小。