/** * Removes the first occurrence of the specified element in this deque (when * traversing the deque from head to tail). If the deque does not contain * the element, it is unchanged. More formally, removes the first element * <tt>e</tt> such that <tt>o.equals(e)</tt> (if such an element exists). * Returns <tt>true</tt> if this deque contained the specified element (or * equivalently, if this deque changed as a result of the call). * * @param o * element to be removed from this deque, if present * @return <tt>true</tt> if the deque contained the specified element */ public boolean removeFirstOccurrence(Object o) { if (o == null) return false; int mask = elements.length - 1; int i = head; E x; while ((x = elements[i]) != null) { if (o.equals(x)) { delete(i); return true; } i = (i + 1) & mask; } return false; }
/** * Removes the last occurrence of the specified element in this deque (when * traversing the deque from head to tail). If the deque does not contain * the element, it is unchanged. More formally, removes the last element * <tt>e</tt> such that <tt>o.equals(e)</tt> (if such an element exists). * Returns <tt>true</tt> if this deque contained the specified element (or * equivalently, if this deque changed as a result of the call). * * @param o * element to be removed from this deque, if present * @return <tt>true</tt> if the deque contained the specified element */ public boolean removeLastOccurrence(Object o) { if (o == null) return false; int mask = elements.length - 1; int i = (tail - 1) & mask; E x; while ((x = elements[i]) != null) { if (o.equals(x)) { delete(i); return true; } i = (i - 1) & mask; } return false; }