[英]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: 链接列表具有以下缺点:
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. 我不知道他们是否影响了基于Queue
和Stack
决定。
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.