队列 q = new LinkedList()

时间:2022-11-14 17:37:27

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

下面是“如何在java中实例化队列对象”的答案。

Queue is an interface. You can't instantiate an interface directly except via an anonymous inner class. Typically this isn't what you want to do for a collection. Instead, choose an existing implementation. For example:

队列是一个接口。除非通过匿名内部类,否则不能直接实例化接口。通常,这不是您想要为集合做的事情。相反,选择一个现有的实现。例如:

Queue q = new LinkedList();

队列q = new LinkedList();

or

Queue q = new ArrayDeque();

队列q = new ArrayDeque();

Typically you pick a collection implementation by the performance and concurrency characteristics you're interested in.

通常,您会根据您感兴趣的性能和并发特性选择一个集合实现。

How does this work? What does it mean for Queue, an interface, to take on the implementation of LinkedList?

这是如何工作的呢?对于队列、接口来说,它对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上调用链表方法会起作用?

4 个解决方案

#1


2  

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.

用一种(也许有点有缺陷的)隐喻来解释——把LinkedList看作一张纸。把任务分配给一个队列,它只覆盖了这张纸的一小部分,它显示了它是一个队列。

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.

如果你在上面调用一个队列方法,它仍然会像一个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,你只能看到它是一个队列,所以你只能调用它上面的队列方法。

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.

整张纸还在那里——你可以简单地把覆盖层去掉,这就意味着把它重新放回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的方式“通常”做事情我上面提到的,我的意思是LinkedList总是一个链表,即使你把它赋值给一个队列,它不会突然开始使用一个数组作为底层实现,作为一个ArrayDeque(还实现了队列),或者其他东西。

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.

队列实际上并不需要FIFO(参见文档)(如果需要,LinkedList也需要),所以LinkedList相当*的在这方面,所以我们继续这个解释使用双端队列,它具有添加和删除方法支持从前面或后面。

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实现了Deque,它需要实现addFirst函数。基于文档,addFirst功能需要添加到队列的前面,而事实上,LinkedList,它将增加LinkedList的前面(在前面的LinkedList不需要双端队列的前面,通过观察LinkedList实现的双端队列方法,我们看到前面的双端队列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,任何像样的图书馆,它应该是安全的假设每一个类实现一个接口将符合该接口要求做什么,但是只要记住并不是阻止它不符合当涉及到更多的阴凉库或缺乏经验的程序员。

#2


0  

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.

行队列q = new LinkedList();只有一个知道在变量q后面的列表的具体实现是什么,它的行为就像一个队列——每个连续的代码只能在使用q时使用队列的API。

#3


0  

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.

实现接口的所有内容都同意遵守接口的契约。这意味着在javadocs中所述队列的所有内容都适用于所有实现类,如LinkedList。它将以与接口定义相同的方式提供相同的功能。

#4


0  

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 implements the 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.

如果您声明一个队列(左、接口)并使用LinkedList(右、类)实例化它,那么您将只拥有队列操作,而不是列表方法。

#1


2  

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.

用一种(也许有点有缺陷的)隐喻来解释——把LinkedList看作一张纸。把任务分配给一个队列,它只覆盖了这张纸的一小部分,它显示了它是一个队列。

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.

如果你在上面调用一个队列方法,它仍然会像一个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,你只能看到它是一个队列,所以你只能调用它上面的队列方法。

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.

整张纸还在那里——你可以简单地把覆盖层去掉,这就意味着把它重新放回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的方式“通常”做事情我上面提到的,我的意思是LinkedList总是一个链表,即使你把它赋值给一个队列,它不会突然开始使用一个数组作为底层实现,作为一个ArrayDeque(还实现了队列),或者其他东西。

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.

队列实际上并不需要FIFO(参见文档)(如果需要,LinkedList也需要),所以LinkedList相当*的在这方面,所以我们继续这个解释使用双端队列,它具有添加和删除方法支持从前面或后面。

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实现了Deque,它需要实现addFirst函数。基于文档,addFirst功能需要添加到队列的前面,而事实上,LinkedList,它将增加LinkedList的前面(在前面的LinkedList不需要双端队列的前面,通过观察LinkedList实现的双端队列方法,我们看到前面的双端队列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,任何像样的图书馆,它应该是安全的假设每一个类实现一个接口将符合该接口要求做什么,但是只要记住并不是阻止它不符合当涉及到更多的阴凉库或缺乏经验的程序员。

#2


0  

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.

行队列q = new LinkedList();只有一个知道在变量q后面的列表的具体实现是什么,它的行为就像一个队列——每个连续的代码只能在使用q时使用队列的API。

#3


0  

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.

实现接口的所有内容都同意遵守接口的契约。这意味着在javadocs中所述队列的所有内容都适用于所有实现类,如LinkedList。它将以与接口定义相同的方式提供相同的功能。

#4


0  

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 implements the 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.

如果您声明一个队列(左、接口)并使用LinkedList(右、类)实例化它,那么您将只拥有队列操作,而不是列表方法。