-
Java - Stack, QueueJava 2018. 1. 25. 13:12반응형// Stack - LIFO// 마지막에 들어온 최신 것을 이용할 때 유용// Queue - FIFO// 순차적으로 작업 처리할 때 유용< Stack >import java.util.Stack;public class TowelBox {public static void main(String[] args) {// Stack -> LIFOStack<Towel> box = new Stack<Towel>();// pushbox.push(new Towel("red"));box.push(new Towel("orange"));box.push(new Towel("yellow"));box.push(new Towel("green"));box.push(new Towel("blue"));box.push(new Towel("navy"));// popwhile(!box.isEmpty()) {System.out.println(box.pop().getColor());// box.peek(); // 꺼내서 확인하고 다시 집어넣음, 반복문에서 실행하면 계속 돌음}}}< Queue >import java.util.LinkedList;import java.util.Queue;public class JobQueue {public static void main(String[] args) {// Queue는 LinkdedList를 활용하는 방법만 제시Queue<Job> queue = new LinkedList<Job>();queue.offer(new Job("send SMS", "Alice"));queue.add(new Job("send SMS", "Blice"));queue.add(new Job("send SMS", "Clice"));queue.offer(new Job("send SMS", "Dlice"));queue.add(new Job("send SMS", "Elice"));System.out.println(queue.size());for(int i = 0; i < queue.size(); ) { // queue.size가 줄어든다, size를 int형에 담아두고 써도 된다.Job job = queue.poll();// queue.peek(); // queue에도 peek이 있다.System.out.println(job.getCommand()+" : "+job.getTo());}}}반응형
'Java' 카테고리의 다른 글
Java - Thread (0) 2018.01.30 Java - 정규표현식 (0) 2018.01.26 Java - Map (0) 2018.01.25 Java - Collection (0) 2018.01.25 Java - Generic Method (0) 2018.01.24 댓글