Converting an Enumeration to Iterator
public class ConvertEnumeration {
public static void main(String [] args) {
// int [] ourArray = {0,1,2,3,4,5,6,7,8,9};
Vector<Integer> vector = new Vector<Integer>(Arrays.asList(0,1,2,3,4,5,6,7,8,9));
//Get Enumerator
Enumeration<Integer> enumerator = vector.elements();
EnumerationToIterator<Integer> enumToIt = new EnumerationToIterator<Integer>(enumerator);
while(enumToIt.hasNext()) {
System.out.println(enumToIt.next());
}
}
}
//Convert our enumeration to Iterator!
class EnumerationToIterator<T> implements Iterator<T> {
//Our enumeration
Enumeration<T> enmueration;
//Constructor
public EnumerationToIterator(Enumeration<T> enmueration){
this.enmueration = enmueration;
}
//Our Methods
public boolean hasNext(){
return enmueration.hasMoreElements();
}
public T next(){
return enmueration.nextElement();
}
public void remove(){
throw new UnsupportedOperationException();
}
}
Java 5 and Later
No need to reinvent the wheel. Just use Collections.list(Enumeration<T> e)
, which returns an ArrayList<T>
. Then use ArrayList.iterator()
to get an Iterator
.
Java 9 and Later
Enumerations now have a method to convert directly to an iterator:
enumeration.asIterator();
Wrong assignment in your constructor. It needs to be this.enmueration = enmueration;
enmueration
is the constructor argument, and this.enmueration
is the object attribute.
Java 9 offers a new default method: Iterator<E> asIterator()