Java Vector Tutorial
Java Vector is a resizable array of Objects which implements the
List interface (As of JDK 1.2, Java Vector is retrofitted to implement
List interface, and thus became a part of Java Collection framework).
Like an array, elements of the Java Vector can be accessed using
an index.
Java Vector is similar to Java ArrayList, but it is synchronized.
Java Vector Capacity
Capacity of the Java Vector is the actual size of the array used
to store the elements. It is usually larger than the size of the
Vector. When more components are added to the Vector, its size is
increased by the size of capacityIncrement.
Program should increase the capacity of Vector before inserting
large number of elements to the Vector. It reduces the number of
reallocations Vector has to make to fit the elements and thus improves
the overall performance.
We can increase the capacity of the Vector using following method.
Vector v = new Vector();
v.ensureCapacity(80);
Java Vector Iterators
Vector provides two types of Iterators.
1) Iterator
2) ListIterator
Using following methods.
Iterator iterator = v.iterator();
Returns Iterator object.
ListIterator listIterator = v.listIterator();
Returns Object of ListIterator.
ListIterator listIterator = v.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.
|