com.xyzwps.lib.dollar.ArrayListReverseIterator Maven / Gradle / Ivy
package com.xyzwps.lib.dollar;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;
/**
* Iterate over an array list in reverse order.
*
* @param element type
*/
public final class ArrayListReverseIterator implements Iterator {
private final ArrayList list;
private int index;
public ArrayListReverseIterator(ArrayList list) {
this.list = Objects.requireNonNull(list);
this.index = list.size();
}
@Override
public boolean hasNext() {
return index > 0;
}
@Override
public T next() {
if (this.index == 0) {
throw new NoSuchElementException();
}
return list.get(--this.index);
}
}