class List<T> implements Iterable<T> {
// code is here ...
public Iterator<T> iterator() {
// что нужно сюда писать?
}
class Node<T>{
T value;
Node<T> next;
// interface of Node is here.
}
}
Iterator<T> implements java.util.Iterator<T>{}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private Node<T> current = first;
@Override
public boolean hasNext() {
return current.hasNext();
}
@Override
public T next() throws IndexOutOfBoundsException {
T result = current.value;
if (!current.hasNext()) throw new IndexOutOfBoundsException("End of list.");
current = current.next;
return result;
}
};
}
import java.util.Iterator;
public class SOList<Type> implements Iterable<Type> {
private Type[] arrayList;
private int currentSize;
public SOList(Type[] newArray) {
this.arrayList = newArray;
this.currentSize = arrayList.length;
}
@Override
public Iterator<Type> iterator() {
Iterator<Type> it = new Iterator<Type>() {
private int currentIndex = 0;
@Override
public boolean hasNext() {
return currentIndex < currentSize && arrayList[currentIndex] != null;
}
@Override
public Type next() {
return arrayList[currentIndex++];
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
return it;
}
}