자바/Collections Framework
컬렉션프레임웍(Collections Framework) - 스택(stack)
은은하게미친자
2022. 10. 2. 02:04
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*
* 스택
* push 넣기
* peek 마지막값 확인하기
* pop 꺼내기
*/
Stack<String> stack = new Stack<String>();
//우선수위큐 최댓값 혹은 최소값으로
for (int i= 1;i<=10;i++) {
stack.push(Integer.toString(i));
}
System.out.println("stack" + stack.toString());
System.out.println(stack.peek());
stack.pop();
System.out.println("stack pop" + stack.toString());
stack.remove(3);
System.out.println("stack remove" + stack.toString());
stack.remove(0);
System.out.println("stack remove" + stack.toString());
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class code1_stack {
public static void main(String[] args) {
System.out.println("stack test");
//FILO
Stack<String> stack = new Stack<String>();
// 비어있는지 확인
System.out.println("stack이 비어있나요? " + stack.empty());
// 값 집어넣기
stack.push("item 1 push");
stack.push("item 2 push");
System.out.println(stack);
// 나올 값 확인하기
System.out.println(stack.peek());
// 나올 순서 찾기 : 인덱스는 1부터 시작
System.out.println(stack.search("item 1 push"));
System.out.println(stack.search("item 2 push"));
// 값 빼기
stack.pop();
System.out.println(stack);
}
}
|
728x90