K
Kabal
Anyone have a better way to implement the below code...
import java.util.Collection;
import java.util.NoSuchElementException;
public class ReverseIterator {
private Object[] elements = null;
private int n;
public ReverseIterator(Collection collection) {
elements = collection.toArray();
n = elements.length - 1;
}
public Object next() {
try {
return elements[n--];
} catch(IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public boolean hasNext() {
return (n >= 0);
}
}
import java.util.Collection;
import java.util.NoSuchElementException;
public class ReverseIterator {
private Object[] elements = null;
private int n;
public ReverseIterator(Collection collection) {
elements = collection.toArray();
n = elements.length - 1;
}
public Object next() {
try {
return elements[n--];
} catch(IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public boolean hasNext() {
return (n >= 0);
}
}