mirror of
https://github.com/corda/corda.git
synced 2025-02-27 03:27:34 +00:00
ArrayList now implements ListIterator (for reverse traversals of lists)
This commit is contained in:
parent
09efe501f5
commit
0beba6cafa
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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() {
|
||||
|
6
classpath/java/util/ListIterator.java
Normal file
6
classpath/java/util/ListIterator.java
Normal file
@ -0,0 +1,6 @@
|
||||
package java.util;
|
||||
|
||||
public interface ListIterator<E> extends Iterator<E> {
|
||||
public boolean hasPrevious();
|
||||
public E previous();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user