Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Vector Class
3.
Constructors
4.
Methods
5.
Basic operations
6.
FAQs
7.
Conclusion
Last Updated: Mar 27, 2024

Vector Class

Author Yashesvinee V
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

The Vector class in Java helps create a dynamic array of objects and contains components that can be referenced using an index. They are very much similar to ArrayLists. The size of a Vector can change depending on whether elements are added or removed from it. The Vector class implements the List interface, which is used to define an ordered collection of elements. Now, let us learn about the functions/methods used to manipulate the size and contents of a vector.

Also see, Duck Number in Java and Hashcode Method in Java

Vector Class

  • The Vector class is synchronized and has some legacy methods that are absent in the collection framework.
     
  • A vector tries to optimize storage management by maintaining a capacity and a capacityIncrement variable. The capacity is always at least as large as the vector size, and the vector's storage increases in the size of capacityIncrement.
     

Declaration:

public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable 
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Constructors

Methods

Also see,  Swap Function in Java

Basic operations

  • Add/Update elements
     
import java.util.Vector;

public class Main {
    public static void main(String[] args) {
        Vector<String> alpha= new Vector<>();

        // Using the add() method
        alpha.add("A");
        alpha.add("B");

        System.out.println("Vector: " + alpha);

        Vector<String> alphanum = new Vector<>();
        alpha.add("1");
        
        alpha.addElement("2");

        alphanum.addAll(alpha);
        System.out.println("New Vector: " + alphanum);
    }
}

 

Output:

Vector: [A, B]
New Vector: [A, B, 1, 2]

 

  • Access elements
     
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Vector<String> alpha= new Vector<>();

        // Using the add() method
        alpha.add("A");
        alpha.add("B");
        alpha.add("C");
        alpha.add("D");
        alpha.add("E");
        System.out.println("First Element: " + alpha.firstElement());
        System.out.println("Element at index 2: " + alpha.elementAt(2));
        
        // Using Iterator
        Iterator<String> iterate = alpha.iterator();
        System.out.print("Vector: ");
        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
      }
    }
}

 

Output:

First Element: A
Element at index 2: C
Vector: A, B, C, D, E, 

 

  • Remove elements
     
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Vector<String> alpha= new Vector<>();

        // Using the add() method
        alpha.add("A");
        alpha.add("B");
        alpha.add("C");
        alpha.add("AD");
        alpha.add("E");
        System.out.println("Vector: " + alpha);
        
        System.out.println("Removing element at index 2: " + alpha.remove(2));
        
        System.out.println("Removing elements starting with A .. ");
        alpha.removeIf(e -> e.startsWith("A"));
        System.out.println("Vector: " + alpha);
    
    }
}

 

Output:

Vector: [A, B, C, AD, E]
Removing element at index 2: C
Removing elements starting with A ..
Vector: [B, E]


Try this code by yourself on Online Java Compiler.

Must Read Array of Objects in Java

FAQs

  1. What are common errors one can encounter during the declaration of a vector?

    If the size parameter of the vector constructor is passed a negative value, it throws an IllegalArgumentException. A NullPointerException is thrown when a null value is passed instead of a collection in the constructor.
     
  2. What is the difference between iterator and listIterator?

    Iterator can traverse the vector elements in the forward direction and can perform only remove operations. ListIterator can traverse the vector elements in backward as well as forward directions. It can perform add, remove, and set operations while traversing the collection.

Conclusion

This article extensively discusses Vectors in Java. We hope that this blog has helped you enhance your knowledge about the different methods and ways of implementing the Vector class in Java. If you would like to learn more, check out our articles on Data Structures in Java and Vectors vs Arrays.

Recommended Readings:

Explore our Coding Ninjas Library and upvote our blog to help other ninjas grow. Happy Coding!

Previous article
How to Convert list to array in Java
Next article
How to Add Date in Arraylist Java
Live masterclass