ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayDeque.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayDeque.java (file contents):
Revision 1.8 by jsr166, Mon May 2 04:19:58 2005 UTC vs.
Revision 1.24 by dl, Sat Sep 17 13:21:19 2005 UTC

# Line 4 | Line 4
4   */
5  
6   package java.util;
7 + import java.util.*; // for javadoc (till 6280605 is fixed)
8   import java.io.*;
9  
10   /**
# Line 18 | Line 19 | import java.io.*;
19   * <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time.
20   * Exceptions include {@link #remove(Object) remove}, {@link
21   * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence
22 < * removeLastOccurrence}, {@link #contains contains }, {@link #iterator
22 > * removeLastOccurrence}, {@link #contains contains}, {@link #iterator
23   * iterator.remove()}, and the bulk operations, all of which run in linear
24   * time.
25   *
# Line 40 | Line 41 | import java.io.*;
41   * should be used only to detect bugs.</i>
42   *
43   * <p>This class and its iterator implement all of the
44 < * optional methods of the {@link Collection} and {@link
45 < * Iterator} interfaces.  This class is a member of the <a
46 < * href="{@docRoot}/../guide/collections/index.html"> Java Collections
47 < * Framework</a>.
44 > * <em>optional</em> methods of the {@link Collection} and {@link
45 > * Iterator} interfaces.
46 > *
47 > * <p>This class is a member of the
48 > * <a href="{@docRoot}/../guide/collections/index.html">
49 > * Java Collections Framework</a>.
50   *
51   * @author  Josh Bloch and Doug Lea
52   * @since   1.6
# Line 88 | Line 91 | public class ArrayDeque<E> extends Abstr
91      /**
92       * Allocate empty array to hold the given number of elements.
93       *
94 <     * @param numElements  the number of elements to hold.
94 >     * @param numElements  the number of elements to hold
95       */
96      private void allocateElements(int numElements) {
97          int initialCapacity = MIN_INITIAL_CAPACITY;
# Line 187 | Line 190 | public class ArrayDeque<E> extends Abstr
190      /**
191       * Inserts the specified element at the front of this deque.
192       *
193 <     * @param e the element to insert
194 <     * @throws NullPointerException if <tt>e</tt> is null
193 >     * @param e the element to add
194 >     * @throws NullPointerException if the specified element is null
195       */
196      public void addFirst(E e) {
197          if (e == null)
# Line 200 | Line 203 | public class ArrayDeque<E> extends Abstr
203  
204      /**
205       * Inserts the specified element at the end of this deque.
203     * This method is equivalent to {@link Collection#add} and
204     * {@link #push}.
206       *
207 <     * @param e the element to insert
208 <     * @throws NullPointerException if <tt>e</tt> is null
207 >     * <p>This method is equivalent to {@link #add}.
208 >     *
209 >     * @param e the element to add
210 >     * @throws NullPointerException if the specified element is null
211       */
212      public void addLast(E e) {
213          if (e == null)
# Line 215 | Line 218 | public class ArrayDeque<E> extends Abstr
218      }
219  
220      /**
218     * Retrieves and removes the first element of this deque, or
219     * <tt>null</tt> if this deque is empty.
220     *
221     * @return the first element of this deque, or <tt>null</tt> if
222     *     this deque is empty
223     */
224    public E pollFirst() {
225        int h = head;
226        E result = elements[h]; // Element is null if deque empty
227        if (result == null)
228            return null;
229        elements[h] = null;     // Must null out slot
230        head = (h + 1) & (elements.length - 1);
231        return result;
232    }
233
234    /**
235     * Retrieves and removes the last element of this deque, or
236     * <tt>null</tt> if this deque is empty.
237     *
238     * @return the last element of this deque, or <tt>null</tt> if
239     *     this deque is empty
240     */
241    public E pollLast() {
242        int t = (tail - 1) & (elements.length - 1);
243        E result = elements[t];
244        if (result == null)
245            return null;
246        elements[t] = null;
247        tail = t;
248        return result;
249    }
250
251    /**
221       * Inserts the specified element at the front of this deque.
222       *
223 <     * @param e the element to insert
224 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst})
225 <     * @throws NullPointerException if <tt>e</tt> is null
223 >     * @param e the element to add
224 >     * @return <tt>true</tt> (as specified by {@link Deque#offerFirst})
225 >     * @throws NullPointerException if the specified element is null
226       */
227      public boolean offerFirst(E e) {
228          addFirst(e);
# Line 263 | Line 232 | public class ArrayDeque<E> extends Abstr
232      /**
233       * Inserts the specified element at the end of this deque.
234       *
235 <     * @param e the element to insert
236 <     * @return <tt>true</tt> (as per the spec for {@link Deque#offerLast})
237 <     * @throws NullPointerException if <tt>e</tt> is null
235 >     * @param e the element to add
236 >     * @return <tt>true</tt> (as specified by {@link Deque#offerLast})
237 >     * @throws NullPointerException if the specified element is null
238       */
239      public boolean offerLast(E e) {
240          addLast(e);
# Line 273 | Line 242 | public class ArrayDeque<E> extends Abstr
242      }
243  
244      /**
245 <     * Retrieves and removes the first element of this deque.  This method
277 <     * differs from the {@link #pollFirst} method only in that it throws an
278 <     * exception if this deque is empty.
279 <     *
280 <     * @return the first element of this deque
281 <     * @throws NoSuchElementException if this deque is empty
245 >     * @throws NoSuchElementException {@inheritDoc}
246       */
247      public E removeFirst() {
248          E x = pollFirst();
# Line 288 | Line 252 | public class ArrayDeque<E> extends Abstr
252      }
253  
254      /**
255 <     * Retrieves and removes the last element of this deque.  This method
292 <     * differs from the {@link #pollLast} method only in that it throws an
293 <     * exception if this deque is empty.
294 <     *
295 <     * @return the last element of this deque
296 <     * @throws NoSuchElementException if this deque is empty
255 >     * @throws NoSuchElementException {@inheritDoc}
256       */
257      public E removeLast() {
258          E x = pollLast();
# Line 302 | Line 261 | public class ArrayDeque<E> extends Abstr
261          return x;
262      }
263  
264 <    /**
265 <     * Retrieves, but does not remove, the first element of this deque,
266 <     * returning <tt>null</tt> if this deque is empty.
267 <     *
268 <     * @return the first element of this deque, or <tt>null</tt> if
269 <     *     this deque is empty
270 <     */
271 <    public E peekFirst() {
313 <        return elements[head]; // elements[head] is null if deque empty
264 >    public E pollFirst() {
265 >        int h = head;
266 >        E result = elements[h]; // Element is null if deque empty
267 >        if (result == null)
268 >            return null;
269 >        elements[h] = null;     // Must null out slot
270 >        head = (h + 1) & (elements.length - 1);
271 >        return result;
272      }
273  
274 <    /**
275 <     * Retrieves, but does not remove, the last element of this deque,
276 <     * returning <tt>null</tt> if this deque is empty.
277 <     *
278 <     * @return the last element of this deque, or <tt>null</tt> if this deque
279 <     *     is empty
280 <     */
281 <    public E peekLast() {
324 <        return elements[(tail - 1) & (elements.length - 1)];
274 >    public E pollLast() {
275 >        int t = (tail - 1) & (elements.length - 1);
276 >        E result = elements[t];
277 >        if (result == null)
278 >            return null;
279 >        elements[t] = null;
280 >        tail = t;
281 >        return result;
282      }
283  
284      /**
285 <     * Retrieves, but does not remove, the first element of this
329 <     * deque.  This method differs from the {@link #peekFirst} method only
330 <     * in that it throws an exception if this deque is empty.
331 <     *
332 <     * @return the first element of this deque
333 <     * @throws NoSuchElementException if this deque is empty
285 >     * @throws NoSuchElementException {@inheritDoc}
286       */
287      public E getFirst() {
288          E x = elements[head];
# Line 340 | Line 292 | public class ArrayDeque<E> extends Abstr
292      }
293  
294      /**
295 <     * Retrieves, but does not remove, the last element of this
344 <     * deque.  This method differs from the {@link #peekLast} method only
345 <     * in that it throws an exception if this deque is empty.
346 <     *
347 <     * @return the last element of this deque
348 <     * @throws NoSuchElementException if this deque is empty
295 >     * @throws NoSuchElementException {@inheritDoc}
296       */
297      public E getLast() {
298          E x = elements[(tail - 1) & (elements.length - 1)];
# Line 354 | Line 301 | public class ArrayDeque<E> extends Abstr
301          return x;
302      }
303  
304 +    public E peekFirst() {
305 +        return elements[head]; // elements[head] is null if deque empty
306 +    }
307 +
308 +    public E peekLast() {
309 +        return elements[(tail - 1) & (elements.length - 1)];
310 +    }
311 +
312      /**
313       * Removes the first occurrence of the specified element in this
314 <     * deque (when traversing the deque from head to tail).  More
315 <     * formally, removes the first element e such that (o==null ?
316 <     * e==null : o.equals(e)). If the deque does not contain the
317 <     * element, it is unchanged.
314 >     * deque (when traversing the deque from head to tail).
315 >     * If the deque does not contain the element, it is unchanged.
316 >     * More formally, removes the first element <tt>e</tt> such that
317 >     * <tt>o.equals(e)</tt> (if such an element exists).
318 >     * Returns <tt>true</tt> if this deque contained the specified element
319 >     * (or equivalently, if this deque changed as a result of the call).
320       *
321       * @param o element to be removed from this deque, if present
322       * @return <tt>true</tt> if the deque contained the specified element
# Line 382 | Line 339 | public class ArrayDeque<E> extends Abstr
339  
340      /**
341       * Removes the last occurrence of the specified element in this
342 <     * deque (when traversing the deque from head to tail). More
343 <     * formally, removes the last element e such that (o==null ?
344 <     * e==null : o.equals(e)). If the deque
345 <     * does not contain the element, it is unchanged.
342 >     * deque (when traversing the deque from head to tail).
343 >     * If the deque does not contain the element, it is unchanged.
344 >     * More formally, removes the last element <tt>e</tt> such that
345 >     * <tt>o.equals(e)</tt> (if such an element exists).
346 >     * Returns <tt>true</tt> if this deque contained the specified element
347 >     * (or equivalently, if this deque changed as a result of the call).
348       *
349       * @param o element to be removed from this deque, if present
350       * @return <tt>true</tt> if the deque contained the specified element
# Line 411 | Line 370 | public class ArrayDeque<E> extends Abstr
370      /**
371       * Inserts the specified element at the end of this deque.
372       *
414     * <p>This method is equivalent to {@link #offerLast}.
415     *
416     * @param e the element to insert
417     * @return <tt>true</tt> (as per the spec for {@link Queue#offer})
418     * @throws NullPointerException if <tt>e</tt> is null
419     */
420    public boolean offer(E e) {
421        return offerLast(e);
422    }
423
424    /**
425     * Inserts the specified element at the end of this deque.
426     *
373       * <p>This method is equivalent to {@link #addLast}.
374       *
375 <     * @param e the element to insert
376 <     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
377 <     * @throws NullPointerException if <tt>e</tt> is null
375 >     * @param e the element to add
376 >     * @return <tt>true</tt> (as specified by {@link Collection#add})
377 >     * @throws NullPointerException if the specified element is null
378       */
379      public boolean add(E e) {
380          addLast(e);
# Line 436 | Line 382 | public class ArrayDeque<E> extends Abstr
382      }
383  
384      /**
385 <     * Retrieves and removes the head of the queue represented by
440 <     * this deque, or <tt>null</tt> if this deque is empty.  In other words,
441 <     * retrieves and removes the first element of this deque, or <tt>null</tt>
442 <     * if this deque is empty.
385 >     * Inserts the specified element at the end of this deque.
386       *
387 <     * <p>This method is equivalent to {@link #pollFirst}.
387 >     * <p>This method is equivalent to {@link #offerLast}.
388       *
389 <     * @return the first element of this deque, or <tt>null</tt> if
390 <     *     this deque is empty
389 >     * @param e the element to add
390 >     * @return <tt>true</tt> (as specified by {@link Queue#offer})
391 >     * @throws NullPointerException if the specified element is null
392       */
393 <    public E poll() {
394 <        return pollFirst();
393 >    public boolean offer(E e) {
394 >        return offerLast(e);
395      }
396  
397      /**
398       * Retrieves and removes the head of the queue represented by this deque.
399 <     * This method differs from the {@link #poll} method only in that it
400 <     * throws an exception if this deque is empty.
399 >     *
400 >     * This method differs from {@link #poll poll} only in that it throws an
401 >     * exception if this deque is empty.
402       *
403       * <p>This method is equivalent to {@link #removeFirst}.
404       *
405       * @return the head of the queue represented by this deque
406 <     * @throws NoSuchElementException if this deque is empty
406 >     * @throws NoSuchElementException {@inheritDoc}
407       */
408      public E remove() {
409          return removeFirst();
410      }
411  
412      /**
413 <     * Retrieves, but does not remove, the head of the queue represented by
414 <     * this deque, returning <tt>null</tt> if this deque is empty.
413 >     * Retrieves and removes the head of the queue represented by this deque
414 >     * (in other words, the first element of this deque), or returns
415 >     * <tt>null</tt> if this deque is empty.
416       *
417 <     * <p>This method is equivalent to {@link #peekFirst}.
417 >     * <p>This method is equivalent to {@link #pollFirst}.
418       *
419       * @return the head of the queue represented by this deque, or
420 <     *     <tt>null</tt> if this deque is empty
420 >     *         <tt>null</tt> if this deque is empty
421       */
422 <    public E peek() {
423 <        return peekFirst();
422 >    public E poll() {
423 >        return pollFirst();
424      }
425  
426      /**
427       * Retrieves, but does not remove, the head of the queue represented by
428 <     * this deque.  This method differs from the {@link #peek} method only in
428 >     * this deque.  This method differs from {@link #peek peek} only in
429       * that it throws an exception if this deque is empty.
430       *
431       * <p>This method is equivalent to {@link #getFirst}.
432       *
433       * @return the head of the queue represented by this deque
434 <     * @throws NoSuchElementException if this deque is empty
434 >     * @throws NoSuchElementException {@inheritDoc}
435       */
436      public E element() {
437          return getFirst();
438      }
439  
440 +    /**
441 +     * Retrieves, but does not remove, the head of the queue represented by
442 +     * this deque, or returns <tt>null</tt> if this deque is empty.
443 +     *
444 +     * <p>This method is equivalent to {@link #peekFirst}.
445 +     *
446 +     * @return the head of the queue represented by this deque, or
447 +     *         <tt>null</tt> if this deque is empty
448 +     */
449 +    public E peek() {
450 +        return peekFirst();
451 +    }
452 +
453      // *** Stack methods ***
454  
455      /**
# Line 500 | Line 459 | public class ArrayDeque<E> extends Abstr
459       * <p>This method is equivalent to {@link #addFirst}.
460       *
461       * @param e the element to push
462 <     * @throws NullPointerException if <tt>e</tt> is null
462 >     * @throws NullPointerException if the specified element is null
463       */
464      public void push(E e) {
465          addFirst(e);
# Line 513 | Line 472 | public class ArrayDeque<E> extends Abstr
472       * <p>This method is equivalent to {@link #removeFirst()}.
473       *
474       * @return the element at the front of this deque (which is the top
475 <     *     of the stack represented by this deque)
476 <     * @throws NoSuchElementException if this deque is empty
475 >     *         of the stack represented by this deque)
476 >     * @throws NoSuchElementException {@inheritDoc}
477       */
478      public E pop() {
479          return removeFirst();
# Line 522 | Line 481 | public class ArrayDeque<E> extends Abstr
481  
482      /**
483       * Removes the element at the specified position in the elements array,
484 <     * adjusting head, tail, and size as necessary.  This can result in
485 <     * motion of elements backwards or forwards in the array.
484 >     * adjusting head and tail as necessary.  This can result in motion of
485 >     * elements backwards or forwards in the array.
486       *
487       * <p>This method is called delete rather than remove to emphasize
488 <     * that its semantics differ from those of List.remove(int).
488 >     * that its semantics differ from those of {@link List#remove(int)}.
489       *
490       * @return true if elements moved backwards
491       */
492      private boolean delete(int i) {
493 <        // Case 1: Deque doesn't wrap
494 <        // Case 2: Deque does wrap and removed element is in the head portion
495 <        if ((head < tail || tail == 0) || i >= head) {
496 <            System.arraycopy(elements, head, elements, head + 1, i - head);
497 <            elements[head] = null;
498 <            head = (head + 1) & (elements.length - 1);
499 <            return false;
500 <        }
501 <
502 <        // Case 3: Deque wraps and removed element is in the tail portion
503 <        tail--;
504 <        System.arraycopy(elements, i + 1, elements, i, tail - i);
505 <        elements[tail] = null;
506 <        return true;
493 >        final E[] elements = this.elements;
494 >        final int mask = elements.length - 1;
495 >        final int h = head;
496 >        final int t = tail;
497 >        final int front = (i - h) & mask;
498 >        final int back  = (t - i) & mask;
499 >
500 >        // Invariant: head <= i < tail mod circularity
501 >        if (front >= ((t - h) & mask))
502 >            throw new ConcurrentModificationException();
503 >
504 >        // Optimize for least element motion
505 >        if (front < back) {
506 >            if (h <= i) {
507 >                System.arraycopy(elements, h, elements, h + 1, front);
508 >            } else { // Wrap around
509 >                System.arraycopy(elements, 0, elements, 1, i);
510 >                elements[0] = elements[mask];
511 >                System.arraycopy(elements, h, elements, h + 1, mask - h);
512 >            }
513 >            elements[h] = null;
514 >            head = (h + 1) & mask;
515 >            return false;
516 >        } else {
517 >            if (i < t) { // Copy the null tail as well
518 >                System.arraycopy(elements, i + 1, elements, i, back);
519 >                tail = t - 1;
520 >            } else { // Wrap around
521 >                System.arraycopy(elements, i + 1, elements, i, mask - i);
522 >                elements[mask] = elements[0];
523 >                System.arraycopy(elements, 1, elements, 0, t);
524 >                tail = (t - 1) & mask;
525 >            }
526 >            return true;
527 >        }
528      }
529  
530      // *** Collection Methods ***
# Line 559 | Line 539 | public class ArrayDeque<E> extends Abstr
539      }
540  
541      /**
542 <     * Returns <tt>true</tt> if this deque contains no elements.<p>
542 >     * Returns <tt>true</tt> if this deque contains no elements.
543       *
544 <     * @return <tt>true</tt> if this deque contains no elements.
544 >     * @return <tt>true</tt> if this deque contains no elements
545       */
546      public boolean isEmpty() {
547          return head == tail;
# Line 573 | Line 553 | public class ArrayDeque<E> extends Abstr
553       * order that elements would be dequeued (via successive calls to
554       * {@link #remove} or popped (via successive calls to {@link #pop}).
555       *
556 <     * @return an <tt>Iterator</tt> over the elements in this deque
556 >     * @return an iterator over the elements in this deque
557       */
558      public Iterator<E> iterator() {
559          return new DeqIterator();
560      }
561  
562 +    public Iterator<E> descendingIterator() {
563 +        return new DescendingIterator();
564 +    }
565 +
566      private class DeqIterator implements Iterator<E> {
567          /**
568           * Index of element to be returned by subsequent call to next.
# Line 617 | Line 601 | public class ArrayDeque<E> extends Abstr
601          public void remove() {
602              if (lastRet < 0)
603                  throw new IllegalStateException();
604 <            if (delete(lastRet))
605 <                cursor--;
604 >            if (delete(lastRet)) // if left-shifted, undo increment in next()
605 >                cursor = (cursor - 1) & (elements.length - 1);
606              lastRet = -1;
607              fence = tail;
608          }
609      }
610  
611 +
612 +    private class DescendingIterator implements Iterator<E> {
613 +        /*
614 +         * This class is nearly a mirror-image of DeqIterator, using
615 +         * (tail-1) instead of head for initial cursor, (head-1)
616 +         * instead of tail for fence, and elements.length instead of -1
617 +         * for sentinel. It shares the same structure, but not many
618 +         * actual lines of code.
619 +         */
620 +        private int cursor = (tail - 1) & (elements.length - 1);
621 +        private int fence =  (head - 1) & (elements.length - 1);
622 +        private int lastRet = elements.length;
623 +
624 +        public boolean hasNext() {
625 +            return cursor != fence;
626 +        }
627 +
628 +        public E next() {
629 +            E result;
630 +            if (cursor == fence)
631 +                throw new NoSuchElementException();
632 +            if (((head - 1) & (elements.length - 1)) != fence ||
633 +                (result = elements[cursor]) == null)
634 +                throw new ConcurrentModificationException();
635 +            lastRet = cursor;
636 +            cursor = (cursor - 1) & (elements.length - 1);
637 +            return result;
638 +        }
639 +
640 +        public void remove() {
641 +            if (lastRet >= elements.length)
642 +                throw new IllegalStateException();
643 +            if (!delete(lastRet))
644 +                cursor = (cursor + 1) & (elements.length - 1);
645 +            lastRet = elements.length;
646 +            fence = (head - 1) & (elements.length - 1);
647 +        }
648 +    }
649 +
650      /**
651 <     * Returns <tt>true</tt> if this deque contains the specified
652 <     * element.  More formally, returns <tt>true</tt> if and only if this
653 <     * deque contains at least one element <tt>e</tt> such that
631 <     * <tt>e.equals(o)</tt>.
651 >     * Returns <tt>true</tt> if this deque contains the specified element.
652 >     * More formally, returns <tt>true</tt> if and only if this deque contains
653 >     * at least one element <tt>e</tt> such that <tt>o.equals(e)</tt>.
654       *
655       * @param o object to be checked for containment in this deque
656       * @return <tt>true</tt> if this deque contains the specified element
# Line 649 | Line 671 | public class ArrayDeque<E> extends Abstr
671  
672      /**
673       * Removes a single instance of the specified element from this deque.
674 <     * This method is equivalent to {@link #removeFirstOccurrence}.
674 >     * If the deque does not contain the element, it is unchanged.
675 >     * More formally, removes the first element <tt>e</tt> such that
676 >     * <tt>o.equals(e)</tt> (if such an element exists).
677 >     * Returns <tt>true</tt> if this deque contained the specified element
678 >     * (or equivalently, if this deque changed as a result of the call).
679       *
680 <     * @param e element to be removed from this deque, if present
680 >     * <p>This method is equivalent to {@link #removeFirstOccurrence}.
681 >     *
682 >     * @param o element to be removed from this deque, if present
683       * @return <tt>true</tt> if this deque contained the specified element
684       */
685 <    public boolean remove(Object e) {
686 <        return removeFirstOccurrence(e);
685 >    public boolean remove(Object o) {
686 >        return removeFirstOccurrence(o);
687      }
688  
689      /**
# Line 672 | Line 700 | public class ArrayDeque<E> extends Abstr
700              do {
701                  elements[i] = null;
702                  i = (i + 1) & mask;
703 <            } while(i != t);
703 >            } while (i != t);
704          }
705      }
706  
707      /**
708       * Returns an array containing all of the elements in this deque
709 <     * in the correct order.
709 >     * in proper sequence (from first to last element).
710 >     *
711 >     * <p>The returned array will be "safe" in that no references to it are
712 >     * maintained by this deque.  (In other words, this method must allocate
713 >     * a new array).  The caller is thus free to modify the returned array.
714 >     *
715 >     * <p>This method acts as bridge between array-based and collection-based
716 >     * APIs.
717       *
718       * @return an array containing all of the elements in this deque
684     *         in the correct order
719       */
720      public Object[] toArray() {
721          return copyElements(new Object[size()]);
722      }
723  
724      /**
725 <     * Returns an array containing all of the elements in this deque in the
726 <     * correct order; the runtime type of the returned array is that of the
727 <     * specified array.  If the deque fits in the specified array, it is
728 <     * returned therein.  Otherwise, a new array is allocated with the runtime
729 <     * type of the specified array and the size of this deque.
730 <     *
731 <     * <p>If the deque fits in the specified array with room to spare (i.e.,
732 <     * the array has more elements than the deque), the element in the array
733 <     * immediately following the end of the collection is set to <tt>null</tt>.
725 >     * Returns an array containing all of the elements in this deque in
726 >     * proper sequence (from first to last element); the runtime type of the
727 >     * returned array is that of the specified array.  If the deque fits in
728 >     * the specified array, it is returned therein.  Otherwise, a new array
729 >     * is allocated with the runtime type of the specified array and the
730 >     * size of this deque.
731 >     *
732 >     * <p>If this deque fits in the specified array with room to spare
733 >     * (i.e., the array has more elements than this deque), the element in
734 >     * the array immediately following the end of the deque is set to
735 >     * <tt>null</tt>.
736 >     *
737 >     * <p>Like the {@link #toArray()} method, this method acts as bridge between
738 >     * array-based and collection-based APIs.  Further, this method allows
739 >     * precise control over the runtime type of the output array, and may,
740 >     * under certain circumstances, be used to save allocation costs.
741 >     *
742 >     * <p>Suppose <tt>x</tt> is a deque known to contain only strings.
743 >     * The following code can be used to dump the deque into a newly
744 >     * allocated array of <tt>String</tt>:
745 >     *
746 >     * <pre>
747 >     *     String[] y = x.toArray(new String[0]);</pre>
748 >     *
749 >     * Note that <tt>toArray(new Object[0])</tt> is identical in function to
750 >     * <tt>toArray()</tt>.
751       *
752       * @param a the array into which the elements of the deque are to
753 <     *          be stored, if it is big enough; otherwise, a new array of the
754 <     *          same runtime type is allocated for this purpose
755 <     * @return an array containing the elements of the deque
756 <     * @throws ArrayStoreException if the runtime type of a is not a supertype
757 <     *         of the runtime type of every element in this deque
753 >     *          be stored, if it is big enough; otherwise, a new array of the
754 >     *          same runtime type is allocated for this purpose
755 >     * @return an array containing all of the elements in this deque
756 >     * @throws ArrayStoreException if the runtime type of the specified array
757 >     *         is not a supertype of the runtime type of every element in
758 >     *         this deque
759 >     * @throws NullPointerException if the specified array is null
760       */
761      public <T> T[] toArray(T[] a) {
762          int size = size();
# Line 752 | Line 805 | public class ArrayDeque<E> extends Abstr
805          s.defaultWriteObject();
806  
807          // Write out size
808 <        int size = size();
756 <        s.writeInt(size);
808 >        s.writeInt(size());
809  
810          // Write out elements in order.
759        int i = head;
811          int mask = elements.length - 1;
812 <        for (int j = 0; j < size; j++) {
812 >        for (int i = head; i != tail; i = (i + 1) & mask)
813              s.writeObject(elements[i]);
763            i = (i + 1) & mask;
764        }
814      }
815  
816      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines