컬렉션 클래스 > Stack클래스
import java.util.Stack;
public class StackEx {
public static void main(String[] args) {
Stack stack = new Stack(); //빈 스택 생성
stack.push(new Integer(1)); //10개의 integer를 스택에 push
stack.push(new Integer(2));
stack.push(new Integer(3));
stack.push(new Integer(4));
stack.push(new Integer(5));
stack.push(new Integer(6));
stack.push(new Integer(7));
stack.push(new Integer(8));
stack.push(new Integer(9));
stack.push(new Integer(10));
while(!stack.empty()) {
Integer element = (Integer)stack.pop();
//스택이 빌때까지 pop()으로 스택에 있는 모든 요소 제거
System.out.println("Element: " + element);
}
}
}
Element: 10
Element: 9
Element: 8
Element: 7
Element: 6
Element: 5
Element: 4
Element: 3
Element: 2
Element: 1
//나중에 입력된 10부터 제거되었다.