Array Enumeration

Array Enumeration

Is it possible to return an Enumeration of an array?

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several