Array or List in Java. Which is faster?

后端 未结 30 2146
滥情空心
滥情空心 2020-11-22 04:30

I have to keep thousands of strings in memory to be accessed serially in Java. Should I store them in an array or should I use some kind of List ?

Since arrays keep

30条回答
  •  独厮守ぢ
    2020-11-22 05:03

    Depending on the implementation. it's possible that an array of primitive types will be smaller and more efficient than ArrayList. This is because the array will store the values directly in a contiguous block of memory, while the simplest ArrayList implementation will store pointers to each value. On a 64-bit platform especially, this can make a huge difference.

    Of course, it's possible for the jvm implementation to have a special case for this situation, in which case the performance will be the same.

提交回复
热议问题