Collections - Arraylist vs Arrays
Table of Contents
Collections - Arraylist vs Arrays
Some of the advantages ArrayList has over arrays are:
- It can grow dynamically. The array is a specified-length data structure whereas ArrayList is a variable-length Collection class.
- It provides more powerful insertion and search mechanisms than arrays
Feature | Array | ArrayList |
---|---|---|
Dimensionality | It can be single-dimensional or multidimensional (win) | It can only be single-dimensional |
Traversing Elements | For and for each generally is used for iterating over arrays | Here iterator is used to traverse riverArrayList |
Length | length() | size() |
Size | It is static and of fixed length | It is dynamic and can be increased or decreased in size when required (win) |
Speed | It is faster as above we see it of fixed size (win) | It is relatively slower because of its dynamic nature |
Primitive Datatype Storage | Primitive data types can be stored directly unlikely objects (win) | Primitive data types are not directly added unlikely arrays, they are added indirectly with help of autoboxing and unboxing |
Generics | They can not be added here hence type unsafe | They can be added here hence makingArrayList type-safe (win) |
Adding Elements | Using assignment operator | Using the add() method |