Array Vs Arraylist in Java - Difference between Array and Arraylist
Array Vs Arraylist in Java - Difference between Array and Arraylist
Comparison Index | Array | Arraylist |
---|---|---|
Definition | An array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location. | The ArrayList is a class of Java Collections framework. It contains popular classes like Vector, HashTable, and HashMap. |
Dimensionality | It can be single-dimensional or multidimensional | It can only be single-dimensional |
Resizable | An array is a fixed-length data structure. | ArrayList is a variable-length data structure. It can be resized itself when needed. |
Traversing Elements | For and for each generally is used for iterating over arrays | Here iterator is used to traverse over ArrayList |
Length | length keyword can give the total size of the array. | size() method is used to compute the size of ArrayList. |
Size | It is static and of fixed length. | It is dynamic and can be increased or decreased in size when required. |
Speed | It is faster as above we see it of fixed size | It is relatively slower because of its dynamic nature |
Primitive/ Generic type | An array can store both objects and primitives type. | We cannot store primitive type in ArrayList. It automatically converts primitive type to object. |
Adding Elements | The additions are done with the assignment operator. | ArrayList uses add() method for performing additions |