QueueDemo
程序员文章站
2022-03-14 11:21:37
...
package com.zhoubo.concurrent.collection;
import java.util.LinkedList;
import java.util.Queue;
public class QueueDemo {
Queue<String> queue = new LinkedList<String>();
public QueueDemo() {
queue.offer("1");//将制定的元素插入此队列,插入失败则返回false。而不像collection.add()抛出异常。
queue.offer("2");
queue.offer("3");
}
public static void main(String args[]){
QueueDemo demo = new QueueDemo();
System.out.println(demo.queue.peek());//检索但是不移除此队列的头,如果队列为空,则返回null。
System.out.println(demo.queue.element());//检索但是不移除此队列的头,如果队列为空,则抛出异常java.util.NoSuchElementException。
System.out.println(demo.queue.poll());//检索并移除此队列的头,如果队列为空,则返回null
System.out.println(demo.queue.poll());
System.out.println(demo.queue.poll());
System.out.println(demo.queue.remove());////检索并移除此队列的头,如果队列为空,则抛出异常java.util.NoSuchElementException
}
}
上一篇: 队列实现系列(一)——队列的数组实现(Java版)
下一篇: php怎么将图片转成二进制?
推荐阅读