如果对列表执行的唯一操作是非随机访问(没有删除、添加或其他愚蠢行为),是否建议使用数组、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/