Treating an Array as an Enumeration : Array Collections « Collections Data Structure « Java

Treating an Array as an Enumeration

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

final public class ArrayEnumerationFactory {
  static public Enumeration makeEnumeration(final Object obj) {
    Class type = obj.getClass();
    if (!type.isArray()) {
      throw new IllegalArgumentException(obj.getClass().toString());
    } else {
      return (new Enumeration() {
        int size = Array.getLength(obj);

        int cursor;

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

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

  public static void main(String args[]) {
    Enumeration e = makeEnumeration(args);
    while (e.hasMoreElements()) {
    e = makeEnumeration(new int[] { 1, 3, 4, 5 });
    while (e.hasMoreElements()) {
    try {
      e = makeEnumeration(new Double(Math.PI));
    } catch (IllegalArgumentException ex) {
      System.err.println("Can't enumerate that: " + ex.getMessage());

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.ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements 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