x

Java – Vector Class

Prev     Next

  • Vector class implements the List interface.
  • It uses array data structure internally to represent the elements in it. It is similar to that of ArrayList except that the vector class is synchronized. It is thread safe.

Creating a Vector class:

There are 3 ways to create a vector list.


1. Syntax:

Vector obj = new Vector(); //without Generics
Vector<DataType> obj = new Vector<DataType>(); //using Generics
  • By default, the capacity of the vector is 10. Whenever an element is added beyond the capacity of a vector, the capacity of the vector is doubled.

2. Syntax:

Vector obj = new Vector(int initialCapacity);
  • Creates a vector ‘obj’  with the specified capacity. Whenever an element is added beyond the initialCapacity, the capacity of the vector is doubled.

Example:

Vector frequency = new Vector(30); //initial capacity is 30

3. Syntax:

Vector obj = new Vector(int initialcapacity, int capacityIncrement );
  • Creates a vector ‘obj’  with the specified capacity. Whenever an element is added beyond the initialCapacity, the capacity of the vector is incremented by the value specified as ‘capacityIncrement’.

Example:

Vector frequency = new Vector(20, 5); //initial capacity is 20 and capacityIncrement is 5

When we insert the 21st element, the size of the vector will increase by 5 i.e the capacity will become 25.

Methods of Vector:

Methods of Vector Description
boolean add(E e) Appends the element e to the end of the list.
void add(int i, E element) Inserts the element e at the specified index ‘i’ in the list.
void addElement(E obj) Inserts the element to the vector and increases the vector size by 1. This method is synchronised.
int capacity() Returns the current capacity of the vector.
boolean contains(Object o) Returns true if the list contains the element specified in the argument.
E elementAt(int index) Returns the element in the given index.
boolean isEmpty() Returns true if the list has no elements
void ensureCapacity(int minCapacity) Ensures that the specified capacity is maintained. Increases the capacity if needed.
void insertElementAt(Object obj, int index) Inserts the object ‘obj’ in the vector at the specified index
E remove(int index) Removes the element at the specified index in the vector.
boolean remove(Object o) Removes the first occurrence of the object from the vector.


The vector is unchanged if no such element is present.

void removeElementAt(int index) Removes the element specified in the given index.
int size() Returns the number of elements in the list.
E set(int index, E element) Replaces the element at the specified index in the vector with the given element.
boolean addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of the vector.
boolean removeAll(Collection<?> c) Removes all the elements that are contained in the specified collection from the vector.

Adding elements in the Vector:

Elements can be added to a vector object by using add() method.

Syntax:

add(E e)

To Add an element at a specific index.

Syntax:

add(int index, Element e)

Example:

Output:

Char:h
Char:i
Char:s

Removing an Element from the Vector:

Elements can be removed from a vector using remove() method..

Syntax:

remove(Object obj)

The above method removes the object obj from the vector.

Syntax:

remove(int index)

The above method removes the element from the position (index) specified in the argument.

Example:

Output:

[h,i,s]

After removing ‘h’ from the vector:[i,s]

Finding the Length of the Vector:

The length of an Vector can be found using size() method of the Vector class.

Example:

Output:

Names: [“Anjali”,”Anu”,”Bala”,”Anu”]

Hari added to the string vector

Size after adding an element:5

Prev     Next



Like it? Please Spread the word!