为什么java链表实现使用接口deque?

发布于 2024-11-05 02:42:25 字数 332 浏览 0 评论 0原文

我在查看LinkedList的java实现,发现了这个:

public class LinkedList<E> 
       extends AbstractSequentialList<E> implements List<E>,
               Deque<E>, Cloneable, java.io.Serializable

Why should a LinkedList support the Deque interface? 我理解将元素添加到链表末尾的愿望,但这些方法应该包含在 List 接口中。

I was looking at the java implementation of LinkedList, and found this:

public class LinkedList<E> 
       extends AbstractSequentialList<E> implements List<E>,
               Deque<E>, Cloneable, java.io.Serializable

Why should a LinkedList support the Deque interface?
I understand the desire to add elements to the end of the linked list, but those methods should have been incuded in the List interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若有似无的小暗淡 2024-11-12 02:42:25

LinkedList 实现恰好满足 Deque 契约,那么为什么不让它实现该接口呢?

The LinkedList implementation happens to to satisfy the Deque contract, so why not make it implement the interface?

丑丑阿 2024-11-12 02:42:25

正如 JavaDocs 中所述:

这些操作允许将链表用作堆栈、队列或双端队列。

List 接口只是一个 List,即您可以添加或删除。因此,List 接口的基本实现必须只提供那些简单的方法,例如 ArrayList。 Deque接口是双端队列,iava的LinkedList IS-A双端队列。

As the JavaDocs states:

These operations allow linked lists to be used as a stack, queue, or double-ended queue.

The List interface is just a List i.e. you can add or remove. So a basic implementation of the List interface has to just provide those simple methods e.g. ArrayList. The Deque interface is the double ended Queue and iava's LinkedList IS-A Double Ended Queue.

软的没边 2024-11-12 02:42:25

IIRC,deque代表双端队列。在您提到的情况下,将通用 List 定义为双端队列是不合逻辑的。例如,ArrayList 不是为 Deque 接口设计的。插入在列表的末尾是有效的,但绝对不是在列表的开头(因为我认为这会导致整个数组的重新分配)。

另一方面,LinkedList 是为 Deque 接口完美设计的,因为它是一个双链表。

IIRC, deque stands for double end queue. In the case you mention, it's not logical to define a generic List as a deque. For instance, an ArrayList is not designed for the Deque interface. Insertions will be efficient in the end of the list, but absolutely not at its beginning (because it will cause the re-allocation of a whole array, I think).

The LinkedList is, on the other end, perfectly designed for the Deque interface, as it is a double linked list.

骄傲 2024-11-12 02:42:25

由于双端队列可能使用 LinkedList 以外的其他方式实现,并且代码可能依赖于任何具有此类功能的东西,因此 Deque 接口需要单独提供。

List 本身不应该实现/扩展 Deque,因为在列表开头添加/删除可能不是每个实现都能(轻松)支持的。

Because a double-ended queue might be implemented using something other than a LinkedList and one's code may depend on anything with such functionality, so the Deque interface needs to be available separately.

List should not itself implement/extend Deque because adding to/removing from the start of a list may not be something that can be (easily) supported by every implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文