How to Create Enumeration of ArrayList in Java


The example aims to create Enumeration of an ArrayList in Java

Source Code

package com.beginner.examples;

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

public class CreateEnumerationOfArraylist {

	public static void main(String[] args) {

		ArrayList al = new ArrayList();

		// Add Elements
		al.add("A");
		al.add("B");
		al.add("C");
		al.add("D");
		al.add("E");

		// Create the enumeration object using the enumeration() method in the
		// Collections utility class
		Enumeration alEmun = Collections.enumeration(al);

		// Iterate alEmun
		System.out.print("alEmun:{");
		while (alEmun.hasMoreElements()) {

			String e = alEmun.nextElement();

			// Determines whether there is a next element, and if there is a
			// next element, prints a comma
			if (alEmun.hasMoreElements()) {

				System.out.print(e + ",");

			} else {
				System.out.print(e);
			}

		}
		System.out.println("}");

	}

}

Output:

alEmun:{A,B,C,D,E}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments