首页 技术 正文
技术 2022年11月18日
0 收藏 857 点赞 2,438 浏览 916 个字

一.Queue的实现

通过LinkedList类实现Queue接口来完成对Queue的实例类的实现,代码如下:

Queue<Integer> queue=new LinkedList<>();//linkedList实现了Queue接口,将其向上转型为队列

二.Queue的方法

1.offer————加入元素(加入至队尾)

queue.offer(2);//使用offer将元素插入队尾

2.remove,poll————返回头结点并删除元素

区别remove方法在队列为空时将抛出异常,而poll将返回null

queue.remove();//remove poll 返回头结点并移除 remove在空时抛出异常  但是poll返回null

3.element,peek ————返回头结点,不删除元素

区别element方法在队列为空时将抛出异常,而peek 将返回null

queue.peek();//删除头结点peek/element都是返回队头但是不移除队头,但是peek在没有元素时返回null  element抛出异常

 

三.PriorityQueue队列(优先级队列)的实现

注意点:

在实现了comparator接口的类可以PriorityQueue队列中调用offer方法实现排序

代码如下:

        PriorityQueue<Integer> pque=new PriorityQueue<>();
pque.offer(6);//使用offer添加可以保证有序
pque.offer(0);
pque.offer(23);
pque.offer(1);
System.out.println(pque);//0 1 23 6
pque.add(3);//使用add方法无法排序
System.out.println(pque);//无须

 

(补充)

一.了解collection和iterator的关系

首先只有实现了iterator接口的才能使用foreach遍历(数组除外)。

collection是iterator接口的子接口

public interface Collection<E> extends Iterable<E> {

所以collection所有的子接口可以实现foreach遍历。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,992
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,767
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844