为什么LinkedList 在java 中没有initialCapacity?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19050211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why LinkedList doesn't have initialCapacity in java?
提问by Maxim Shoustin
I wonder why LinkedList
doesn't have initialCapacity
.
我想知道为什么LinkedList
没有initialCapacity
.
I know good when to use ArrayList
and when LinkedList
.
我知道什么时候用ArrayList
,什么时候用LinkedList
。
Its good practice to define Collection final size like:
定义集合最终大小的好习惯,如:
List<String> arraylist = new ArrayList<String>(5);
For LinkedList
for example:
对于LinkedList
例如:
List<String> linkedlist = new LinkedList<String>(); // right way
but
但
List<String> arraylist = new LinkedList<String>(5); // compilation error
Can somebody spread a light on that issue?
有人可以就这个问题发表一些看法吗?
[EDIT]
[编辑]
BTW, I can write
BTW,我可以写
List<String> arraylist = new ArrayList<String>(5);
List<String> linkedlist = new LinkedList<String>(arraylist);
采纳答案by Kimi
LinkedList by nature does not have "capacity", since it does not allocate memory to the items before the items are added to the list. Each item in a LinkedList holds a pointer to the next in the list.
LinkedList 本质上没有“容量”,因为它在将项目添加到列表之前不会为项目分配内存。LinkedList 中的每一项都持有一个指向列表中下一项的指针。
There would be no point in allocating memory to the list beforehand, since LinkedList does not have capacity.
事先为列表分配内存是没有意义的,因为 LinkedList 没有容量。
回答by Hovercraft Full Of Eels
Its model is not based on an array but rather a true linked list, and so there is no need and further it would not make sense. It doesn't make much sense to have empty links like you have empty array items.
它的模型不是基于数组,而是基于真正的链表,因此没有必要,而且没有意义。像空数组项那样空链接没有多大意义。
回答by Gabriel Negut
Why would LinkedList
have an initial capacity?
为什么LinkedList
会有初始容量?
ArrayList
is backed up by an array, so the initial capacity is the initial size of the array. LinkedList
has no need of that.
ArrayList
由数组备份,因此初始容量是数组的初始大小。LinkedList
不需要那个。
回答by Carsten Hoffmann
Why would you need a capacity on a LinkedList? A LinkedList does not work with fixed sized arrays. Every LinkedListElement has a pointer (a link!) to the next Element in the list. Which Because of that it is possible to add an element to a linked list in constant time. But it is costly to have random access to the elements in the List. You need to go through all the Elements in the list until you reach your destination.
为什么需要 LinkedList 上的容量?LinkedList 不适用于固定大小的数组。每个 LinkedListElement 都有一个指向列表中下一个 Element 的指针(链接!)。因此,可以在恒定时间内将元素添加到链表中。但是随机访问 List 中的元素代价高昂。您需要遍历列表中的所有元素,直到到达目的地。
回答by Arjun
Linkedlist does not need an initial value. Thats is the primary difference between array and linked list.
Linkedlist 不需要初始值。这就是数组和链表之间的主要区别。
array will end somewhere. But linkedlist not. Linked list does not work on boundary values.
数组将在某处结束。但链表不是。链表对边界值不起作用。
回答by Arash Saidi
When you declare an array you have to know its size because pointers need to be created in memory. A linked list does not need this because there is no need for pointers to memory before any object is added to the list.
当你声明一个数组时,你必须知道它的大小,因为需要在内存中创建指针。链表不需要这个,因为在将任何对象添加到链表之前不需要指向内存的指针。
A linked list is defined recursively as: an empty list en element that points to the empty list
一个链表递归地定义为:一个空列表 en 指向空列表的元素
therefore whenever you add an element, you allocate memory (or rather in Java the compiler does this) when you create the element, and then when you add it to the list it now points to the list (or the last element in the list points to it).
因此,每当您添加元素时,您都会在创建元素时分配内存(或者在 Java 中编译器会这样做),然后当您将其添加到列表时,它现在指向列表(或列表中的最后一个元素指向到它)。
So you don't need to declare initial size of linked list because a linked list always starts with the empty list, and when an element is added it points to the list.
所以你不需要声明链表的初始大小,因为链表总是从空列表开始,当添加元素时它指向列表。