Iterator Class Exercise


Use eclipse for this exercise.

  1. Copy all files from Shared Files, Iterator Exercise folder. You will also need your Collection exercise files.
  2. The Collection in main should be a LinkedCollection of Person objects.
  3. Create a class which is an iterator for the LinkedCollection. This class must implement the Iterator interface. You must think about what field(s) you need to keep track of the current element in the iteration, how to initialize the field(s), how to move to the next element, and how to know when all elements have been returned.
  4. The LinkedCollection must implement the Iterable interface. Write the iterator() method which returns an Iterator object.
  5. Add a loop at the end of main to print the collection.
  6. Run your program using the person.dat file and make sure it works
  7. Change your program so that the class for the iterator is an inner class contained in the LinkedCollection class.
  8. Change the loop in main: if you used hasNext() and getNext(), use a foreach loop; if you used the foreach loop, use hasNext() and getNext().
  9. Check your program again.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2017