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 enumuration.
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 program to demonstrate // Enumeration.nextElement() method import java.util.*; public class GFG { @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { // create a Enumeration and vector object Enumeration<Integer> company; Vector<Integer> employees = new Vector<Integer>(); // add values to employees employees.add(1001); employees.add(2001); employees.add(3001); employees.add(4001); company = employees.elements(); while (company.hasMoreElements()) { // get elements using nextElement() System.out.println("Emp ID = " + company.nextElement()); } } } |
Emp ID = 1001 Emp ID = 2001 Emp ID = 3001 Emp ID = 4001
Program 2:
// Java program to demonstrate // Enumeration.nextElement() method import java.util.*; public class GFG { @SuppressWarnings({ "unchecked", "rawtypes" }) public static void main(String[] args) { // create a Enumeration and vector object Enumeration<String> Users; Vector<String> user = new Vector<String>(); // add Users user.add("Aman Singh"); user.add("Raunak Singh"); user.add("Siddhant Gupta"); Users = user.elements(); while (Users.hasMoreElements()) { // get elements using nextElement() System.out.println(Users.nextElement()); } } } |
Aman Singh Raunak Singh Siddhant Gupta
References: https://docs.oracle.com/javase/10/docs/api/java/util/Enumeration.html#nextElement()
Recommended Posts:
- StringTokenizer nextElement() Method in Java with Examples
- Collections enumeration() method in Java with Examples
- Enumeration asIterator() Method in Java with Examples
- Enumeration hasMoreElements() Method in Java with Examples
- Difference between Iterator and Enumeration in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



