Vector class Java


Vector class Java is the general purpose Data Structure used very often by the Programmers. Vector, though belongs to legacy classes (DS of JDK 1.0), it can use the methods of List interface as Vector implements List from JDK 1.2. In this way, Vector became part of collections framework.

All Java Vector class methods are synchronized and thus best suitable to use in multithreaded environment.

Following is a simple example on Vector class Java and for indepth knowledge 5 links are also given.
import java.util.*;
public class PrintElements
{
 public static void main(String args[])
 {
   Vector vect1 = new Vector();

   vect1.add("Jyostna");
   vect1.add(100);
   vect1.add(10.5);
   vect1.add(new Date());

   Enumeration e = vect1.elements();

   System.out.println("Vector Elements:");
   
   while(e.hasMoreElements())
   {
     System.out.println(e.nextElement());
   }
 }
}


Vector class JavaOutput Screenshot of Example on Vector class Java

Vector object vect1 is created and added elements with add() method. The elements() method of Vector returns an object of Enumeration interface with all the elements of vect1. The method hasMoreElements() of Vector iterates all the elements of the Vector. The nextElement() method returns each element.

This is simple example and explanation and for more indepth on Vector class with all operations on elements read these 5 examples:
1. Vector Methods
2. Vector Generics
3. Vector Retrieval
4. Vector Play With
5. Vector vs ArrayList

Leave a Comment

Your email address will not be published.