4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
+ |
|
8 |
|
import java.io.Serializable; |
9 |
|
import java.util.function.Consumer; |
10 |
|
import java.util.stream.Stream; |
54 |
|
* |
55 |
|
* @author Josh Bloch and Doug Lea |
56 |
|
* @since 1.6 |
57 |
< |
* @param <E> the type of elements held in this collection |
57 |
> |
* @param <E> the type of elements held in this deque |
58 |
|
*/ |
59 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
60 |
|
implements Deque<E>, Cloneable, Serializable |
316 |
|
* @return {@code true} if the deque contained the specified element |
317 |
|
*/ |
318 |
|
public boolean removeFirstOccurrence(Object o) { |
319 |
< |
if (o == null) |
320 |
< |
return false; |
321 |
< |
int mask = elements.length - 1; |
322 |
< |
int i = head; |
323 |
< |
Object x; |
324 |
< |
while ( (x = elements[i]) != null) { |
325 |
< |
if (o.equals(x)) { |
326 |
< |
delete(i); |
326 |
< |
return true; |
319 |
> |
if (o != null) { |
320 |
> |
int mask = elements.length - 1; |
321 |
> |
int i = head; |
322 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
323 |
> |
if (o.equals(x)) { |
324 |
> |
delete(i); |
325 |
> |
return true; |
326 |
> |
} |
327 |
|
} |
328 |
– |
i = (i + 1) & mask; |
328 |
|
} |
329 |
|
return false; |
330 |
|
} |
342 |
|
* @return {@code true} if the deque contained the specified element |
343 |
|
*/ |
344 |
|
public boolean removeLastOccurrence(Object o) { |
345 |
< |
if (o == null) |
346 |
< |
return false; |
347 |
< |
int mask = elements.length - 1; |
348 |
< |
int i = (tail - 1) & mask; |
349 |
< |
Object x; |
350 |
< |
while ( (x = elements[i]) != null) { |
351 |
< |
if (o.equals(x)) { |
352 |
< |
delete(i); |
354 |
< |
return true; |
345 |
> |
if (o != null) { |
346 |
> |
int mask = elements.length - 1; |
347 |
> |
int i = (tail - 1) & mask; |
348 |
> |
for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { |
349 |
> |
if (o.equals(x)) { |
350 |
> |
delete(i); |
351 |
> |
return true; |
352 |
> |
} |
353 |
|
} |
356 |
– |
i = (i - 1) & mask; |
354 |
|
} |
355 |
|
return false; |
356 |
|
} |
655 |
|
* @return {@code true} if this deque contains the specified element |
656 |
|
*/ |
657 |
|
public boolean contains(Object o) { |
658 |
< |
if (o == null) |
659 |
< |
return false; |
660 |
< |
int mask = elements.length - 1; |
661 |
< |
int i = head; |
662 |
< |
Object x; |
663 |
< |
while ( (x = elements[i]) != null) { |
664 |
< |
if (o.equals(x)) |
668 |
< |
return true; |
669 |
< |
i = (i + 1) & mask; |
658 |
> |
if (o != null) { |
659 |
> |
int mask = elements.length - 1; |
660 |
> |
int i = head; |
661 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
662 |
> |
if (o.equals(x)) |
663 |
> |
return true; |
664 |
> |
} |
665 |
|
} |
666 |
|
return false; |
667 |
|
} |
805 |
|
/** |
806 |
|
* Saves this deque to a stream (that is, serializes it). |
807 |
|
* |
808 |
+ |
* @param s the stream |
809 |
+ |
* @throws java.io.IOException if an I/O error occurs |
810 |
|
* @serialData The current size ({@code int}) of the deque, |
811 |
|
* followed by all of its elements (each an object reference) in |
812 |
|
* first-to-last order. |
826 |
|
|
827 |
|
/** |
828 |
|
* Reconstitutes this deque from a stream (that is, deserializes it). |
829 |
+ |
* @param s the stream |
830 |
+ |
* @throws ClassNotFoundException if the class of a serialized object |
831 |
+ |
* could not be found |
832 |
+ |
* @throws java.io.IOException if an I/O error occurs |
833 |
|
*/ |
834 |
|
private void readObject(java.io.ObjectInputStream s) |
835 |
|
throws java.io.IOException, ClassNotFoundException { |