Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

The java.util Package - The Iterator Interface

  • used to sequentially access collection elements
  • element order depends on the collection ie List elements are presented as they appear in the List, Set elements can be in any order
Iterator Methods
hasNext() returns true if the iteration has more elements
next() returns the next element in the iteration
remove() removes the most recently retrieved element from the underlying collection
  • has one subinterface, ListIterator, which allows a programmer to traverse a List in either direction and make modifications to the underlying List
java.util.ListIterator Methods
Query Methods
hasNext() returns true if there are more elements in a forward direction
hasPrevious() returns true if there are more elements in a backward direction
next() returns the next element in the List
nextIndex() returns the index of the next element in the list, or, the size of the list if there are no more elements
previous() returns the previous element in the List
previousIndex() returns the index of the previous element in the list. If positioned at the first element, returns -1
Modification Methods
add(Object obj) inserts the new object immeadiately before the element which would be returned by next().
remove() removes the last element in the List retrieved by a next() or previous() operation.

Can only be made once after a next() or previous() operation and cannot be made if there has been an intervening add().
set(Object obj) replaces the last element in the List retrieved by a next() or previous() operation; there can be no intervening call to add() or remove().


Collections Framework Collection Abstract Classes Iterator List