Trusted answers to developer questions

What is a vector in Java?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Vector

The Vector class implements the List interface – with the Vector class, we can create a resizable list of objects. These objects are accessed using the index.

Syntax for creating vector

We can create a vector in three ways:

  • Creating a vector with default capacity10:
Vector<Type> vector = new Vector<>();
  • Creating a vector with a specific capacity:
Vector<Type> vector = new Vector<>(int capacity);
  • Creating a vector with initial size and capacityIncrement:

Whenever the vector reaches full capacity the capacity grows as currentCapacity + capacityIncrement.

Vector<Type> vector = new Vector<>(int capacity, int capacityIncrement);

Important methods in Vector

Methods

Explanation

add(element)

Appends an element

add(int index, element)

Add an element at an index

addAll(Collection elements)

Appends all the elements to the vector

get(int position)

Get element at the position (to get first element pass 1)

remove( int index)

Removes an element at the index

clear()

Removes all elements

size()

Returns number of elements in the vector

firstElement()

Returns the first element

lastElement()

Returns the last element

contains(Object t)

Returns true if the element is present in vector

setElementAt(Object t, int index)

Set the passed element as value at the index

import java.util.Vector;
public class VectorDemo {
public static void main(String args[]) {
Vector<Integer> vector = new Vector<>(4, 2); // initial capacity is 4, once 4 elements filled the vector size increments by 2
System.out.println("Initial size: " + vector.size());
System.out.println("Initial capacity: " + vector.capacity());
vector.add(1);
vector.add(2);
vector.add(3);
vector.add(4);
System.out.println("Vector value is: " + vector);
System.out.println("Capacity after four elements: " + vector.capacity());
vector.add(5);
System.out.println("Capacity after one more element : " + vector.capacity());
vector.add(0, 10);
System.out.println("After adding 10 as first value" + vector);
System.out.println("Vector.firstElement: " +vector.firstElement());
System.out.println("Vector.lastElement: " +vector.lastElement());
System.out.println("Getting element at index 3: " +vector.get(2));
vector.remove(3);
System.out.println(" After Removing 3 : " +vector);
System.out.println("Check if 3 present in vector: " + vector.contains(3));
System.out.println("Check if 10 present in vector: " + vector.contains(10));
vector.setElementAt(99, 0);
System.out.println("Vector After changing first value as 99 : " + vector);
vector.clear();
System.out.println("Vector After clearing : " + vector);
}
}

In the above program, we have created a vector with initial capacity as 4, and the capacity increment value as 2when the vector reaches its capacity, the capacity of the vector increases by 2. We have also used functions to add, remove, set, and get elements of the vector.

How is ArrayList different from Vector?

  • Vector synchronizes each operation, which means that when a thread is performing an operation in a vector, the same vector cannot be accessed by another thread. If we try to access it in another thread, then it will throw ConcurrentModificationException as ArrayList doesn’t synchronize for each operation (as the vectors lock for each operation). Therefore, it is recommended that you use ArrayList instead.

For more methods in Vector, refer here.

RELATED TAGS

vector
java
Did you find this helpful?