Stack - LIFO (Last In First Out)
<aside> ☑️ 참고 블로그
</aside>
| Stack.push(Object o) | Stack에 자료를 넣어준다 |
|---|---|
| Stack.pop() | Stack에서 자료를 빼온다 (최 상단) |
| Stack.peek() | Stack의 가장 최상단에 있는 자료를 조회한다. (출력) |
| Stack.clear() | Stack을 초기화 한다. |
| Stack.contains(Object o) | Collection 기본 method (포함되어 있는지 본다) |
| Stack.size() | Collection 기본 method (stack size를 측정한다) |
| Stack.isEmpty() | |
Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(3);
stack.push(5);
stack.push(10);
stack.push(7);
/* [1, 3, 5, 10, 7] */
//stack pop method (최상단 제거 후 반환)
int a = stack.pop();
System.out.println(a);
/*
7
[1, 3, 5, 10] (pop이후 전체 출력)
*/
//stack 가장 맨 위에 있는거 조회하기 (제거는 x)
int b = stack.peek();
System.out.println(b);
/*
10
[1, 3, 5, 10] (peek이후 전체 출력)
*/
//전체 조회
System.out.println(stack);
//or stack size와 stack get()
for(int i=0; i<stack.size(); i++) {
System.out.print(stack.get(i) + " ");
}
//비어있는지 확인하기
boolean emptyYn = stack.isEmpty();
System.out.println(emptyYn);
/*비어있으면 - true
자료 하나라도 있으면 - fasle
*/