Java Vector Methods


Four programs are given on Vector 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, 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.

In the following program many of the above methods of Vector are used.

import java.util.*;
public class VectorMethods
{
  public static void main(String args[])
  {
    Vector vect1 = new Vector();
    System.out.println("Before adding elements isEmpty(): " + vect1.isEmpty());
			// adding elements
    vect1.addElement("apple");
    vect1.add(10);
    vect1.addElement(10.5);
    Date d = new Date();
    vect1.add(d);
    vect1.add(10);
    StringBuffer sb1 = new StringBuffer("citrus");
    vect1.addElement(sb1);
                                             // inserting element
    vect1.insertElementAt("banana", 2);
                                            // miscellaneous methods
    System.out.println("After adding elements isEmpty(): " + vect1.isEmpty()); 
    System.out.println("Storing capacity of Vector: " + vect1.capacity());
    System.out.println("No. of elements in Vector: " + vect1.size());
    System.out.println("Vector contains 10.5: " +       vect1.contains(10.5));
    System.out.println("Index of 10: " + vect1.indexOf(10));
    System.out.println("Last index of 10: " + vect1.lastIndexOf(10));
                                              // retrieval of elements
    System.out.println("First element: " + vect1.firstElement()); 
    System.out.println("Last element: " + vect1.lastElement());
    System.out.println("Element at 3rd position: " + vect1.elementAt(3));

    System.out.println();
 }
}


Java Vector Methods
Output screen of VectorMethods.java of Java Vector Methods

All the methods used in the code are explained earlier under "Vector Methods".

More methods like get, remove(), removeAll() and setElementAt() are discussed in the second program of vector – Vector Retrieval.

4 thoughts on “Java Vector Methods”

  1. Dear sir,
    A vector capacity cannot be below the size; it may equal.
    What does it mean?
    Capacity increases automatically as explained above –> there is no question of size

  2. Hello sir, I think there is a mistake: the second vector object vect2 is created with a default capacity of 50. When 50 used out, it adds 50 more not 10!!!
    and when 100 used out, it adds 100 more and then capacity becomes 200. i tried this.

Leave a Comment

Your email address will not be published.