Why is it preferred to use Lists instead of Arrays in Java?

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.


Lists can easily grow in size, and you can add and remove elements in the middle of the list easily. That cannot be done with arrays. You need to consider what you need the list for though. If you don't think the list is going to change a lot, then use an array instead.


From Array vs ArrayList

An ArrayList is better than Array to use when you have no knowledge in advance about elements number. ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays if possible.


One thing to keep in mind is that the Java Collections classes favor general purpose ease of use over optimization for specific scenarios. So, as a previous responder said, you really need to consider how you're going to use it.

For example, if you're creating "large" data structures, then ArrayList can get pretty inefficient. Each time you hit the limit of the array, it allocates a new one at (I believe) 2x the size. So on average an ArrayList is only going to be 75% utilized.

In general one can consider the Java Collections to be first approximations that are usually good enough most of the time, and when you have measurable performance issues you should be prepared to use alternate, more specialized Collection implementations.

In the case you mention, you can consider ArrayList to be just a more convenient way to deal with an array.