CPSC 2150 - DAY 18 OCTOBER 27, 2O16 ================================================================================ Three methods in the Iterator interface .hasNext() .returns true iff the iteration has more elements .public E next() .returns the next element in the iteration .an exception will be thrown if there is no next element .Note use of generics in return type .public void remove() .Remove the next element Special For-loop syntax .Syntatic shortcut for looping through something iterable for (Type loop-var : set-expression List Iterator interface extends Iterator interface .Provides ordering guarantee for iteration .Adds methods for moving forwards or backwards .Methods: .public boolean hasNext()/ hasPrevious() .public E next() / E previous() .public int nextIndex() / int previousIndex() .public void remove() .public void add (E elem) .public void set (E elem) While iterating though a collection, the only safe way to modify the collection is through the iterator itself .Use iterator's remove() method, not the Collection's remove (Object) method .Many iterators in Java SDK try to detect a modification of the underlying collection and complain Array list has dynamically changeable size