java - Difference between add(E e) and offer(E e) of ArrayDqueue Class -
hi used add , offer add element in last pace. both returning boolean , both not throw exception apart npe.
public class arraydequedemo { public static void main(string[] args) { // create arraydeque elements. arraydeque<integer> deque = new arraydeque<>(); deque.add(10); deque.offer(30); } }
both add element in last place returning boolean.
java implementation
//for add , offer both public void addlast(e e) { if (e == null) throw new nullpointerexception(); elements[tail] = e; if ( (tail = (tail + 1) & (elements.length - 1)) == head) doublecapacity(); }
the 2 methods equivalent.
the reason both exist the java.util.queue
interface specifies both.
the reason java.util.queue
specifies both implementation of java.util.queue
allowed implement capacity restrictions, , 2 methods specified behave differently in case adding element violate restriction; specifically, add(...)
specified throw illegalstateexception
in case, whereas offer(...)
returns false
.
java.util.arraydequeue
, however, not implement capacity restrictions, situation not arise it, distinction not apply.
Comments
Post a Comment