English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java arrays

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input/output (I/O)

Java Reader/Writer

Other Java topics

Java Vector (Vector)

In this tutorial, we will learn about the Vector class and how to use it. We will also learn about the differences between it and the ArrayList class, and why we should switch to array lists.

The Vector class is an implementation of the List interface that allows us to create something similar to}}ArrayListClass adjustable size array.

Java vector (vector) and ArrayList

In Java, ArrayList and Vector both implement the List interface and provide the same functionality. However, there are some differences between them.

The Vector class synchronizes each individual operation. This means that every time we want to perform some operation on the vector, the Vector class will automatically apply a lock to the operation.

This is because when one thread accesses a vector while another thread tries to access it, a ConcurrentModificationException exception is generated. Therefore, using a lock for each operation continuously will reduce the efficiency of the vector.

However, in ArrayList, the methods are not synchronized. Instead, it uses the Collections.synchronizedList() method to synchronize the entire list.

Note:Since vector is not thread-safe and has lower efficiency, it is recommended to use ArrayList instead of Vector.

Create vector (vector)

This is how we create a vector (vector) in Java.

Vector<Type> vector = new Vector<>();

Here, Type represents the data type stored in the vector. For example,

//Create a vector of integer type
Vector<Integer> vector = new Vector<>();
//Create a vector of string type
Vector<String> vector = new Vector<>();

Vector (vector) methods

The Vector class also provides an adjustable size array implementation of the List interface (similar to the ArrayList class). Some Vector methods are:

Add element to vector

  • add(element) - Add an element to the vector (vector)

  • add(index, element) - Add an element at a specified position

  • addAll(vector) - Add all elements of one vector (vector) to another vector (vector)

For example,

import java.util.Vector;
class Main {
    public static void main(String[] args) {
        Vector<String> mammals = new Vector<>();
        //Use the add() method
        mammals.add("Dog");
        mammals.add("Horse");
        //Use index number
        mammals.add(2, "Cat");
        System.out.println("Vector: " + mammals);
        // Use addAll() method
        Vector<String> animals = new Vector<>();
        animals.add("Crocodile");
        animals.addAll(mammals);
        System.out.println("New Vector: " + animals);
    }
}

Output Result

Vector: [Dog, Horse, Cat]
New Vector: [Crocodile, Dog, Horse, Cat]

Access vector element

  • get(index) - Returns the element specified by the index

  • iterator() - Returns an iterator object to sequentially access vector elements

For example,

import java.util.Iterator;
import java.util.Vector;
class Main {
    public static void main(String[] args) {
        Vector<String> animals = new Vector<>();
        animals.add("Dog");
        animals.add("Horse");
        animals.add("Cat");
        // Use the get() method
        2);}}
        System.out.println("Element at index", 2: " + element);
        // Use iterator()
        Iterator<String> iterate = animals.iterator();
        System.out.print("Vector: ");
        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
}

Output Result

Element at index 2: Cat
Vector: Dog, Horse, Cat,

Delete vector element

  • remove(index) - Delete element from specified position

  • removeAll() - Delete all elements

  • clear() - Delete all elements. It is more efficient than removeAll()

For example,

import java.util.Vector;
class Main {
    public static void main(String[] args) {
        Vector<String> animals = new Vector<>();
        animals.add("Dog");
        animals.add("Horse");
        animals.add("Cat");
        System.out.println("Initial Vector: " + animals);
        //Use the remove() method
        String element = animals.remove(1);}}
        System.out.println("Removed Element: " + element);
        System.out.println("New Vector: " + animals);
        // Use the clear() method
        animals.clear();
        System.out.println("Vector after clear(): " + animals);
    }
}

Output Result

Initial Vector: [Dog, Horse, Cat]
Removed Element: Horse
New Vector: [Dog, Cat]
Vector after clear(): []

Other Methods of Vector (vector)

MethodContent Description
set()Change the element of the vector
size()Return the size of the vector
toArray()Convert the vector to an array
toString()Convert the vector to a string
contains()

Search for the specified element in the vector and return a boolean value