Reputation: 6709
What determines whether one should be used over the other?
I used to think that the deciding factor is whether you know the size of the things you want to store but I think there might be more to it than that.
Upvotes: 3
Views: 6812
Reputation: 62864
Use array when you know the exact size of the collection and you don't expect to add/remove elements.
Use List
(ArrayList
) when you don't know the exact size of the collection and you expect to alter it at some point.
If you're using Java8, there is the Stream API
, which helps to significantly reduce the boilerplate code when working with collections. This is another plus for ArrayList
(and all Collections and Maps).
More info:
Upvotes: 4
Reputation: 66
Array is fixed, ArrayList is growable.If the number of elements is fixed, use an array Also one of the great benefits of collection implementations is they give you a lot of flexibility. So depending on your need, you can have a List behave as an ArrayList or as a LinkedList and so on. Also if you look at the Collection API, you'd see you have methods for almost everything you'd ever need to do.
Upvotes: 0
Reputation: 46841
Some more differences:
First and Major difference between Array and ArrayList in Java is that Array is a fixed length data structure while ArrayList is a variable length
Collection class. You can not change length of Array once created in Java but ArrayList re-size itself when gets full depending upon capacity and load factor. Since ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance
as it involves creating new Array and copying content from old array to new array.
Another difference between Array and ArrayList in Java is that you can not use Generics along with Array, as Array instance knows about what kind of type it can hold and throws ArrayStoreException
, if you try to store type which is not convertible into type of Array. ArrayList allows you to use Generics to ensure type-safety
.
One more major difference between ArrayList and Array is that, you can not store primitives
in ArrayList, it can only contain Objects. While Array can contain both primitives and Objects in Java. Though Autoboxing of Java 5 may give you an impression of storing primitives in ArrayList, it actually automatically converts primitives to Object.
Java provides add()
method to insert element into ArrayList and you can simply use assignment operator to store element into Array e.g. In order to store Object to specified position.
One more difference on Array vs ArrayList is that you can create instance of ArrayList without specifying size
, Java will create Array List with default size but its mandatory to provide size of Array while creating either directly or indirectly by initializing Array while creating it. By the way you can also initialize ArrayList while creating it.
Upvotes: 5
Reputation: 420
Generally, I use ArrayList, not arrays, because they offer a lot of several methods that are very usefull. I think you can use array if performance is very important, in very special cases.
Upvotes: 0
Reputation: 58868
Unless speed is critical (really critical, like every microsecond counts), use ArrayList
whenever possible. It's so much easier to use, and that's usually the most important thing to consider.
Upvotes: 3