Vector Class in Java

 

Let us learn what is Vector class in Java programming with its explanation, constructors, methods, implementation and output.

What is Vector Class?

The Vector class is a predefined class available in java.util package. This class implements a dynamic array and it is quite similar to an ArrayList in some aspects.

However, unlike ArrayList, Vector class is synchronized by default. If multiple threads are considered, then Vector class performs better than an ArrayList.

Another difference is that the Vector class contains numerous legacy methods such as Iterator and Enumeration that are not a part of collection framework.

If several threads act on a Vector object simultaneously, the results will be reliable. If the size of the array is not known in advance, the Vector proves to be very useful.

A Vector can hold objects of any data type and number. The default size of the Vector class is 10. However, a Vector requires an Iterator to display its contents.

Every Vector object maintains a capacity and capacity increment. The capacity of a Vector is always greater than or equal to the size of the Vector.

Declaration of Vector Class

Constructors of Vector Class

The constructor of the Vector class is overloaded. Let us now see all the constructors available in the Vector class.

   

Sr. No.ConstructorDescription
1.Vector()This method constructs an empty vector. It has a size of 10 and capacity increment is 0.
2.Vector(int capacity)This method constructs an empty vector with a capacity specified by the parameter.
3.Vector(int capacity, int capacity_increment)This method builds an empty vector object with the capacity and capacity increment specified in the method parameters.
4.Vector(Collection a)This method constructs a vector containing the elements of the Collection a. The elements are accessed in the order returned by the Collection’s iterator.

Methods of Vector Class

Sr. No.MethodDescription
1.boolean add(Object element)This method appends the specified element to the end of the Vector and returns true if the element is added successfully.
2.void add(int position, Object element)This method adds the element at a specific index position as mentioned in the method parameter.
3.Object remove(int position)This method removes the element at the index specified in the method parameter and returns that element.
4.boolean remove(Object element)This method deletes the first occurrence of the specified element from the Vector if it is available.
5. void clear()This method deletes all the elements from the Vector.
6.int indexOf(Object element)This method returns the index of the first occurrence of the specified element in the Vector and returns -1 if the element is absent.
7.Object set(int position, Object element)This method inserts an element at the position specified in the parameter.
8.Object get(int position)This method returns the element located at the position specified by the parameter.
9.int size()This method returns the size of the Vector.

Implementation of Vector Class in Java

Output

   

If you have any doubts about the implementation of Vector class in Java programming, let us know about it in the comment section.

Collection Frameworks
Random Class
Stack Class
StringTokenizer Class

One thought on “Vector Class in Java

  1. Isabella Hayek

    HOLA. Yo soy de España. Tus códigos Java y C especialmente me han ayudado mucho a ser un mejor programador. Finalmente conseguí un trabajo de programador de computadoras en París, todo gracias a ti. Por favor, mantenga la nueva generación educando la ciencia de la computación.

    Reply

Let's Discuss