package examples; import java.util.*; /** * CS401 Example 28d * * * * @author PJ Dillon */ public class ex28d { public static void main(String[] args) { String[] strs = {"transaction", "seriptitious", "sharpener", "Baseball", "perspecacity", "constellation", "Marzipan", "negociation", "temper", "restore", "set", "jocularity"}; /* * The Stack class is a concrete class of which we can declare instances. It * implements the Collection interface, but we use a Stack reference here so * as to use the Stack behavior. */ Stack stack = new Stack(); /* * the push() method is used to add an item on to the Stack. As this is * done, each item is printed out to show the order in which the strings are * pushed on to the Stack. */ System.out.println("Pushing:"); for(String str : strs) { System.out.print('\t'); System.out.println(str); stack.push(str); } /* * The pop() method returns and removes the top object from the stack, which * is printed to the screen here. The order in which the strings are printed * when they're removed will then be the reverse order in which they were * added, demonstrating the LIFO order of stack access. */ System.out.println("\nPopping:"); while(!stack.isEmpty()) { System.out.print('\t'); System.out.println(stack.pop()); } } }