Get Enumeration Over Java ArrayList
ArrayList class is a re-sizeable array that is present in java.util package. Unlike the built-in arrays, ArrayList can change their size dynamically where elements can be added and removed from an ArrayList.
In java version 1, enum was not present. With advancement to version, 1.5 enum was introduced in java which is not as same as seen in C/C++ so do the functionality differs. Enum is basically used to constrict the range for example months, age, or be it a string (considering name of a company) which can not be changed as final values initially itself. In java version 1, enumeration was carried out by interfaces. After version 1.5 it was introduced in java as discussed so simply importing enum class makes working with enum easy simply by importing classes.
ArrayList can be of any type as shown below. The enumeration method of java.util.Collections class is used to return an enumeration over the specified collection.
- An ArrayList of integer, string, double, etc.
- ArrayList of ArrayLists, or simply an ArrayList of HashMaps.
- An ArrayList of any user-defined objects.
Syntax: For declaring an enumeration
public static Enumeration enumeration(Collection c) ;
Method Used: hasMoreElements() Method
An object that implements the Enumeration interface generates a series of elements, one at a time. hasMoreElements() method of enumeration used to tests if this enumeration contains more elements. If enumeration contains more elements than it will return true else false.
Syntax:
boolean hasMoreElements() ;
Parameters: This method accepts nothing.
Return value: This method returns true if and only if this enumeration object contains at least one more element to provide; false otherwise.
Implementation:
Example 1
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> shoppingList = new ArrayList<>();
shoppingList.add( "Perfume" );
shoppingList.add( "Clothes" );
shoppingList.add( "Sandal" );
shoppingList.add( "Jewellery" );
shoppingList.add( "Watch" );
Enumeration e
= Collections.enumeration(shoppingList);
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
|
Output
Perfume
Clothes
Sandal
Jewellery
Watch
Example 2
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> LanguageList = new ArrayList<>();
LanguageList.add( "Java" );
LanguageList.add( "C++" );
LanguageList.add( "Kotlin" );
LanguageList.add( "Scala" );
LanguageList.add( "Ruby" );
LanguageList.add( "Python" );
LanguageList.add( "C#" );
Enumeration e
= Collections.enumeration(LanguageList);
while (e.hasMoreElements())
System.out.println(e.nextElement());
}
}
|
Output
Java
C++
Kotlin
Scala
Ruby
Python
C#
Last Updated :
15 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...