In Java, when would it be preferential to use a List rather than an Array?
Java – When to Use a List Over an Array
arraysdata-structuresjavalist
Related Solutions
You should generally prefer to choose the right data structure for the job. You need to understand your task at hand as well as all the different options you have and how they compare in terms of iteration, and searching, and adding, removing, and inserting data. In general, you need to understand how the data structure accesses and manipulates memory and choose the best data structure based on how you anticipate your application will be used.
Obviously, it isn't always clear-cut. But you can understand the ideals for different data structures.
For example, purely static, fixed length data in which you'll only iterate, without a need for search, is ideal for an array. It's common to use such arrays in cipher algorithms. If the data is static but instead of iterating, you need to search, you might want some type of tree structure. If you want fast insertion, hashing is probably the ideal. If the data changes often, you want a structure that is efficient at changing its size, like a list.
Of course, there's many variations and combinations of data structures designed to solve all kinds of specific problems. The reason there are so many is because of the importance they play in writing efficient programs. Anyway, my point is, learn about data structures. Understand the ideal situations for each and then you'll be able to decide or design suitable data structures for any task.
In general (and in Java) an array is a data structure consisting of sequential memory storing a collection of objects.
List
is an interface in Java, which means that it may have multiple implementations. One of these implementations is ArrayList
, which is a class that implements the behavior of the List
interface using arrays as the data structure.
There are a number of other classes that implement the List
interface. One easy way to take a look at them is by viewing the Javadoc for List
. On this page, you'll see "all known implementing classes," which are all of the kinds of lists in Java.
Best Answer
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 (e.g.: 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 data structure is a better fit than a List for what you are trying to do.
Each data structure, and implementation, 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:
As an example:
vs