5.2 Java-style Iteration

For iteration in the style of Java iterators, there are methods:

boolean         hasNext()
<iterated type> next()

where hasNext returns true is the iterator has another object left, and next returns the current "object" and then increments the iterator. Note that next returns a reference to the actual type inside the iterator (OEAtomBase, OEBondBase, etc.) and does not require a cast like some Java containers.

To us a for loop over the atoms in a molecule and print their atomic number:

for (OEAtomBaseIter aiter = mol.GetAtoms();aiter.hasNext();) {
    OEAtomBase atom = aiter.next();
    System.out.println(atom.GetAtomicNum());
}

this can also be accomplished with a while loop:

OEAtomBaseIter aiter = mol.GetAtoms();
while (aiter.hasNext()) {
    OEAtomBase atom = aiter.next();
    System.out.println(atom.GetAtomicNum());
}