ArrayList now implements ListIterator (for reverse traversals of lists)

This commit is contained in:
Eric Scharff 2007-09-26 09:48:59 -06:00
parent 09efe501f5
commit 0beba6cafa
3 changed files with 30 additions and 2 deletions

View File

@ -154,4 +154,8 @@ public class ArrayList<T> implements List<T> {
public Iterator<T> iterator() {
return new Collections.ArrayListIterator(this);
}
public ListIterator<T> listIterator(int index) {
return new Collections.ArrayListIterator(this, index);
}
}

View File

@ -102,13 +102,31 @@ public class Collections {
}
}
static class ArrayListIterator<T> implements Iterator<T> {
static class ArrayListIterator<T> implements ListIterator<T> {
private final List<T> list;
private boolean canRemove = false;
private int index = -1;
private int index;
public ArrayListIterator(List<T> list) {
this(list, 0);
}
public ArrayListIterator(List<T> list, int index) {
this.list = list;
this.index = index - 1;
}
public boolean hasPrevious() {
return index >= 0;
}
public T previous() {
if (hasPrevious()) {
canRemove = true;
return list.get(index--);
} else {
throw new NoSuchElementException();
}
}
public T next() {

View File

@ -0,0 +1,6 @@
package java.util;
public interface ListIterator<E> extends Iterator<E> {
public boolean hasPrevious();
public E previous();
}