In programming, one of the most commonly used data structures is Vector in Java. Arrays are static data structures that can linearly store data. Similarly, vector in java also store the data linearly, but they are not restricted to a fixed size. Instead, its size can grow or shrink as per requirement. The parent class is AbstractList class and is implemented on List Interface.

Before you start to use vectors, import it from the java.util.package as follow:

import java.util.Vector

Declaration and Assessing Elements of a Vector

Here is how a vector in java is declared:

public class Vector extends AbstractList

implements List, RandomAccess, Cloneable, Serializable

Here, V is the type of element which can be int, string, char, etc.

Like we access data members in arrays, we can do that in vectors, too, by using the element’s index. For example, the second element of Vector E can be accessed as E[2].

Some common errors made while declaring a vector in java:

  • An IllegalArgumentException is thrown if the initial size of the vector is a negative value
  • A NullPointerException is thrown if the specified collection is null
  • The size of the vector is less than or equal to the capacity of the vector
  • Capacity is doubled in every increment cycle if vector increment is not specified

#full stack development #java #vector #vector in java

Vector in Java | Java Vector Class with Examples
1.35 GEEK