简体   繁体   English

在Java中使用基于Vector的Stack实现而不是链表的动机是什么?

[英]What is the motivation of using a Vector based implementation of a Stack in Java instead of a linked list?

What was the motivation of using a Vector based implementation of a Stack in Java instead over a linked list implementation? 什么是使用的动机Vector一个基于实现Stack在Java中,而不是在一个链表实现? I realize a Vector is synchronized and that has inherit advantages (and overhead), but I feel like not only are these data structures typically taught in texts as linked list based structures but LLs avoid costly resizes as the underlying array fills. 我意识到Vector是同步的并且具有继承优势(和开销),但我觉得这些数据结构通常不仅仅是作为基于链表的结构在文本中教授,而且LL会避免代价高昂的调整,因为底层数组填充。

I do understand that Vectors , using amortized analysis, are O(1) even with the resizes. 我确实理解使用摊销分析的Vectors即使有调整大小也是O(1)。 So maybe taking this into consideration it doesn't make much of a difference, but I would be curious to understand the rationale none the less. 因此,考虑到这一点并没有太大的区别,但我很想知道理由。

Linked-lists have the following disadvantages: 链接列表具有以下缺点:

  • Per-element storage overhead 每元素存储开销
  • Computational complexity from having to follow pointers/references from element to element 必须遵循从元素到元素的指针/引用的计算复杂性
  • Bad cache locality 缓存局部性不好

Of course, these are just generic disadvantages of linked-lists; 当然,这些只是链表的一般缺点; I have no idea whether they affected the decision for what to base Queue and Stack on. 我不知道他们是否影响了基于QueueStack决定。

A vector stores its data in contiguous memory. 向量将其数据存储在连续的内存中。 This is good for caching. 这对缓存很有用。

A linked list can become very fragmented in memory. 链表可能会在内存中变得非常碎片化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM