java - 在 LinkedList 和 ArrayList 之间没有明显优势的情况下,应该使用哪个?

标签 java arrays performance arraylist linked-list

如果对列表执行的唯一操作是非随机访问(没有删除、添加或其他愚蠢行为),是否建议使用数组、ArrayList、LinkedList 或其他东西?还是选择哪个无关紧要?

最佳答案

除非在某些特定情况下(如嵌入式系统),否则您可能会使用分层和/或虚拟内存系统。

虽然操作系统或硬件已经充分抽象了其实现细节以使其对您透明,但仍有一些重要的注意事项。

与独立链接的元素相比,基于数组的实现将在元素之间表现出更高程度的空间局部性。

http://en.wikipedia.org/wiki/Locality_of_reference

In computer science, locality of reference, also known as the principle of locality, is the phenomenon of the same value or related storage locations being frequently accessed. There are two basic types of reference locality. Temporal locality refers to the reuse of specific data and/or resources within relatively small time durations. Spatial locality refers to the use of data elements within relatively close storage locations. Sequential locality, a special case of spatial locality, occurs when data elements are arranged and accessed linearly, e.g., traversing the elements in a one-dimensional array.

Locality is merely one type of predictable behavior that occurs in computer systems. Systems which exhibit strong locality of reference are good candidates for performance optimization through the use of techniques, like the cache and instruction prefetch technology for memory, or like the advanced branch predictor at the pipelining of processors.

基于此,在所有其他条件相同的情况下,我会选择 ArrayList 而不是 LinkedList。

关于java - 在 LinkedList 和 ArrayList 之间没有明显优势的情况下,应该使用哪个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586003/

相关文章:

java - 计算 JSONArray 有多少个特定的 JSONObject

javascript - 基于响应式布局设置固定高度 Pane

wpf - Visual Studio 2012 - 性能分析 Prism 应用程序

c++ - 为什么单独循环中的元素加法比组合循环中的元素加法快得多?

java - FileReader 已在此编译单元中定义错误 Java

javax.crypto.BadPaddingException : pad block corrupted exception

java - Apache POI : Storing key and sum in a Map

python - 如何找到二维数组中每一行的最大值?

java - 使用 Java 生成的内容向 Outlook 发送电子邮件

java - java中ArrayIndex越界异常