Stack(스택,큐)
//큐,Queue
- 선입선출 구조를 갖는 특징을 가지고 있습니다. -ex) 줄서는 구조
- FIFO( First Input First Output)
add() - 요소를 추가
ex) add("값")
poll() - 요소 접근( 데이터를 꺼내오는것)
size() - 큐에 크기를 나타내는것
clear() - 큐 초기화
contains(단어) - 큐안에 단어가 있는지 boolean값으로 표시
isEmpty() - 큐 안에 비어있는지 확인해주는 것.
peek() - (빠져나올 첫번째 것만 훔쳐보는것) 임시 체크할때 사용
ex)Queue<String> queue = new LinkedList<String>();
//루프탐색
a. 일반 for문
b. 향상된 for문
c. while문
/a.
int size = queue.size();
for(int i=0;i<size;i++) { //queue.size() ->계속 바뀐다.
System.out.println(queue.poll());
}
System.out.println(queue.size());
ArrayList<String> list = new ArrayList<String>();
list.add("데이터");//x100회
size = list.size();
for(int i=0;i<size;i++) { //Arraylist도 중간에 삽입 또는 삭제가 일어나면 list.size가 변하기 때문에 따로 변수에 넣어놓는다.
System.out.println(list.get(i));
}
/b. 사용안함.(Queue 가치가 사라짐)
for(String c : queue) {
System.out.println(c);
}
System.out.println(queue.size());
/c-1
while(true) {
System.out.println(queue.poll());
if(queue.size()==0) {
break;
}
}
/c-2
while(queue.size()>0) {
System.out.println(queue.poll());
}
//스택, Stack
- 후입선출
- LIFO(Last Input First Output)
push() - 요소를 집어넣는 것.
ex) push(값);
pop() - 요소를 꺼내오는 것.
size() - 스택의 크기를 나타내는것
clear() - 스택 초기화
contains(단어) - 스택안에 단어가 있는지 boolean값으로 표시
isEmpty() - 스택 안에 비어있는지 확인해주는 것.
peek() - (빠져나올 첫번째 것만 훔쳐보는것) 임시 체크할때 사용
//ex) Stack<String> stack = new Stack<String>();
* 스택은 더이상 가져올 데이터가 없으면 에러
* 큐는 더이상 가져올 데이터가 없으면 null을 반환