Home » Core Java » Vector in Java

Vector in Java

Vector & its feature ::
1.  Vector class is an important class of Collection framework.
2.  Vector extends generic AbstractList.
3.  Vector implements generic List, RandomAccess, Cloneable and java.io.Serializable
4.  Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
5.  Vector can accept any element including multiple null values.
6. Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector’s storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
7.  Vector uses Iterator interface to traverse the elements.While traversing the elements using iterator if the      structure of the Vector is modified using add or remove method ,iterator will throw ConcurrentModificationException.So iterators for Vector are fail-fast.

Sample Program ::

Output ::

Now let us see an example of ConcurrentModificationException.In the below program while iterating the Vector,i am modifying the structure of the Vector by adding a new element.So I am getting ConcurrentModificationException.
Example of ConcurrentModificationException ::

Output ::


Leave a comment

Your email address will not be published. Required fields are marked *

12 − one =