黑马程序员技术交流社区

标题: 关于集合LinkedList中的方法问题 [打印本页]

作者: 邓超军    时间: 2012-7-20 14:13
标题: 关于集合LinkedList中的方法问题
本帖最后由 邓超军 于 2012-7-20 14:18 编辑

我们从API中看到,“public boolean add(E e) 将指定元素插入此双端队列的末尾。”,“public boolean offer(E e) 将指定元素插入此双端队列的末尾。 ”。那么,add方法与offer方法有什么不同呢?
同理,remove和poll方法又有什么区别呢?
作者: 黄丽慧    时间: 2012-7-20 14:23
本帖最后由 黄丽慧 于 2012-7-20 14:27 编辑

add 方法和 offer 方法 的区别是:
当队列的容量固定(有界)时, 使用offer 方法,如果插入不成功则返回 false值。而使用add 方法可能无法插入元素,只是抛出一个异常。
而remove() 和poll() 不同的地方是:
当队列为空时:remove() 方法和add的方法一样都会抛出一个异常,而 poll() 方法则返回 null值。

作者: 王超    时间: 2012-7-20 15:07
add是list的;offer是queue的
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中去

java5中新增加了java.util.Queue接口,用以支持队列的常见操作。该接口扩展了java.util.Collection接口。
Queue使用时要尽量避免Collection的add()和remove()方法,而是要使用offer()来加入元素,使用poll()来获取并移出元素。它们的优
点是通过返回值可以判断成功与否,add()和remove()方法在失败的时候会抛出异常。 如果要使用前端而不移出该元素,使用
element()或者peek()方法。值得注意的是LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。


作者: rslheima    时间: 2012-7-20 15:46
以下都是LinkedList中的源码   
public boolean add(E e) {
        addBefore(e, header);
        return true;
    }
        
            public boolean offer(E e) {
              return add(e);
            }
从源码中可以看到
其实add和offer没什么区别,offer底层也是调用的add方法
   --------------------------------
  public E remove() {
        return removeFirst();
    }
                            public E removeFirst() {
                                return remove(header.next);
                                }
                              
                                                                public E next() {
                                                                        checkForComodification();
                                                                        if (nextIndex == size)
                                                                        throw new NoSuchElementException();
                                                                        lastReturned = next;
                                                                        next = next.next;
                                                                        nextIndex++;
                                                                        return lastReturned.element;
                                                                }
                                                        
        
     public E poll() {
        if (size==0)
            return null;
        return removeFirst();
    }
有源码看出:
当链表为空时,poll返回null,remove会抛出异常














作者: 石好强    时间: 2012-7-20 15:53
看了本篇回复,我惊了。高手这么多,新特性,新接口,源码都出来了!就是一个区别么,都让你们说了,我还说啥...
赶快回去啃笔记去了...
作者: 陈雷    时间: 2012-7-20 15:54
add 方法在插入失败的时候会抛出 IllegalStateException 异常
而offer可以通过返回值来判断成功与否
作者: 张凯    时间: 2012-7-20 16:25
add方法与offer方法的不同之处是:
你看:
boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. When using a capacity-restricted queue, this method is generally preferable to add(E), which can fail to insert an element only by throwing an exception.

它们的区别就是add 方法在插入失败的时候会抛出 IllegalStateException 异常
而offer可以通过返回值来判断成功与否。
同理,remove方法在移除失败的时候会抛出 IllegalStateException 异常
而poll方法可以通过返回值来判断成功与否。
希望可以帮到你。





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2