LinkedList在Java内部如何工作? [英] How does LinkedList work internally in Java?
问题描述
据我所知,链表的概念是一堆对象,它们通过具有下一个"(有时是上一个")属性来遍历对象而彼此连接.
As far as I know, the concept of a linked list is a bunch of object connected to each other by having a 'next' and sometimes 'previous' attribute to traverse the objects.
我注意到在Java中,您可以创建一个LinkedList对象...但是可以使用.add()、. get()等相同的方法将其视为数组/列表/序列.
I noticed in Java, you can create a LinkedList object...but treat it like an array/list/sequence by using the same methods such as .add(), .get(), etc.
那么,LinkedList在内部是一个类似数组的序列吗?
So, is LinkedList internally an array-like sequence?
推荐答案
那么,LinkedList在内部是一个类似数组的序列吗?
So, is LinkedList internally an array-like sequence?
不.它是私有嵌套类 Entry
的一系列实例,该类具有 next
, previous
和 element
引用.请注意,您可以通过查看JDK随附的源代码自己找到它.
No. It's a series of instances of a private nested class Entry
, which has next
, previous
and element
references. Note that you could have found this out yourself by looking at the source code, which comes with the JDK.
不公开此内部结构的原因是,它可以防止结构损坏,例如包含一个循环.通过 List
和 Deque
接口的统一访问允许多态使用.
The reason why this internal structure is not exposed is that it prevents the structure from becoming corrupted and e.g. containing a loop. And the uniform access via the List
and Deque
interfaces allows polymorphic use.
这篇关于LinkedList在Java内部如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!