Java Program to Iterate Vector using Enumeration
The Vector class implements a growable array of objects. It is available in java.util package. It implements the List interface. The Enumeration interface defines the methods by which you can traverse the elements in a collection of objects. Now in order to add elements
Vector Syntax:
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
java.util.Enumeration interface is one of the predefined interfaces, whose object is used for retrieving the data from collections framework variable( like Stack, Vector, HashTable, etc.) in a forward direction only and not in the backward direction.
You can use the Java.util.Vector.addElement() method to append a specified element to the end of this vector by increasing the size of the vector by 1. The functionality of this method is similar to that of the add() method of the Vector class.
Syntax:
boolean addElement(Object element)
Parameters: This function accepts a single parameter element of object type and refers to the element specified by this parameter is appended to the end of the vector.
Return Value: This is a void type method and does not return any value.
Example 1:
Java
import java.util.Enumeration;
import java.util.Vector;
public class GFG {
public static void main(String a[])
{
Vector<String> v = new Vector<String>();
v.add( "Welcome" );
v.add( "To" );
v.add( "Geeks for" );
v.add( "Geeks" );
Enumeration<String> en = v.elements();
while (en.hasMoreElements()) {
System.out.println(en.nextElement());
}
}
}
|
Output
Welcome
To
Geeks for
Geeks
Example 2:
Java
import java.util.Enumeration;
import java.util.Vector;
public class GFG {
public static void main(String a[])
{
Vector<Integer> v = new Vector<Integer>();
v.add( 1 );
v.add( 2 );
v.add( 3 );
v.add( 4 );
Enumeration<Integer> en = v.elements();
while (en.hasMoreElements()) {
System.out.println(en.nextElement());
}
}
}
|
Last Updated :
10 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...