2012年6月4日 星期一

Intermediate Java(14) - Stacks, push, pop


import java.util.*;
public class apples{
public static void main(String[] args){
Stack<String> stack = new Stack<String>();
stack.push("first"); firstStack
printStack(stack);
stack.push("second"); secondStack
printStack(stack);
stack.push("third"); thirdStack
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





沒有留言:

張貼留言