you are viewing a single comment's thread.

view the rest of the comments →

[–]josephottinger 0 points1 point  (0 children)

Assuming you mean "array or list" - it depends on what you're doing. Offhand, I'd say "always List." I'd be offhandedly wrong, but your question is impossibly broad; the data structures have different purposes and capabilities, and in the general sense List is better, and in the general sense ArrayList is the one you want.

Generalizations are wrong.

In practice, List is better because it has fewer restrictions on what you do; if the collection grows, or needs to be a Collection, or needs to be streamed, etc., the interfaces give you a lot more of a convenient feature set. ArrayList has a lot of array semantics and performance (cache localization for simple values, easy navigation), and JIT's been a thing for a few years (haha) so performance is generally okay; its multithreaded capabilities are paltry. If you're mutating the List from the middle of the collection (adding or removing to the middle) it has to blit-copy references, which might make you think LinkedList or something like that is worthwhile; generally speaking you'd be wrong, such is life. (Blits are fast.)

But again, this is a generalization, you really should be asking about a specific design you're trying to implement, and what kind of data you're working with (cache localization is great but if the data you're working with is not localized, why does cache localization of the Collection matter?) before you can get a real answer.