Iterator: Object that traverses a collection of data.
int
.Tip: Enhanced for loop can implicitly call a class’s iterator, like so:
<String> bag = new ArrayBag<>(); // (assume ArrayBag implements an iterator) BagInterfacefor (String x : bag) { // etc... }
- This is not a language feature, but part of the class structure.
Remember: Iterators traverse serially, you can’t traverse backwards.
Note:
toArray()
- The
toArray()
we implemented inArrayBag.java
is basically a naive iterator that uses lots of memory.
Example: Iterator interface
package java.util; // (this must be imported) public interface Iterator<T> { // Required boolean hasNext(); next(); T // Optional (default) void remove(); }
- Remember: A class can implement multiple things!
Example: Iterable interface
package java.lang; public interface Iterable<T> { Iterator<T> iterator(); }
Example: Iterable ArrayBag
public class ArrayBag<T> implements BagInterface<T>, Iterable<T> { // (ArrayBag implementation here) @Override public Iterator<T> iterator() { return new BagIterator(); } private class BagIterator<T> implements Iterator<T>{ private int cursor; public BagIterator() { = 0; cursor } @Override public boolean hasNext() { return cursor < entries; } @Override public T next() { if (hasNext()) { return bag[cursor++]; } throw new NoSuchElementException(); } } }
Usage:
Option 1: # Iterator<String> bagIterator = bag.iterator(); while (bagIterator.hasNext()) { System.out.println(bagIterator.next()); } Option 2: # for (String x : bag) { System.out.println(x); }