简体   繁体   English

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. 据我所知,链表的概念是一堆对象,它们通过“下一个”,有时是“前一个”属性来遍历对象。

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. 我注意到在Java中,您可以创建一个LinkedList对象...但是使用相同的方法(如.add(),. get()等)将其视为数组/列表/序列。

So, is LinkedList internally an array-like sequence? 那么,LinkedList内部是一个类似数组的序列吗?

So, is LinkedList internally an array-like sequence? 那么,LinkedList内部是一个类似数组的序列吗?

No. It's a series of instances of a private nested class Entry , which has next , previous and element references. 不是。它是一系列私有嵌套类Entry的实例,它具有nextpreviouselement引用。 Note that you could have found this out yourself by looking at the source code, which comes with the JDK. 请注意,您可以通过查看JDK附带的源代码自己找到它。

The reason why this internal structure is not exposed is that it prevents the structure from becoming corrupted and eg containing a loop. 这种内部结构未暴露的原因是它防止结构被破坏并且例如包含环。 And the uniform access via the List and Deque interfaces allows polymorphic use. 通过ListDeque接口的统一访问允许多态使用。

LinkedList is a chain of entities in which every entity knows about next-one, so get(index) operation requires iterating over this chain with counter. LinkedList是一个实体链,其中每个实体都知道next-one,因此get(index)操作需要使用counter迭代此链。 But this list optimized for adding and deleting by position (in case when I need to put element inside list or delete element from middle linked list performs better) 但是这个列表针对按位置添加和删除进行了优化(如果我需要将元素放在列表中或从中间链表中删除元素表现更好)

A LinkedList in Java works just as you would expect it to do. Java中的LinkedList就像您期望的那样工作。 If you use the official Collections LinkedList then it will indeed be a a bunch of object connected to each other by having a 'next' and sometimes 'previous' . 如果你使用官方的Collections LinkedList,那么它确实是一堆通过“下一个”而有时是“前一个”相互连接的对象

And yes, it has a get(int index) method which is surprising because it would not be very efficient as you would need to start at the beginning and count your way up the list to find the index th entry and this is not what LinkedLists are good at. 是的,它有一个令人惊讶的get(int index)方法,因为它不是非常有效,因为你需要从头开始计算你的方式在列表中找到index条目,这不是什么LinkedLists擅长。 The reason it is there is because LinkedList implements the List interface. 之所以存在,是因为LinkedList实现了List接口。 This is something you can with ALL lists. 这是你可以使用所有列表的东西。

However, you would probably try to avoid using a LinkedList when most of your access to it is through the get(int index) method as that would clearly be most inefficient. 但是,当您对它的大多数访问都是通过get(int index)方法时,您可能会尝试避免使用LinkedList,因为这显然是效率最低的。 You would be perhaps better to use an ArrayList . 你可能会更好地使用ArrayList

as i know linkedlist is like what you said in the first paragraph. 因为我知道链接列表就像你在第一段中说的那样。 each object has two references, called it's previous and it's next. 每个对象有两个引用,称为前一个,下一个。 unlike array which works sequentially (in C it's exactly stored in sequence. i think java just does the same, that's why we can't re-size an array), list works by referencing. 不像顺序工作的数组(在C中它完全按顺序存储。我认为java只是做同样的事情,这就是为什么我们不能重新调整数组大小),列表通过引用工作。 so logically it works slower than array does. 所以逻辑上它比数组工作得慢。

It is possible to implement an object that works like an array, using a linked list. 可以使用链表实现一个像数组一样工作的对象。 Compared to arrays, some operations will be faster and some will be slower. 与数组相比,某些操作会更快,而某些操作会更慢。 For example, calling get() for an element near the end might be quite a lot slower with a linked list than with an array. 例如,对于链接列表而言,对于靠近末尾的元素调用get()可能比使用数组慢得多。 But, it's still possible. 但是,它仍然有可能。

On the other hand, deleting an element from the middle of a linked list will be faster than the corresponding operation done using arrays. 另一方面,从链表中间删除元素将比使用数组完成的相应操作更快。

Not really. 并不是的。 Linked list is provided by the collection and by good design you could create a double linked list. 链接列表由集合提供,通过良好的设计,您可以创建双链表。 Now it is not like array because these objects will not have indexes and are not elements within the container ie list. 现在它不像数组,因为这些对象不会有索引,也不是容器中的元素,即列表。 Hence you go through defining the next and previous and dictate what the next move is. 因此,您将通过定义下一个和上一个并指示下一步行动。 They all have the same methods because again they are collections like i said. 他们都有相同的方法,因为他们再次像我说的那样收藏。

So, is LinkedList internally an array-like sequence? 那么,LinkedList内部是一个类似数组的序列吗?

NO It is Doubly-linked list implementation. NO这是双向链表实现。

Internally it will be using what is called a 'slab allocator' -- this is basically a linked list of small arrays. 在内部,它将使用所谓的“slab allocator” - 这基本上是一个小型数组的链表。 Each time you add an object, it checks if there is space in the slab, and if so, puts the object there. 每次添加对象时,它都会检查平板中是否有空格,如果是,则将对象放在那里。 Otherwise, it allocates a new slab and puts the object in the first element of the slab. 否则,它会分配一个新的slab并将该对象放在slab的第一个元素中。

This technique minimizes memory allocation overheads -- if you add a lot of items to a linked list, that would be a lot of small memory allocations which take a lot of time, and the slab allocators minimize that 这种技术可以最大限度地减少内存分配开销 - 如果你将大量项目添加到链表中,那将是大量的小内存分配,这需要花费很多时间,并且slab分配器会最小化

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

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