Differnce between addfirst and offerFirst methods in ArrayDeque

后端 未结 2 1235
悲&欢浪女
悲&欢浪女 2021-01-04 19:23

Have tried out a sample program to understand the difference between addFirst and offerFirst methods in ArrayDeque of Java 6. But they

2条回答
  •  爱一瞬间的悲伤
    2021-01-04 20:12

    the source code of offerFirst :

     public boolean offerFirst(E e) {
            addFirst(e);
            return true;
        }
    

    And addFirst

     public void addFirst(E e) {
            if (e == null)
                throw new NullPointerException();
            elements[head = (head - 1) & (elements.length - 1)] = e;
            if (head == tail)
                doubleCapacity();
        }
    

    offerFirst returns true, thats the only difference ...

提交回复
热议问题