Skip to main content Skip to docs navigation
TechSpiderTutorials

java.util.Vector in Java

java.util.Vector class

The 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.

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.

Creating Objects

public Vector()

Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.

public Vector(int initialCapacity,int capacityIncrement)

Constructs an empty vector with the specified initial capacity and capacity increment.

public Vector(int initialCapacity)

Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.

public Vector(Collection<? extends E> c)

Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.

java.util.Vector class Methods

public boolean add(E e)

Appends the specified element to the end of this Vector.


public void add(int index, E element)

Inserts the specified element at the specified position in this Vector. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

public boolean remove(Object o)

Removes the first occurrence of the specified element in this Vector If the Vector does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

public E remove(int index)

Removes the element at the specified position in this Vector. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the Vector.

public int capacity()

Returns the current capacity of this vector.


public void clear()

Removes all of the elements from this Vector. The Vector will be empty after this call returns (unless it throws an exception).


public boolean containsAll(Collection<?> c)

Returns true if this Vector contains all of the elements in the specified Collection.


public boolean addAll(Collection<? extends E> c)

Appends all of the elements in the specified Collection to the end of this Vector, in the order that they are returned by the specified Collection's Iterator.


public boolean removeAll(Collection<?> c)

Removes from this Vector all of its elements that are contained in the specified Collection.


public List<E> subList(int fromIndex,int toIndex)

Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned List is empty.) The returned List is backed by this List, so changes in the returned List are reflected in this List, and vice-versa. The returned List supports all of the optional List operations supported by this List.


public ListIterator<E> listIterator(int index)

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.


public Iterator<E> iterator()

Returns an iterator over the elements in this list in proper sequence.


java.util.Vector class Example

import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        // Creating a Vector
        Vector<Strin> vector = new Vector<>();

        // Adding elements to the Vector
        vector.add("Apple");
        vector.add("Banana");
        vector.add("Cherry");
        vector.add("Date");

        // Displaying the elements
        System.out.println("Vector elements: " + vector);

        // Accessing an element
        String firstElement = vector.get(0);
        System.out.println("First element: " + firstElement);

        // Checking the size of the Vector
        System.out.println("Size of vector: " + vector.size());

        // Removing an element
        vector.remove("Banana");
        System.out.println("After removing 'Banana': " + vector);

        // Iterating through the Vector
        System.out.println("Iterating through Vector:");
        for (String fruit : vector) {
            System.out.println(fruit);
        }

        // Checking if Vector contains an element
        if (vector.contains("Cherry")) {
            System.out.println("Vector contains 'Cherry'");
        }

        // Clearing the Vector
        vector.clear();
        System.out.println("After clearing, size of vector: " + vector.size());
    }
}

Output:

Vector elements: [Apple, Banana, Cherry, Date]
First element: Apple
Size of vector: 4
After removing 'Banana': [Apple, Cherry, Date]
Iterating through Vector:
Apple
Cherry
Date
Vector contains 'Cherry'
After clearing, size of vector: 0

java.util.Vector Videos

java.util.Vector class: part-2
How to get the elements from vector class