Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Algorithms
Expertise: Beginner
Jul 6, 2000

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 through their elements using a loop and direct indices. Creating an Enumeration will eliminate the performance gain of using an array by requiring a method call for each element access. If you're going to end up doing this, you might as well use a container class instead of an array. However, there is at least one good reason to want to associate an Enumeration with an array. If you have generic algorithms that use Enumerations to iterate through containers, creating an Enumeration from an array may be the only way to apply those algorithm implementations to an array without duplicating code.

It is simple enough to write your own array enumeration if, following the Collections Framework's design, you do not make it thread safe. An alternative is to use Arrays.asList (Object[]) to convert an Array to a List, and then create an Iterator (the preferred enumeration construct as of JDK 1.2) instead of an Enumeration. The following example implements an ArrayEnumeration that allows you to iterate through an entire array or a subsection of it. To apply it to arrays of primitive types, you have to change the code to use the specific primitive type array because Java does not have a template mechanism akin 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());

  }
}

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap