Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a List over an Array in Java?

In Java, when would it be preferential to use a List rather than an Array?

like image 871
John Moffitt Avatar asked Oct 19 '09 16:10

John Moffitt


People also ask

When would you use a list over an array Java?

The ArrayList one the other hand is a class in Java Collection framework which was introduced as a dynamic array. Since an array is static in nature i.e. you cannot change the size of an array once created, So, if you need an array which can resize itself then you should use the ArrayList.

Should I use list or array Java?

One of the major differences is between Java List vs Array List is that list is an interface, and the Array list is a standard collection class. The Java List interface extends the Collection, and the Array list extends the abstract listening class, and it can also implement the List interface.

Why is a list better than an array?

The list is better for frequent insertion and deletion, whereas Arrays are much better suited for frequent access of elements scenario. List occupies much more memory as every node defined the List has its own memory set whereas Arrays are memory-efficient data structure.


2 Answers

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

like image 72
Tom Neyland Avatar answered Oct 08 '22 23:10

Tom Neyland


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.
like image 34
Tom Hawtin - tackline Avatar answered Oct 09 '22 01:10

Tom Hawtin - tackline