Vectors

Vectors

In Java, the Vector class is part of the java.util package and is a legacy class that was introduced in the earlier versions of Java. It is similar to an ArrayList but with two significant differences. First, the Vector class is thread-safe, meaning that it can be safely used in a multithreaded environment. Second, the Vector class is synchronized, which means that all the methods of the Vector class are synchronized, so only one thread can access the Vector object at any given time.

The Vector class can be used to create resizable arrays, similar to an ArrayList. However, unlike ArrayList, which can only grow in size, Vector can grow or shrink dynamically by using the add() and remove() methods. The Vector class also provides several other methods to manipulate its elements, such as get(), set(), size(), and clear().

Here’s an example of how to create a Vector and add elements to it:

import java.util.Vector;

public class Example {

  public static void main(String[] args) {

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

    vector.add(“Hello”);

    vector.add(“World”);

    vector.add(“!”);

    System.out.println(vector);

  }

}

This would output: [Hello, World, !]

Apply for Core Java Developer Certification Now!!

https://www.vskills.in/certification/certified-core-java-developer

Back to Tutorial

Share this post
[social_warfare]
The Map Interface
ArrayList

Get industry recognized certification – Contact us

keyboard_arrow_up