Java Vector Generics Example

Java Vector Generics 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 Generics Examples are given on Vector to know thoroughly about Vector, methods and usage. Vector is a general purpose data structure used 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, shuttle, 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.
Java Vector Generics Example: Two Vector objects are created storing only int values and string values.
import java.util.*;
public class VectorGenerics
  public static void main(String args[])
  {                       // vector storing only ints
    Vector vect1 = new Vector();
                       // vector storing only strings
    Vector vect2 = new Vector();

    List numbersList = vect2.subList(1, 9);
    System.out.println("Elements of subList: " + numbersList);

    System.out.println("Original values vect2: " + vect2);
    Collections.swap(vect2, 2, 4);
    System.out.println("After swapping 2 and 4: " + vect2);
    System.out.println("Maximum value of vect1: " + Collections.max(vect1));
    System.out.println("Maximum value of vect2: " + Collections.max(vect2));

    System.out.println("Minimum value of vect1: " + Collections.min(vect1));
    System.out.println("Minimum value of vect2: " + Collections.min(vect2));

    Vector vect3 = new Vector();
    System.out.println("Original values vect3: " + vect3);
    System.out.println("After adding vect3 to vect1, vect1 values: " + vect1);    

    System.out.println("Reversed values vect3: " + vect3);

    System.out.println("Original values vect1: " + vect1);
    System.out.println("1st Shuffling vect1: " + vect1);
    System.out.println("2nd Shuffling vect1: " + vect1);

    Collections.rotate(vect1, 3);
    System.out.println("vect1 values after rotating: " + vect1);

    Collections.fill(vect1, 100);    
    System.out.println("vect1 after filling with 100: " + vect1);
    System.out.println("vect1 size after calling clear(): " + vect1.size());

   String str = vect3.toString();
   System.out.println("vect3 as string: " + str);

Java Vector Generics Example
Output screen of of Java Vector Generics

List numbersList = vect2.subList(1, 9);

Three Java Vector Generics objects storing only integers (vect1, vect3) and strings(vect2) are created. subList(1, 9) returns a List object containing vector vect2 elements from 1 to 8 (9-1).

Collections.swap(vect2, 2, 4);

The elements 2 and 4 of vect2 are swapped. That is, in the place of 2 four comes and in 4 place 2 comes. See the screenshot.

System.out.println(“Maximum value of vect1: ” + Collections.max(vect1));
System.out.println(“Minimum value of vect1: ” + Collections.min(vect1));

max(vect1) returns the maximum value element in vect1 and min(vect1) returns the minimum value element in vect1 vector.


All the elements of vector vect3 are added to vect1. vect3 elements are added at the end (appended) of vect1 elements.


All the elements of vect3 are reversed. It affects the original vector itself. Now vect3 contains the elements in reverse order.


The static method shuffle(vect1) of Collections class shuffles the elements in the vector vect1. Calling the same method a number of times, prints the elements in different orders. After this method call, vect1 contains elements in a different order.

Collections.rotate(vect1, 3);

The static method rotate(vect1, 3) of Collections class rotates the elements. First it prints the last 3 and afterwards 0 to size()-3.

Collections.fill(vect1, 100);

The fill() fills all the elements of vect1 with 100. The earlier values are lost. It helps to have common values in all the elements.


The clear() method removes all the elements. Now the size becomes zero.

String str = vect3.toString();

The vector object vect3 is converted to string str. The str contains all the elements of vect3. The string format is helpful when required to display in text field. Learn more methods of Collections class.

Leave a Comment

Your email address will not be published.