As part of an exercise, am implementing an ArrayList which will support Enumerations.
Following is the adapter that converts an Iterator to an Enumeration:
public class MyEnumeratorAdapter<Object> implements Enumeration<Object> {
private Iterator<Object> adaptee;
public MyEnumeratorAdapter(Iterator<Object> it) {
this.adaptee = it;
}
@Override
public boolean hasMoreElements() {
return adaptee.hasNext();
}
@Override
public Object nextElement() {
return adaptee.next();
}
}
and my arraylist class is:
public class MyArrayList<Object> extends ArrayList<Object> {
public MyArrayList() {
this.enumerator = new MyEnumeratorAdapter<Object>(this.iterator());
}
public Enumeration<Object> enumerator() {
return this.enumerator;
}
public boolean hasMoreElements() {
return this.enumerator.hasMoreElements();
}
public Object nextElement() {
return this.enumerator.nextElement();
}
private static final long serialVersionUID = 1L;
private Enumeration<Object> enumerator;
}
however, when I try this testing with the following code, I'm getting java.util.ConcurrentModificationException
public static void main(String[] args) {
MyArrayList<String> names = new MyArrayList<String>();
names.add("jim");
names.add("jack");
names.add("jai");
for (Enumeration<String> iterator = names.enumerator(); iterator
.hasMoreElements();) {
String name = (String) iterator.nextElement();
System.out.println(name);
}
}
what mistake am I doing?
Can I have an ArrayList class that support Enumerations?