자바/Collections Framework

컬렉션프레임웍(Collections Framework) - 큐(Queue)

은은하게미친자 2022. 10. 2. 02:05
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        //큐
        /*
         * 큐
         * LinkedList 로 생성
         * 
         * rear >>> data  >>>  front
         * add : rear로 값 추가
         * peek : front 데이터 확인
         * poll - front데이터 삭제하고 확인
         */
        
        Queue<Integer> que = new LinkedList<Integer>();
        for (int i= 1;i<=10;i++) {
            que.add(i);
        }
        System.out.println("=============que");
        System.out.println("que" + que.toString());
        System.out.println("que peek : " + que.peek());
        
        que.poll();
        System.out.println("poll " + que.toString());
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class code1_que {
    public static void main(String[] args) {
        System.out.println("Queue test");
        //FIFO
        Queue<String> que = new LinkedList<String>();
        
        // 값 집어넣기
        que.add("item 1 push");
        que.add("item 2 push");
        System.out.println(que);
        System.out.println(que.offer("item 3 push"));
        System.out.println(que);
        // 나올 값 확인하기 
        System.out.println(que.peek());
        // 값 빼기
        que.poll();
        System.out.println(que);
    }
}
 
cs
728x90