I recently posted some reusable code that facilitates reverse iteration over a list in Java.
Here is another potentially useful Java method related to iteration, from my buddy Jordan Armstrong:
import org.w3c.dom.Node; import org.w3c.dom.NodeList;
/**
* @param n An XML node list
* @return A newly created Iterable for the given node list, allowing
* iteration over the nodes in a for-each loop. The iteration
* behavior is undefined if concurrent modification of the node list
* occurs.
*/
public static Iterable<Node> iterable(final NodeList n) {
return new Iterable<Node>() {
@Override
public Iterator<Node> iterator() {
return new Iterator<Node>() {
int index = 0;
@Override
public boolean hasNext() {
return index < n.getLength();
}
@Override
public Node next() {
if (hasNext()) {
return n.item(index++);
} else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}