The elements() method of the Vector class is used to retrieve an enumeration of the elements that were added to the vector. A while loop is then used to cycle through and print the elements contained in the enumeration. The hasMoreElements() method is used to determine whether the enumeration contains more elements. If it does, the nextElement() method is used to retrieve the object for printing.
The removeElement() of the Vector class is used to remove the vector element The for loop indexes each element in the vector using the elementAt() method.
import java.util.Vector;
import java.util.Enumeration;
class elementAtJavaExample
{
public static void main(String args[])
{
Vector vector = new Vector(4);
int n,i;
vector.addElement("Blue Demon");
vector.addElement("Doble Cola");
vector.addElement("Ganga");
vector.addElement("Orchids");
vector.addElement("Duke's Mangola");
System.out.println("Capacity of vector is :" + vector.capacity());
System.out.println("First element is :" + vector.firstElement());
System.out.println("Last element is :" + vector.lastElement());
System.out.println("Third element is :" + vector.elementAt(3));
System.out.println("Orchids is at position: " + vector.indexOf("Orchids",0));
vector.insertElementAt("Vanilla",2);
vector.removeElementAt(0);
n=vector.size();
String arr[]=new String[n];
vector.copyInto(arr);
for(i=0;i<=n-1;i++)
{
System.out.println(arr[i]);
}
}
}