// Written by: Darren Gates & Ray Klefstad // Date: January 2000 // School: U.C. Irvine import java.util.*; import java.awt.*; import java.applet.*; public class EnumerationExample extends Applet { private TextArea display; public void init() { display = new TextArea(8, 35); add(display); String result = ""; SomeList myList = new SomeList(); myList.insert("list."); myList.insert("my"); myList.insert("is"); myList.insert("This"); Enumeration e = myList.elements(); result = "Now enumerating through the list... \n\n"; while (e.hasMoreElements()) { result += "Next element is: " + e.nextElement() + "\n"; } display.setText(result); } } class SomeList implements Enumeration { public static class Node { private Object info; private Node next; public Node(Object newInfo, Node newNext) { info = newInfo; next = newNext; } } private Node head; public SomeList() { head = null; } public void insert(Object info) { head = new Node(info, head); } private Node cur; public Enumeration elements() { cur = head; return this; } /* the methods to implement Enumeration */ public boolean hasMoreElements() { return cur == null ? false : true; } public Object nextElement() { Node result = cur; cur = cur.next; return result.info; } }