Running the Program
Home ] Up ] Enumerating a Stack ] [ Running the Program ]

 

 

Now let's see what our Stack and StackEnumerator classes can do. (Note that this is by no means a thorough test!)

package examples;
/**
 * Tests the Stack and StackIterator classes
 */
public class StackTest
{
  /**
   * Main entry point
   */
  public static void main(String[] args)
  {
    Stack aStack = new Stack(10);
    for (int i = 0; i < aStack.getMaxStackSize(); i++)
      aStack.push(i*i);
    
    StackEnumerator enumerator = new StackEnumerator(aStack);
    while (enumerator.hasMoreElements())
    {
      int value = (Integer) enumerator.nextElement();
      System.out.print(" " + value);
    }
    System.out.println();
  }
}

As you can see, the main method creates an instance of Stack, pushes some values onto the stack, and then uses a StackEnumerator to enumerate all the values on the stack. 

When run, this program outputs the following:

 0 1 4 9 16 25 36 49 64 81
 

This page was last modified on 02 October, 2007