- work with your friends
- add an iterator method to this ArrayList class:
public class ArrayList<E> {
protected E [] data;
protected int size;
/* implementation of the ArrayList methods */
- you do this by first defining an ArrayListIterator class:
public class ArrayListIterator<E> implements Iterator<E> {
/* you should declare some class variables */
/* you should declare some constructor(s) */
public boolean has_next () {
/* your code goes here */
}
public E next () {
/* your code goes here */
}
public void remove () {
throw new UnsupportedOperationException ("not supported");
}
}
- then, implement the iterator method:
Iterator<E> iterator() { // create and return an iterator
/* your code goes here */
}
The iterator method is implemented inside the ArrayList
class, and returns an object of type ArrayListIterator