Java Queue用法总结

本文介绍了Java中队列的基本概念及其实现方式。队列是一种遵循先进先出原则的数据结构,Java提供了多种操作队列的方法,如插入、删除和检查等。文章详细解释了这些方法及其在异常情况下的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列,一种结构体,遵循先进先出的原则。

java中提供的方法有以下几种:


操作有插入、删除和检查某个下标是否有值三种,其中每种操作包含两个函数,它们的功能是相同的,区别在于insert,remove,element三个函数在操作失败是报异常,而offer,poll,peek三个函数不报异常。

具体描述原句是这样的:

  Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation). 

但是这种说法并不绝对,poll和peek确实不报异常,但是offer函数在某些情况下会报异常,具体如下:

ClassCastException - if the class of the specified element prevents it from being added to this queue NullPointerException - if the specified element is null and this queue does not permit null elements

IllegalArgumentException - if some property of this element prevents it from being added to this queue


但是插入发生异常的情况可能性比较小。

在以后的使用中,要根据应用场景选择合适的函数。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值