LinkedList 中add 和 offer 的区别?

wzm812505448 2012-06-07 05:06:52
查看了下源代码,发现add和offer其实实现的方法貌似是一样的,那么,为什么还要设置两个不同的名称?

它们之间的区别是什么?

新手,求解!
...全文
6404 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jlu_lamp_lamp 2012-06-08
  • 打赏
  • 举报
回复
add是list的
offer是queue的
api里说:
add:Inserts the specified element at the specified position in this list
将指定的元素插入到list中指定的的位置。
offer:Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
如果在不违反容量限制的情况下,尽可能快的将指定的元素插入到queue中去

通地这个就可以看出区别了,哪个没有任何限制,哪个有限制
beiouwolf 2012-06-08
  • 打赏
  • 举报
回复
这个要看应用场景
作为List使用时,一般采用add / get方法来 压入/获取对象
作为Queue使用时,才会采用 offer/poll/take等方法

作为链表对象时,offer等方法相对来说没有什么意义
这些方法是用于支持队列应用的
xlhb 2012-06-07
  • 打赏
  • 举报
回复
不同接口的不同方法
add当属于List
龙四 2012-06-07
  • 打赏
  • 举报
回复
不同接口的不同方法
add当属于List
offer当属于queue吧
wzm812505448 2012-06-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

当队列为空时候,使用add方法会报错,而offer方法会返回false。
[/Quote]

这个我知道,难道就这么点分别
ZZZ5512536 2012-06-07
  • 打赏
  • 举报
回复
当队列为空时候,使用add方法会报错,而offer方法会返回false。
hybeureka 2012-06-07
  • 打赏
  • 举报
回复
offer(E e)
Adds the specified element as the tail (last element) of this list.

LikedList 通过继承,实现了接口

offer属于 offer in interface Deque<E>

add 属于 add in interface Collection<E>

都实现大概是为了 反射时使用的

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧