I have these code:
class ReversibleArrayList<T> extends ArrayList<T> {
public ReversibleArrayList(Collection<T> c) {
super(c);
}
public Iterable<T> reversed() {
return new Iterable<T>() {
public Iterator<T> iterator() {
return new Iterator<T>() {
int current = size() - 1;
@Override
public boolean hasNext() {
return current >= 0;
}
@Override
public T next() {
return get(current--);
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}
This is code is used to make ArrayList can be reversed in foreach.
There are two question:
1、I don't understand the method public Iterable<T> reversed(),
the return value of this method is Iterable and there also has Iterator.
2、I think Interable just an interface, Does an interface can be used for return value?