使用 LinkedList 或 ArrayList 进行迭代 [英] Using a LinkedList or ArrayList for iteration
问题描述
如果我要向 List 添加未知数量的元素,并且该列表只会被迭代,那么在特定实例中 LinkedList 会比 ArrayList 更好吗(使用 Java,如果有任何相关性)
If I am be adding an unknown number of elements to a List, and that list is only going to be iterated through, would a LinkedList be better than an ArrayList in the particular instance (Using Java, if that has any relevance)
推荐答案
ArrayList
和 LinkedList
之间的性能权衡已经讨论过之前,但简而言之:ArrayList
在大多数现实生活中往往更快使用场景.ArrayList
将导致更少的内存碎片并与垃圾收集器更好地配合使用,它将使用更少的内存并允许更快的迭代,并且对于发生在列表末尾的插入会更快.
The performance trade-offs between ArrayList
and LinkedList
have been discussed before, but in short: ArrayList
tends to be faster for most real-life usage scenarios. ArrayList
will cause less memory fragmentation and will play nicer with the Garbage Collector, it will use up less memory and allow for faster iteration, and it will be faster for insertions that occur at the end of the list.
所以,只要列表中的插入总是发生在最后一个位置,就没有理由选择 LinkedList
- ArrayList
是明显的赢家.
So, as long as the insertions in the list always occur at the last position, there's no reason to pick LinkedList
- ArrayList
is the clear winner.
这篇关于使用 LinkedList 或 ArrayList 进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!