问题描述
我不知道为什么的LinkedList
没有参数:initialCapacity
。
我知道好的时候使用的ArrayList
当的LinkedList
。
I know good when to use ArrayList
and when LinkedList
.
其良好的实践来定义集合最终大小,如:
Its good practice to define Collection final size like:
List<String> arraylist = new ArrayList<String>(5);
有关的LinkedList
例如:
List<String> linkedlist = new LinkedList<String>(); // right way
但
List<String> arraylist = new LinkedList<String>(5); // compilation error
能否代码别人的$ P $垫在这个问题上的光?
Can somebody spread a light on that issue?
顺便说一句,我可以写
List<String> arraylist = new ArrayList<String>(5);
List<String> linkedlist = new LinkedList<String>(arraylist);
推荐答案
链表由性质不具有容量,由于项目被添加到列表之前它不分配存储器的项目。在一个链表的每个项目包含一个指针列表中的下一个
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.
有将是事先分配内存到列表中没有意义的,因为链表不具备的容量的
There would be no point in allocating memory to the list beforehand, since LinkedList does not have capacity.
这篇关于为什么链表没有参数:initialCapacity在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!