StackクラスはVectorを拡張し、LIFO型(最後に入れたものを最初に取り出す方式)スタックを提供します。
例
#codeprettify{{ import java.util.*;
class Pushpop{
public static void main(String args[]){
//スタックを作成する
Stack stack = new Stack();
//要素をスタックにプッシュする
for ( int i = 0; i < args.length; i++){
stack.push(new Integer(args[i]));
}
//スタックから要素をポップして表示する
while(!stack.empty()){
Object obj = stack.pop();
System.out.println(obj);
}
}
} }}
結果
c:\>java Pushpop 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1