CS 261 University of Puget Sound Spring, 2020
 
Computer Science II
Abstract Data Types and their Implementations, Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency
 

Warning: This course is under development. Although the basic structure of this course is largely established, nothing on this Web site should be considered official or even possibly correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.

Implementing the ArrayList Class and refining the SchoolDirectory Class

The first part of this lab extends the ArrayList class developed in previous lab on ArrayLists, and the second part of the lab adds an iterator to the SchoolDirectory class from the earlier lab on Generalization

Adding an Iterator to ArrayLists

An Iterator is an object that allows a program to move sequentially through elements in a collection. When the collection has an order (e.g., item at index 0, item at index 1, etc.), the Iterator normally is expected to maintain the order.

In implementing an Iterator, one normally does not copy the full structure. Rather, the Iterator maintains a reference to the structure through some internal field. Also, the Iterator likely maintains a few private fields (usually just one or two fields) to keep track of where processing within the structure is taking place.

Since an Iterator moves sequentially through a structure, troubles could arise if the structure changed during processing. Thus, an Iterator normally provides a method remove to change the current item in the structure. However, any other changes to the structure typically yield an exception. Also, note that remove for the Iterator can only be called once for each call to next.

  1. Using your ArrayList class from the previous lab on ArrayLists, implement an Iterator method for MyArrayList. See examples of iterators in MyArray1.java and MyArray2.java. (The second example may be more relevant here.)

Refining the SchoolDirectory Class

The lab on Generalization included class definitions for each entry of a schoolDirectory, including Person, Student, and and Faculty. Further, class SchoolDirectory provided an implementation for a collection which maintained entries in an array, ordered by name (last name and first name).

  1. Implement an iterator within SchoolDirectory.java that will return successively those entries in the directory for which the firstName contains less than 6 characters and the lastName contains more than 6 characters.

  2. Expand both SchoolDirectory.java and the iterator, to implement a remove method (with parameters first and last names).

Notes:


created for ArrayList class 30 March 2012
revised 3 April 2012
iterator material added 30 January 2020
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.