public static void main(String[] args)
{
Stack<String> stack = new
Stack<String>();
System.out.println("now the stack is "
+ isEmpty(stack));
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.push("5");
System.out.println("now the stack is "
+ isEmpty(stack));
System.out.println(stack.peek());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.search("2"));
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
public static String isEmpty(Stack<String> stack) {
return stack.empty() ? "empty" : "not empty";
}
now the stack is empty
now the stack is not empty
5
5
4
2
3
2
1
Exception in thread "main" java.util.EmptyStackException
at
java.util.Stack.peek(Unknown Source)
at
java.util.Stack.pop(Unknown Source)
at com.main.StackClass.main(StackClass.java:22)
没有评论:
发表评论