LinkedList提供了方法支持队列的行为,并且它实现了Queue接口,因此LinkedList可以用作Queue的一种实现。
package cn.usst.queue.demo;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
/*
* Queue的基本使用
*/
public class QueueDemo {
public static void main(String[] args) {
Queue queue = new LinkedList();
Random random = new Random(47);
for(int i=0; i<10; i++){
queue.offer(random.nextInt(i+10));
}
printQ(queue);
Queue qc = new LinkedList();
for(char c : "Brontosaurus".toCharArray()){
qc.offer(c);
}
printQ(qc);
}
private static void printQ(Queue queue) {
while(queue.peek() !=null ){
System.out.println(queue.remove() + " ");
}