Java ArrayList is a resizable array which implements List interface. ArrayList provides all operation defined by List interface. Internally ArrayList uses an array to store its elements. ArrayList provides additional methods to manipulate the array that actually stores the elements.
ArrayList is equivalent to Vector, but ArrayList is not synchronized.
Java ArrayList Capacity
Capacity of an ArrayList is the size of the array used to store the list elements. It 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 the initial 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
Iterator iterator = arrayList.iterator();
Returns object of Iterator.
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.
ArrayList Constructors
1) ArrayList()
Creates an empty ArrayList.
For example,
ArrayList arrayList = new ArrayList();
2) ArrayList(int capacity)
Creates an ArrayList with specified initial capacity.
For example,
ArrayList arrayList = new ArrayList(10);
3) ArrayList(Collection c)
Creates an ArrayList containing elements of the collection specified.
For example,
ArrayList arrayList = new ArrayList(myCollection);
Where myCollection is an object of the type Collection. This creates an ArrayList of elements contained in the myCollection, in the order returned by the myCollection’s Iterator.
Java ArrayList Examples
Bookmark/Search this post with:
Helpfull!!!
Hi
This is a very helpfull website.Please cover Threads also.
Regards
asha
Read & Write method
This is very helpful. Is it possible to show an example of the write/read methods?
helpful
it is very useful.please give many programs.
Better way
This content is help and serves better for any beginner to collections framework
making progress
Thanks for the great programs and annotation. It really helps.
Superb
Please give some examples for xml parsing
Post new comment