用程式來說明一切吧
package com.tianyu.summer;
import java.util.*;
public class StackTest {
public StackTest() {
Stack stack = new Stack();
Boolean bool = Boolean.TRUE;
Character character = new Character('$');
try {
stack.push(bool);
printStack(stack);
stack.push(character);
printStack(stack);
stack.pop(); //
printStack(stack);
stack.pop();
printStack(stack);
stack.pop();
} catch (EmptyStackException e) {
e.printStackTrace();
}
}
public void printStack(Stack stack) {
if (stack.isEmpty()) {
System.out.println("堆棧為空");
} else {
System.out.print("堆棧包含:");
Enumeration items = stack.elements();
while (items.hasMoreElements()) {
System.out.print(items.nextElement() + " ");
}
System.out.print("/n");
}
}
public void removeStack(Stack stack) {
if (stack.isEmpty()) {
System.out.println("堆棧為空");
} else {
System.out.print("堆棧包含:");
Enumeration items = stack.elements();
while (items.hasMoreElements()) {
System.out.print(items.nextElement() + " ");
}
System.out.print("/n");
}
}
public static void main(String[] args) {
new StackTest();
}
}