JAVA_25_Stack_컬렉션

hyeong taek jo·2023년 7월 11일

JAVA

목록 보기
25/39

📌1.Stack 이란?

  • 객체를 후입선출, last-in-first-out(LIFO)이며 객체의 저장시의 push()메서드와 검출 시 사용하는 pop()과 Stack의 가장 위쪽 객체를 의미하는 peek()메서드 그리고 Stack이 비어있는지 판별해주는 empty()와 객체를 검색해주는 search()메서드들로 Vector라는 클래스를 확장하였다

📌2.Stack 예시

import java.util.Stack;

public class StackEx {

	public static void main(String[] args) {
		String[] pet = {"강아지", "야옹이", "물고기", "야옹이"};
		
		Stack<String> st = new Stack<>();
		for (String str : pet) {
			System.out.println("push--->" + str);
			st.push(str);
		}
		
		System.out.println("----------1---LIFO-------");
		while(!st.isEmpty()) {
			System.out.println("POP--->" + st.pop());
		}
		
		System.out.println("----------2--------------");
		while(!st.isEmpty()) {
			System.out.println("POP-->" + st.pop());
		}
	}
}

push--->강아지
push--->야옹이
push--->물고기
push--->야옹이
----------1---LIFO-------
POP--->야옹이
POP--->물고기
POP--->야옹이
POP--->강아지
----------2--------------

profile
마포구 주민

0개의 댓글