devxlogo

Array Enumeration

Array Enumeration

Question:
Is it possible to return an Enumeration of an array?

Answer:
There is no standard class for creating an Enumeration of an array.More often than not, when you use arrays, you will iterate throughtheir elements using a loop and direct indices. Creating anEnumeration will eliminate the performance gain of using an array byrequiring a method call for each element access. If you’re going toend up doing this, you might as well use a container class instead ofan array. However, there is at least one good reason to want toassociate an Enumeration with an array. If you have genericalgorithms that use Enumerations to iterate through containers,creating an Enumeration from an array may be the only way to applythose algorithm implementations to an array without duplicating code.

It is simple enough to write your own array enumeration if, followingthe Collections Framework’s design, you do not make it thread safe.An alternative is to use Arrays.asList (Object[]) to convert an Arrayto a List, and then create an Iterator (the preferred enumerationconstruct as of JDK 1.2) instead of an Enumeration. The followingexample implements an ArrayEnumeration that allows you to iteratethrough an entire array or a subsection of it. To apply it to arraysof primitive types, you have to change the code to use the specificprimitive type array because Java does not have a template mechanismakin to C++.

import java.util.*;public class ArrayEnumeration implements Enumeration, Iterator {  protected int _currentIndex_, _lastIndex_;  protected Object[] _array_;  public ArrayEnumeration(Object[] array, int startIndex, int length) {    _currentIndex_ = startIndex;    _lastIndex_ = startIndex + length - 1;    _array_ = array;  }  public ArrayEnumeration(Object[] array) {    this(array, 0, array.length);  }  public boolean hasMoreElements() {    return (_currentIndex_ <= _lastIndex_);  }  public boolean hasNext() {    return hasMoreElements();  }  public Object nextElement() {    if(!hasMoreElements() || _currentIndex_ < 0 ||       _currentIndex_ >= _array_.length)      throw new NoSuchElementException();    return _array_[_currentIndex_++];  }  public Object next() { return nextElement(); }  public void remove() {    throw new UnsupportedOperationException();  }  public static final void main(String[] args) {    Integer[] array = new Integer[10];    Enumeration enum;    for(int i = 0; i < array.length; ++i)      array[i] = new Integer(i);    enum = new ArrayEnumeration(array, 2, 5);    // Should print the numbers 2 through 6    while(enum.hasMoreElements())      System.out.println(enum.nextElement());  }}

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist