James Lockhart
James Lockhart

Reputation: 141

Array or ArrayList

I am familiar with using arrays but recently came across ArrayList and was wondering if you would typically use ArrayList more than just a regular Array?

For reference an ArrayList looks like :

ArrayList<Dog> myDogArrayList = new ArrayList<Dog>();

Regular Array:

String [] Dog = new String(type)

Upvotes: 1

Views: 50

Answers (1)

mp911de
mp911de

Reputation: 18119

ArrayLists (or, in general, the Collections) are intended for heavy read and write operation without thinking too much about capacity, extension and so forth (in the first place). It is much more comfortable to call ArrayList#add than creating a new array with size oldArray.length + 1, doing a System.arraycopy and adding then the element. There are lots of handy methods (indexOf, remove, sublist) which leverage the complexity of use in comparison to arrays.

If you take a look at the Collections class, you'll find a lot of convenience methods like shuffle or reverse.

The use of List in general is much more common than arrays, except you're heading for performance.

Upvotes: 1

Related Questions