When to use a List over an Array in Java? When to use a List over an Array in Java? arrays arrays

When to use a List over an Array in Java?


I see the question as being the opposite-

When should you use an Array over a List?

Only you have a specific reason to do so (eg: Project Constraints, Memory Concerns (not really a good reason), etc.)

Lists are much easier to use (imo), and have much more functionality.

Note: You should also consider whether or not something like a Set, or another datastructure is a better fit than a List for what you are trying to do.

Each datastructure, and implmentation, has different pros/cons. Pick the ones that excel at the things that you need to do.

If you need get() to be O(1) for any item? Likely use an ArrayList, Need O(1) insert()? Possibly a Linked List. Need O(1) contains()? Possibly a Hashset.

TLDR: Each data structure is good at some things, and bad at others. Look at your objectives and choose the data structure that best fits the given problem.

Edit:

One thing not noted is that you're better off declaring the variable as its interface (i.e. List or Queue) rather than its implementing class. This way, you can change the implementation at some later date without changing anything else in the code.

As an example:

List<String> myList = new ArrayList<String>(); 

vs

List<String> myList = new LinkedList<String>(); 

Note that myList is a List in both examples. --R. Bemrose


Rules of thumb:

  • Use a List for reference types.
  • Use arrays for primitives.
  • If you have to deal with an API that is using arrays, it might be useful to use arrays. OTOH, it may be useful to enforce defensive copying with the type system by using Lists.
  • If you are doing a lot of List type operations on the sequence and it is not in a performance/memory critical section, then use List.
  • Low-level optimisations may use arrays. Expect nastiness with low-level optimisations.


Most people have answered it already.

There are almost no good reason to use an array instead of List. The main exception being the primitive array (like int[]). You cannot create a primitive list (must have List<Integer>).

The most important difference is that when using List you can decide what implementation will be used. The most obvious is to chose LinkedList or ArrayList.

I would like to point out in this answer that choosing the implementation gives you very fine grained control over the data that is simply not available to array:

  1. You can prevent client from modifying your list by wrapping your list in a Collection.unmodifiableList
  2. You can synchronize a list for multithreading using Collection.synchronizedList
  3. You can create a fixed length queue with implementation of LinkedBlockingQueue
  4. ... etc

In any case, even if you don't want (now) any extra feature of the list. Just use an ArrayList and size it with the size of the array you would have created. It will use an Array in the back-end and the performance difference with a real array will be negligible. (except for primitive arrays)