Another general purpose implementation of List interface in Java is ArrayList.
Interfaces implemented - List, Collection, RandomAccess, Serializable, Cloneable, Iterable
Classes extends from - AbstractCollection, AbstractList
Structurally, it maintains an array to store the data. Initially the array is created with a size of 10 and when that limit is reached (which implies that the load factor is 1), a new array is created with 1.5 times the original arrays capacity and the data from the existing array is copied using System.arraycopy().
Features
Interfaces implemented - List, Collection, RandomAccess, Serializable, Cloneable, Iterable
Classes extends from - AbstractCollection, AbstractList
Structurally, it maintains an array to store the data. Initially the array is created with a size of 10 and when that limit is reached (which implies that the load factor is 1), a new array is created with 1.5 times the original arrays capacity and the data from the existing array is copied using System.arraycopy().
Features
- provides random access
- non-synchronized data structure (Collections.synchronizedList() can be used to reverse the behavior)
- Iterator and ListIterator returned from ArrayList are fail-fast (throws ConcurrentModificationException when the structure changes by any method other than the iterators' methods)
- insertion order is retained in ArrayList
- has one tuning parameter initial capacity which refers to the size of the array before it grows
- O(1) for searching as ArrayList maintains an index for each element which makes the searching an easy affair
- Addition and deletion operations have O(n) as all the elements need to be repositioned in the array to fill once an element is added or deleted.
Comments