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
// Java Program to Iterate Vector using Enumeration // Importing Enumeration class import java.util.Enumeration; // Importing vector class import java.util.Vector; public class GFG { // Main driver method public static void main(String a[]) { // Creating a new vector Vector<String> v = new Vector<String>(); // Adding elements to the end v.add( "Welcome" ); v.add( "To" ); v.add( "Geeks for" ); v.add( "Geeks" ); // Creating an object of enum Enumeration<String> en = v.elements(); while (en.hasMoreElements()) { // Print the elements using enum object // of the elements added in the vector System.out.println(en.nextElement()); } } } |
Welcome To Geeks for Geeks
Example 2:
Java
// Java Program to Iterate Vector using Enumeration // Importing Enumeration class import java.util.Enumeration; // Importing Vector class import java.util.Vector; public class GFG { // Main driver method public static void main(String a[]) { // Creating a vector object Vector<Integer> v = new Vector<Integer>(); // Adding elements to the end v.add( 1 ); v.add( 2 ); v.add( 3 ); v.add( 4 ); // Creating an enum object Enumeration<Integer> en = v.elements(); while (en.hasMoreElements()) { // Displaying elements of vector class // calling enum object System.out.println(en.nextElement()); } } } |
1 2 3 4