<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/**
 * Some example constructs for iteration.
 * 
 * @author Paul Chew for CS211, Oct 2006
 */

import java.util.Iterator;

/**
 * Uses different schemes to iterate through the items in a collection.
 * None of the schemes in this file are recommended; use an inner class instead.
 */
public class ExampleIteration {
        
    public static void arrayIteration (MyCollection c) {
        // Iteration using an array
        Object[] temp = c.arrayIteration2();
        for (int i=0; i &lt; temp.length; i++)
            System.out.print(" " + temp[i]);
        System.out.println();
    }
    
    public static void arrayLikeIteration (MyCollection c) {
        // Array-like iteration using access methods
        for (int i=0; i &lt; c.numItems(); i ++)
            System.out.print(" " + c.getItem(i));
        System.out.println();
    }
    
    public static void itselfIteration (MyCollection c) {
        // The collection itself maintains the iteration-state (a bad strategy).
        c.resetIteration();
        while (c.hasNext())
            System.out.print(" " + c.getNext());
        System.out.println();
    }
}

/**
 * This is supposed to represent some kind of collection (of Objects) with unknown internal 
 * data structure.
 */
class MyCollection {
    
    private String[] data = {"jan", "feb", "mar", "apr", "may", "jun", 
                             "jul", "aug", "sep", "oct", "nov", "dec"};
    
    private int index;      // State of iteration as part of collection (a BAD strategy).
                            // Used with methods resetIteration(), hasNext(), and getNext().
    
    /**
     * Returns an array for iteration; there are better strategies.
     */
    public Object[] arrayIteration1 () {
        return this.data;   // This is BAD: client now has access to internal data structure
    }
    
    /**
     * Returns an array for iteration; there are better strategies.
     */
    public Object[] arrayIteration2 () {
        Object[] other = new Object[data.length];
        System.arraycopy(data, 0, other, 0, data.length);
        return other;
    }
    
    /**
     * Report number of items in collection; part of an array-like interface.
     * There are better iteration strategies.
     */
    public int numItems () {
        return data.length;
    }
    
    /**
     * Return the i-th item; part of an array-like interface.
     * There are better iteration strategies.
     */
    public Object getItem (int i) {
        return data[i];  // For this collection, it's easy to find the i-th item.
        // It's not always so easy (e.g., think about finding the i-th item in a tree).
    }
    
    /**
     * Reset iteration; the collection itself maintains iteration-state (a bad strategy).
     */
    public void resetIteration () {
        index = 0;
    }
    
    /**
     * More iteration?; the collection itself maintains iteration-state (a bad strategy).
     */
    public boolean hasNext () {
        return index &lt; data.length;
    }
    
    /**
     * Next item in iteration; collection itself maintains iteration-state (a bad strategy).
     */
    public Object getNext () {
        return this.data[index++];
    }
}

</pre></body></html>