查看了下源代码,发现add和offer其实实现的方法貌似是一样的,那么,为什么还要设置两个不同的名称?    它们之间的区别是什么?     新手,求解!

解决方案 »

  1.   

    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>都实现大概是为了 反射时使用的
      

  2.   

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

  3.   

    不同接口的不同方法
    add当属于List
    offer当属于queue吧
      

  4.   

    不同接口的不同方法
    add当属于List
      

  5.   

    这个要看应用场景
    作为List使用时,一般采用add / get方法来 压入/获取对象
    作为Queue使用时,才会采用 offer/poll/take等方法作为链表对象时,offer等方法相对来说没有什么意义
    这些方法是用于支持队列应用的
      

  6.   

    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中去通地这个就可以看出区别了,哪个没有任何限制,哪个有限制