Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does an ArrayList compare to a dynamic array

Is an ArrayList is just the interface for a dynamic array? Or are they the same thing?

like: ArrayList corresponds to dynamic array, HashMap corresponds to Map ?

except I don't see any Java API for something like a dynamic array, unless it is ArrayList?

like image 609
bezzoon Avatar asked Nov 07 '13 17:11

bezzoon


1 Answers

Yes. In short.

A longer explanation is that an ArrayList is a collection that uses arrays for storage, rather than a linked list, doubly linked list or similar. This means that it gives all the benefits of using an Array, whilst Java looks after the mechanics of sizing the Array for you (dynamically).

I seem to remember that the initial array is created with a default maximum size (which can be specified by the user). Should the collection run out of space, then a larger array is created and the contents of the original array copied into the new one. The increment in size is set to prevent this happening too often, as the operation is fairly costly.

Java also offers the Vector collection which is similar, but is also thread safe, see: What are the differences between ArrayList and Vector?.

like image 176
Henry Florence Avatar answered Oct 20 '22 01:10

Henry Florence