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());  }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.