栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java | 队列 add() 和 offer() 方法的区别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java | 队列 add() 和 offer() 方法的区别

同:二者都是向队列中添加元素
异:

总结:当队列容量超过限制时,add()会报异常,offer()会返回false

1. add()

API:

Inserts the specified element into this queue if it is possible to do soimmediately 
without violating capacity restrictions, returning true upon success and throwing an 
IllegalStateException if no space is currently available.
  • capacity restrictions: 容量限制
  • if no space is currently available: 当前没有可用空间

Return:

true (as specified by Collection.add)

Throws: IllegalStateException - if the element cannot be added at thistime due to capacity restrictions

2. offer()

API:

Inserts the specified element into this queue if it is possible to doso immediately 
without violating capacity restrictions.When using a capacity-restricted queue, this
method is generallypreferable to add, which can fail to insert an element onlyby throwing an exception.
  • capacity-restricted queue: 容量受限的队列

Return:

true if the element was added to this queue, else false
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821861.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号