Array or List in Java. Which is faster?

后端 未结 30 2148
滥情空心
滥情空心 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 04:58

    Array vs. List choice is not so important (considering performance) in the case of storing string objects. Because both array and list will store string object references, not the actual objects.

    1. If the number of strings is almost constant then use an array (or ArrayList). But if the number varies too much then you'd better use LinkedList.
    2. If there is (or will be) a need for adding or deleting elements in the middle, then you certainly have to use LinkedList.

提交回复
热议问题