Java Vector Example


Java Vector Example: About class Vector, its constructors and methods are explained very elaborately in the first program (and is advised to read before this program) Vector Methods.

Four Java Vector Examples given to know thoroughly about Vector and above methods. Vector is a general purpose data structure user very often by the programmer even though the collections come with many classes.

  1. Vector – Methods: Uses all the methods of Vector class like capacity, size, firstElement etc.
  2. Vector – Generics: Create generics vector storing only integers and uses all Collections static methods like swap, shuffle, fill, rotate etc.
  3. Vector – Retrieval: Illustrates some more methods like get, remove, removeAll and also how to retrieve the elements – 6 styles
  4. Vector – Play With: Give manipulations on vector elements like removing duplicate values, converting vector elements into an array etc.
This is the second Java Vector Example

.

import java.util.*;
public class VectorRetrieval
{
  public static void main(String args[])
  {
    Vector vect1 = new Vector();
    vect1.add(10);
    vect1.add(20);
    vect1.add(10.5);
    vect1.add("hello");
    vect1.add('A');
    vect1.setElementAt("world", 3);

    System.out.print("Get 2nd element: " + vect1.get(2));  
   
    System.out.print("\nElements: " + vect1);  // 1st style

    System.out.print("\nElements with for loop: ");  // 2nd style
    for(int i = 0; i < vect1.size(); i++)
    {
      System.out.print(vect1.elementAt(i) + " ");
    }
      
    System.out.print("\nElements with Enumeration: ");  // 3rd style
    Enumeration e = vect1.elements();
    while(e.hasMoreElements())
    {
      System.out.print(e.nextElement() + " ");
    }
   
    vect1.remove(10.5);
    System.out.print("\nElements with Iterator: ");// 4th style
    Iterator it1 = vect1.iterator();
    while(it1.hasNext())
    {
      System.out.print(it1.next() + " ");
    }

    System.out.print("\nElements with ListIterator: ");  // 5th style
    ListIterator it2 = vect1.listIterator(2);
    while(it2.hasNext())
    {
      System.out.print(it2.next() + " ");
    }

    System.out.print("\nElements with foreach: ");   // 6th style
    for(Object obj : vect1)
    {
      System.out.print(obj + " ");
    }
  
    vect1.removeAllElements();
    System.out.println("\nSize after removing all elements: " + vect1.size());
 }
}


Java Vector Examplen
Output screen of VectorRetrieval.java of Java Vector Retrieval

Six styles of printing elements of a vector are given.

1. Printing direct vector object.
2. Using general for loop with size() method.
3. Using Enumeration.
4. Using Iterator
5. Using ListIterator
6. Using foreach

Vector is mostly used in a multithreaded environment to avoid data inconsistency. All the methods of Vector are implicitly synchronized. So, only one object can do the operation at a time. Vector methods are safe with threads; known as thread safe.

2 thoughts on “Java Vector Example”

Leave a Comment

Your email address will not be published.