How to Get Enumeration Over ArrayList in Java


Here this example will write an example to get Enumeration over ArrayList with enumeration method of Collections class in Java. The example will also prompt user to enumerate through elements of ArrayList in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class GetEnumerationOverArrayList {

	public static void main(String[] args) {
		//create ArrayList.
		ArrayList arrayList = new ArrayList();
		
		arrayList.add("A");
		arrayList.add("B");
		arrayList.add("C");
		
		/*
		 Get Enumeration over Java ArrayList object using enumeration(Collection c) method of Collections.
		*/
		Enumeration enumeration = Collections.enumeration(arrayList);
		
		while(enumeration.hasMoreElements())
		      System.out.println(enumeration.nextElement());
		  }
}

Output:

A
B
C

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments