Java Tutorials and Java Concept

Khurshid Md Anwar
3 min readAug 13, 2020

--

Vector in Java

Introduction

Vectors in Java uses data in a linear fashion like Array. But it implements a dynamic array that can grow or shrink as and when required to accommodate adding and removing data item. In the Java 2 SDK, the Vector class has been included in the Collection framework to implement the List interface and it comes under the java.util package. The Vector class is favored over the ArrayList class when we are creating a multi-threaded application.

  • The Vector class is similar to the ArrayList class but Vector is synchronized(the main facet of Vector class is that it is thread-safety. ).
  • The Vector class is created dynamic Array which can grow and shrink.
  • Vector class is a child class of AbstractList class and implements on List interface
  • Duplicate element/objects are allowed in Vector
  • Insertion order is maintained
  • It allows NULL insertion
  • Vectors don’t support primitives data types like short, int, double, char, etc.

Advantages of Vector in Java

  1. Due to its Dynamic size, memory wastage is not happening.
  2. Vector class is synchronized and it helps in the multithreaded program and helps to check data corruption.
  3. Vectors have some legacy methods which can be implemented only on vector class and not on the ArrayLists class.

Note: The Vector class may reduce the performance of the developed application as it is thread safety and it only allowed one thread as it locked the thread and the remaining thread have to wait until a thread released the object lock. So it is recommended that if we do not need thread safety, then we should use the ArrayList class instead of Vector class.

The Vector class defines the following constructors

public Vector() This a constructor creates an empty vector and its initial data array size is 10

public Vector(int n) This A constructor creates an empty vector with specified size n.

Vector (int initSize, int incrSize) This form of constructor creates an empty vector with its initial and increment capacity.

Vector (Collection c) This a constructor creates a vector containing the elements of the specified collection c.

Apart from the above constructor’s Vector define three protected data member which are as follows

protected int capacity increment

This data member of the vector is automatically incremented when its size becomes greater than its capacity.

protected intelementCount

The number of valid elements in the vector.

protected int elementData[]

The array holds the elements of the vector.

public class TheVectorClassExample

public static void main(String args[]) {

Vector<String> student = new Vector<String>(4);

//Adding elements to a student Vector

//Checking the size and capacity of Vector

System.out.println(“The Size of Vector is: “+student.size());

System.out.println(“Default capacity is: “+student.capacity());

//Display Vector elements

System.out.println(“Vector element is: “+student);

student.addElement(“Rat”);

student.addElement(“Cat”);

student.addElement(“Deer”);

//Again check size and capacity after two insertions

System.out.println(“Size after addition: “+student.size());

System.out.println(“Capacity after addition is: “+student.capacity());

//Display Vector elements again

System.out.println(“Elements are: “+student);

//Checking if Tiger is present or not in this student or

if(student.contains(“Tiger”))

System.out.println(“Tiger is present at the index “ +student.indexOf(“Tiger”));

System.out.println(“Tiger is not present in the list.”);

System.out.println(“The first animal of the studenttor is = “+student.firstElement());

System.out.println(“The last animal of the studenttor is = “+student.lastElement());

Enumeration en = student.elements();

System.out.println(“\nStudent are:”);

while(en.hasMoreElements())

System.out.print(en.nextElement() + “ “);

Originally published at https://javaknowhow.blogspot.com.

--

--

Khurshid Md Anwar
Khurshid Md Anwar

Written by Khurshid Md Anwar

Computer Science Trainer and blogger

No responses yet