Vector in Java

Naveen AnamaneniUpdated · 4 min read

Vector in Java is a class that implements a growable array of objects, much like ArrayList. It is used to store an ordered list of elements that can be accessed by index. The main difference is that every method of Vector is synchronized, so only one thread can use it at a time. Vector has existed since Java 1.0, before the Collections Framework, and is now called a legacy class. In this lesson, you will learn how Vector works, its methods, and why modern code uses other classes instead.

Declaration

Declaration
public class Vector<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, Serializable

In Java 1.2, Vector was updated to implement List, so it has all the usual list methods as well as its own older ones.

Features of Vector

  • Resizable array: like ArrayList, it grows as needed.
  • Synchronized: every method locks the vector, so it is thread-safe for single calls.
  • Doubles in size: when full, its capacity doubles by default (an ArrayList grows by half).
  • Default capacity 10: allocated as soon as the vector is created.
  • Legacy methods: such as addElement, elementAt and elements().

Methods of Vector

MethodDescription
void addElement(E e)It adds an element at the end (old name for add).
E elementAt(int index)It returns the element at the index (old name for get).
E firstElement()It returns the first element.
E lastElement()It returns the last element.
void removeElementAt(int index)It removes the element at the index.
int capacity()It returns the current capacity.
Enumeration<E> elements()It returns an old-style Enumeration over the elements.

Example: Using Vector

Let's see an example that uses both the old and the new methods.

VectorDemo.java▶ Try it
import java.util.Enumeration;
import java.util.Vector;
 
public class VectorDemo {
    public static void main(String[] args) {
        Vector<String> v = new Vector<>();
        System.out.println("Capacity: " + v.capacity());
 
        v.addElement("Navika");
        v.add("Srihitha");
        v.add("Advitha");
        System.out.println(v);
        System.out.println("First: " + v.firstElement());
        System.out.println("At 1: " + v.elementAt(1));
 
        Enumeration<String> e = v.elements();
        StringBuilder sb = new StringBuilder();
        while (e.hasMoreElements()) {
            sb.append(e.nextElement()).append(' ');
        }
        System.out.println(sb.toString().strip());
 
        for (int i = 0; i < 8; i++) {
            v.add("x" + i);
        }
        System.out.println("Size " + v.size() + ", capacity " + v.capacity());
    }
}
Output
Capacity: 10
[Navika, Srihitha, Advitha]
First: Navika
At 1: Srihitha
Navika Srihitha Advitha
Size 11, capacity 20

Explanation: A new Vector has capacity 10 straight away. addElement and add do the same job. Enumeration is the old way to loop, from before Iterator existed. When the 11th element arrives, the capacity doubles from 10 to 20.

Why Vector Is Not Used Today

Synchronizing every method sounds safe, but it has two problems:

  1. It is slower in single-threaded code, which is most code, because every call takes a lock.
  2. It is not enough for real thread safety. A check followed by an action, such as "if it's empty, add an element", is two calls, and another thread can run between them.

Modern code uses ArrayList for one thread, and CopyOnWriteArrayList or Collections.synchronizedList when threads share a list.

Common Mistakes

Choosing Vector "to be thread-safe". Compound actions still need extra locking; use a concurrent collection.

Using Enumeration in new code. Use Iterator or a for-each loop.

Assuming Vector and ArrayList grow the same way. Vector doubles; ArrayList grows by half.

Practice

  1. Easy: Create a Vector with new Vector<>(4, 3) (capacity 4, increment 3), add 10 elements, and print the capacity after each add.
  2. Harder: Rewrite the example with ArrayList and an Iterator, removing every legacy method.

Quick check: Vector

4 questions
1What makes Vector different from ArrayList?
2How does a Vector grow by default when full?
3What is Enumeration?
4What should new single-threaded code use instead of Vector?

Key Takeaways

Was this tutorial helpful?

Lesson 264 of 482