4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
+ |
|
8 |
|
import java.io.Serializable; |
9 |
|
import java.util.function.Consumer; |
9 |
– |
import java.util.stream.Stream; |
10 |
|
|
11 |
|
/** |
12 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
53 |
|
* |
54 |
|
* @author Josh Bloch and Doug Lea |
55 |
|
* @since 1.6 |
56 |
< |
* @param <E> the type of elements held in this collection |
56 |
> |
* @param <E> the type of elements held in this deque |
57 |
|
*/ |
58 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
59 |
|
implements Deque<E>, Cloneable, Serializable |
251 |
|
@SuppressWarnings("unchecked") |
252 |
|
E result = (E) elements[h]; |
253 |
|
// Element is null if deque empty |
254 |
< |
if (result == null) |
255 |
< |
return null; |
256 |
< |
elements[h] = null; // Must null out slot |
257 |
< |
head = (h + 1) & (elements.length - 1); |
254 |
> |
if (result != null) { |
255 |
> |
elements[h] = null; // Must null out slot |
256 |
> |
head = (h + 1) & (elements.length - 1); |
257 |
> |
} |
258 |
|
return result; |
259 |
|
} |
260 |
|
|
262 |
|
int t = (tail - 1) & (elements.length - 1); |
263 |
|
@SuppressWarnings("unchecked") |
264 |
|
E result = (E) elements[t]; |
265 |
< |
if (result == null) |
266 |
< |
return null; |
267 |
< |
elements[t] = null; |
268 |
< |
tail = t; |
265 |
> |
if (result != null) { |
266 |
> |
elements[t] = null; |
267 |
> |
tail = t; |
268 |
> |
} |
269 |
|
return result; |
270 |
|
} |
271 |
|
|
318 |
|
if (o != null) { |
319 |
|
int mask = elements.length - 1; |
320 |
|
int i = head; |
321 |
< |
Object x; |
322 |
< |
while ( (x = elements[i]) != null) { |
321 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
322 |
|
if (o.equals(x)) { |
323 |
|
delete(i); |
324 |
|
return true; |
325 |
|
} |
327 |
– |
i = (i + 1) & mask; |
326 |
|
} |
327 |
|
} |
328 |
|
return false; |
341 |
|
* @return {@code true} if the deque contained the specified element |
342 |
|
*/ |
343 |
|
public boolean removeLastOccurrence(Object o) { |
344 |
< |
if (o == null) |
345 |
< |
return false; |
346 |
< |
int mask = elements.length - 1; |
347 |
< |
int i = (tail - 1) & mask; |
348 |
< |
Object x; |
349 |
< |
while ( (x = elements[i]) != null) { |
350 |
< |
if (o.equals(x)) { |
351 |
< |
delete(i); |
354 |
< |
return true; |
344 |
> |
if (o != null) { |
345 |
> |
int mask = elements.length - 1; |
346 |
> |
int i = (tail - 1) & mask; |
347 |
> |
for (Object x; (x = elements[i]) != null; i = (i - 1) & mask) { |
348 |
> |
if (o.equals(x)) { |
349 |
> |
delete(i); |
350 |
> |
return true; |
351 |
> |
} |
352 |
|
} |
356 |
– |
i = (i - 1) & mask; |
353 |
|
} |
354 |
|
return false; |
355 |
|
} |
657 |
|
if (o != null) { |
658 |
|
int mask = elements.length - 1; |
659 |
|
int i = head; |
660 |
< |
Object x; |
665 |
< |
while ( (x = elements[i]) != null) { |
660 |
> |
for (Object x; (x = elements[i]) != null; i = (i + 1) & mask) { |
661 |
|
if (o.equals(x)) |
662 |
|
return true; |
668 |
– |
i = (i + 1) & mask; |
663 |
|
} |
664 |
|
} |
665 |
|
return false; |
746 |
|
* The following code can be used to dump the deque into a newly |
747 |
|
* allocated array of {@code String}: |
748 |
|
* |
749 |
< |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
749 |
> |
* <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
750 |
|
* |
751 |
|
* Note that {@code toArray(new Object[0])} is identical in function to |
752 |
|
* {@code toArray()}. |
901 |
|
throw new NullPointerException(); |
902 |
|
Object[] a = deq.elements; |
903 |
|
int m = a.length - 1, f = getFence(), i = index; |
904 |
< |
if (i != fence) { |
904 |
> |
if (i != f) { |
905 |
|
@SuppressWarnings("unchecked") E e = (E)a[i]; |
906 |
|
index = (i + 1) & m; |
907 |
|
if (e == null) |