ArrayEnumeration class (implements Enumeration) : Array Collections « Collections Data Structure « Java

ArrayEnumeration class (implements Enumeration)

ArrayEnumeration class (implements Enumeration)

import java.lang.reflect.Array;
import java.util.Enumeration;

public class ArrayEnumeration implements Enumeration {
  private final int size;

  private int cursor;

  private final Object array;

  public ArrayEnumeration(Object obj) {
    Class type = obj.getClass();
    if (!type.isArray()) {
      throw new IllegalArgumentException("Invalid type: " + type);
    size = Array.getLength(obj);
    array = obj;

  public boolean hasMoreElements() {
    return (cursor < size);

  public Object nextElement() {
    return Array.get(array, cursor++);

  public static void main(String args[]) {
    Object obj = new int[] { 2, 3, 5, 8, 13, 21 };
    ArrayEnumeration e = new ArrayEnumeration(obj);
    while (e.hasMoreElements()) {
    try {
      e = new ArrayEnumeration(ArrayEnumeration.class);
    } catch (IllegalArgumentException ex) {


Related examples in the same category

1.Array Iterator
2.Array MapArray Map
3.Array SetArray Set
4.Array Int Set
5.Remove duplicate element from array
6.Convert an Array to a List
7.Converting an Array to a Collection
8.Converting a Collection of user objects to an Array
9.Create an array containing the elements in a set
10.Convert an array to a Map
11.Converting a Collection of String to an ArrayConverting a Collection of String to an Array
12.Treating an Array as an Enumeration
13.Custom ArrayMap implementation (extends AbstractMap)Custom ArrayMap implementation (extends AbstractMap)
14.Custom ArraySet implementation (extends AbstractSet)Custom ArraySet implementation (extends AbstractSet)
15.Converts array into a java.util.Map.
16.Growable array of intsGrowable array of ints
17.Growable array of floats.
18.Acts like an java.util.ArrayList but for primitive int valuesActs like an java.util.ArrayList but for primitive int values
19.Acts like an java.util.ArrayList but for primitive long values
20.Add array to collection