Java ArrayList Tutorial
Java ArrayList is a resizable array which implements List interface.
ArrayList provides all operation defined by List interface. Internally
ArrayList uses array to store elements. ArrayList provides addition
methods to manipulate this array that actually stores the elements.
ArrayList is equivalent to Vector, but ArrayList is not synchronized.
Java ArrayList Capacity
Capacity of the Java ArrayList is the actual size of the array
used to store the list elements. Capacity of the ArrayList grows
automatically as we add elements to it. Every time this happens
the internal array has to be reallocated. This increases the load.
We can set increase the capacity of the ArrayList using following
method.
ArrayList arrayList = new ArrayList();
arrayList.ensureCapacity(100);
Java ArrayList Iterators
Java ArrayList provides two types of Iterators.
1) Iterator
2) ListIterator
Using following methods.
Iterator iterator = arrayList.iterator();
Returns Iterator object.
ListIterator listIterator = arrayList.listIterator();
Returns Object of ListIterator.
ListIterator listIterator = arrayList.listIterator(int startIndex);
Returns object of ListIterator. The first next method call on this
ListIterator object will return the element at the specified index
passed to get the ListIterator object.
Iterators returned by these methods are fail-fast. That means if
the list is modified after getting the Iterator by using some other
means rather than Iterators own add or remove method, Iterator will
throw ConcurrentModificationException.
|