I have one question(correction?) regarding Java advanced lesson . Ultimate Java Part 3: Advanced Topics->Collections->5. The iterator interface. I wonder, why did we need to pass a copy of the reference to GenericList object to the ListIterator inner class, and save it in the inner class property? The inner class already has access to all the outer class properties, what was the point of passing it? Am I missing something? Basically the constructor and the property in the inner class wasn’t needed.
What did I miss?
package generics;
import java.util.Iterator;
public class GenericList implements Iterable {
private T[] genericArray;
int count = 0;
public GenericList(int length) {
genericArray = (T[]) new Object[length];
}
public void add(T item) {
genericArray[count++] = item;
}
public T indexOf(int index) {
return genericArray[index];
}
@Override
public Iterator<T> iterator() {
return new ListIterator<T>() ;
}
private class ListIterator<T> implements Iterator<T> {
private int index;
@Override
public boolean hasNext() {
return count > index;
}
@Override
public T next() {
return (T) genericArray[index++];
}
}
}