forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueDemo.java
More file actions
30 lines (24 loc) · 846 Bytes
/
QueueDemo.java
File metadata and controls
30 lines (24 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package onlyfun.caterpillar;
import java.util.*;
public class QueueDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Queue<String> queue = new LinkedList<String>();
System.out.println("輸入名稱(使用quit結束)");
while(true) {
System.out.print("# ");
String input = scanner.next();
if(input.equals("quit"))
break;
// offer():加入元素至佇列中
queue.offer(input);
}
System.out.print("顯示輸入: ");
String element = null;
// poll():取得並移去佇列中的元素
while((element = queue.poll()) != null) {
System.out.print(element + " ");
}
System.out.println();
}
}