Java中使用LinkedList实现Queue
来源:互联网 发布:单片机激光测距 编辑:程序博客网 时间:2024/06/16 02:45
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<Integer> queue = new LinkedList<Integer>();Random random = new Random(47);for(int i=0; i<10; i++){queue.offer(random.nextInt(i+10));}printQ(queue);Queue<Character> qc = new LinkedList<Character>();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() + " ");}System.out.println();}}/* * Offer()将一个元素插入到队尾 * peek()和element()都是在移除的情况下返回队头 * peek()方法在队列为空时返回null,element()会抛出NoSuchElementException异常 * poll()和remove()方法将移除并返回队头 * poll()在队列为空时返回null,而remove()会抛出NoSuchElementException异常 */
PriorityQueue:优先队列的实现
package cn.usst.queue.demo;import java.util.Arrays;import java.util.Collections;import java.util.List;import java.util.PriorityQueue;import java.util.Queue;import java.util.Random;public class PriorityQueueDemo {public static void main(String[] args) {PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>();Random rand = new Random();for(int i=0; i<10; i++){priorityQueue.offer(rand.nextInt(i+10));}printQ(priorityQueue);List<Integer> ints = Arrays.asList(25, 22, 20, 18, 14, 9, 8, 2, 4, 7);priorityQueue = new PriorityQueue<Integer>(ints);printQ(priorityQueue);//反向输出priorityQueue = new PriorityQueue<Integer>(ints.size(), Collections.reverseOrder());priorityQueue.addAll(ints);printQ(priorityQueue);}private static void printQ(Queue queue) {while(queue.peek() !=null ){System.out.println(queue.remove() + " ");}System.out.println();}}
1 0
- Java中使用LinkedList实现Queue
- 使用LinkedList实现Queue
- JAVA中实现------LinkedList的使用
- java中使用LinkedList实现stack机制
- Linkedlist实现Queue
- java中Queue队列的介绍和使用(1)之LinkedList
- JavaSE第四十三讲:使用LinkedList实现Stack与Queue
- Java中ArrayList和LinkedList以及queue的模仿
- java中LinkedList的实现
- Java中使用LinkedList实现栈与队列
- Java中LinkedList的使用
- Queue和LinkedList的使用
- Java中Queue类实现
- java中queue的使用
- java中queue的使用
- Java中Queue的使用
- java中queue的使用
- java中queue的使用
- YII 显示代码执行时间
- 关于 vector push_back 实现
- 这样学英语三个月超过你过去学三年
- Ubautu 14.04.1 LTS安装Nvidia显卡驱动
- 魅族MX4新版本明天正式开卖 1799元良心依旧
- Java中使用LinkedList实现Queue
- 一元二次求根方程
- mina框架详解
- 线程(五)mutex
- C8_指针
- shell按行读取字符串,并进行分割
- android 安全之——文件加密3DES算法
- Mac上安装Android SDK
- 还是1799元!魅族MX4新版本:帅