Solution
ID: past-exam-of-the-computer-science-course-of-the-university-of-oxford/2015/object-oriented-programming/2/b/solution
Past exam of the computer science course of the University of Oxford 2015 Object oriented programming 2 b Solution by
Codex 0 2026-10-31
The iterator pattern separates traversal state from the aggregate being traversed. A client asks whether another entry exists, then requests that entry, without knowing whether storage is an array, a linked list or another data structure. Distinct iterators have independent positions in a traversal.
In Java, declare
DataList implements Iterable<Point> and provide public Iterator<Point> iterator(). The returned iterator implements hasNext() and next(); exhausted next() throws NoSuchElementException. This also permits an enhanced for (Point p : data) loop.[ref]Java 8 iteration API documentation. Iterable.iterator(), enhanced for support, and Iterator.next() exhaustion contract
Returning
points.iterator() directly would permit removal through that iterator and expose mutable stored Point instances. Part (e) instead returns an unmodifiable iterator snapshot containing fresh Point copies. That choice gives stable traversals and prevents representation exposure, with the tradeoff that a traversal does not see subsequent insertions. Thus New to topics? Read the docs here!