import
java.util.*;
public
class
apples{
public
static
void
main(String[] args){
Stack<String>
stack = new
Stack<String>();
stack.push("first");
→ 放first入Stack
printStack(stack);
stack.push("second");
→ 放second入Stack
printStack(stack);
stack.push("third");
→ 放third入Stack
printStack(stack);
stack.pop();
→ 收回Stack剛才(最後)放入的內容(third)
printStack(stack);
stack.pop();
→ 收回Stack剛才(最後)放入的內容(second)
printStack(stack);
stack.pop();
→ 收回Stack剛才(最後)放入的內容(first)
printStack(stack);
}
private
static
void
printStack(Stack<String> s){
if(s.isEmpty())
System.out.println("you
have nothing in Stack");
else
System.out.printf("%s
TOP\n", s);
}
}
這是運行時的結果:
[first]
TOP
[first,
second] TOP
[first,
second, third] TOP
[first,
second] TOP
[first]
TOP
you
have nothing in Stack
沒有留言:
張貼留言