Enumeration nextElement() Method in Java with Examples
An object that implements the Enumeration interface generates a series of elements, one at a time. The nextElement() method of Enumeration used to return the next element of this enumeration if this enumeration object has at least one more element to provide.This method is used to get elements from the enumeration. Syntax:
E nextElement()
Parameters: This method accepts nothing. Return value: This method returns true the next element of this enumeration. Exception: This method throws NoSuchElementException if no more elements exist. Below programs illustrate nextElement() method: Program 1:
Java
import java.util.*;
public class GFG {
@SuppressWarnings ({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
Enumeration<Integer> company;
Vector<Integer> employees = new Vector<Integer>();
employees.add( 1001 );
employees.add( 2001 );
employees.add( 3001 );
employees.add( 4001 );
company = employees.elements();
while (company.hasMoreElements()) {
System.out.println("Emp ID = "
+ company.nextElement());
}
}
}
|
Output:
Emp ID = 1001
Emp ID = 2001
Emp ID = 3001
Emp ID = 4001
Program 2:
Java
import java.util.*;
public class GFG {
@SuppressWarnings ({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
Enumeration<String> Users;
Vector<String> user = new Vector<String>();
user.add("Aman Singh");
user.add("Raunak Singh");
user.add("Siddhant Gupta");
Users = user.elements();
while (Users.hasMoreElements()) {
System.out.println(Users.nextElement());
}
}
}
|
Output:
Aman Singh
Raunak Singh
Siddhant Gupta
References: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#nextElement()
Last Updated :
27 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...