Vector/Enumeration Example
[ Home ] [ Up ] [ Vector/Enumeration Example ] [ ArrayList/Iterator Example ] [ LinkedList Example ] [ Stack Example ] [ Comparable Example ] [ Comparator Example ] [ HashSet Example ] [ TreeSet Example ] [ TreeSet with Comparator ] [ HashMap Example ] [ TreeMap Example ] [ TreeMap with Comparator ]

 

 

Here's the Vector/Enumeration example from before, translated to using generics:

package examples;

import java.util.Enumeration;
import java.util.Vector;

public class VectorExample
{
  public static void main(String[] args)
  {
    Vector<String> vector = new Vector<String>();
    vector.add("Mary");
    vector.add("Frank");
    vector.add("Joe");
    vector.add("Sylvia");
    vector.add("Vanessa");
    
    for (int i = 0; i < vector.size(); i++)
    {
      String name = vector.elementAt(i); // Note NO required cast
      System.out.println(i + ": " + name);
    }
    
    for (Enumeration<String> en = vector.elements(); en.hasMoreElements(); )
    {
      String name = en.nextElement(); // Note NO required cast
      System.out.println(name);
    }
  }
}

which outputs the following (the same as the original):

0: Mary
1: Frank
2: Joe
3: Sylvia
4: Vanessa
Mary
Frank
Joe
Sylvia
Vanessa

 

This page was last modified on 27 November, 2007