ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/PriorityQueue.java
Revision: 1.43
Committed: Sun Oct 5 22:59:21 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.42: +1 -1 lines
Log Message:
addAll(self) throws exception; javadoc touchups

File Contents

# User Rev Content
1 dl 1.38 /*
2     * %W% %E%
3     *
4     * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
5     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6     */
7    
8     package java.util;
9 tim 1.1
10     /**
11 dl 1.41 * An unbounded priority {@linkplain Queue queue} based on a priority
12     * heap. This queue orders elements according to an order specified
13     * at construction time, which is specified either according to their
14     * <i>natural order</i> (see {@link Comparable}), or according to a
15     * {@link java.util.Comparator}, depending on which constructor is
16     * used. A priority queue does not permit <tt>null</tt> elements.
17 dl 1.42 * A priority queue relying on natural ordering also does not
18 dl 1.43 * permit insertion of non-comparable objects (doing so may result
19 dl 1.42 * in <tt>ClassCastException</tt>).
20 dl 1.40 *
21 dl 1.41 * <p>The <em>head</em> of this queue is the <em>least</em> element
22     * with respect to the specified ordering. If multiple elements are
23     * tied for least value, the head is one of those elements -- ties are
24 dl 1.42 * broken arbitrarily. The queue retrieval operations <tt>poll</tt>,
25     * <tt>remove</tt>, <tt>peek</tt>, and <tt>element</tt> access the
26     * element at the head of the queue.
27 tim 1.14 *
28 dl 1.41 * <p>A priority queue is unbounded, but has an internal
29     * <i>capacity</i> governing the size of an array used to store the
30 dl 1.40 * elements on the queue. It is always at least as large as the queue
31     * size. As elements are added to a priority queue, its capacity
32     * grows automatically. The details of the growth policy are not
33     * specified.
34 tim 1.2 *
35 dl 1.41 * <p>This class implements all of the <em>optional</em> methods of
36     * the {@link Collection} and {@link Iterator} interfaces. The
37     * Iterator provided in method {@link #iterator()} is <em>not</em>
38 dl 1.29 * guaranteed to traverse the elements of the PriorityQueue in any
39     * particular order. If you need ordered traversal, consider using
40     * <tt>Arrays.sort(pq.toArray())</tt>.
41     *
42     * <p> <strong>Note that this implementation is not synchronized.</strong>
43     * Multiple threads should not access a <tt>PriorityQueue</tt>
44     * instance concurrently if any of the threads modifies the list
45     * structurally. Instead, use the thread-safe {@link
46 dholmes 1.34 * java.util.concurrent.PriorityBlockingQueue} class.
47 dl 1.29 *
48     *
49 dholmes 1.11 * <p>Implementation note: this implementation provides O(log(n)) time
50     * for the insertion methods (<tt>offer</tt>, <tt>poll</tt>,
51     * <tt>remove()</tt> and <tt>add</tt>) methods; linear time for the
52     * <tt>remove(Object)</tt> and <tt>contains(Object)</tt> methods; and
53     * constant time for the retrieval methods (<tt>peek</tt>,
54     * <tt>element</tt>, and <tt>size</tt>).
55 tim 1.2 *
56     * <p>This class is a member of the
57     * <a href="{@docRoot}/../guide/collections/index.html">
58     * Java Collections Framework</a>.
59 dl 1.7 * @since 1.5
60 dl 1.38 * @version %I%, %G%
61 dl 1.7 * @author Josh Bloch
62 tim 1.2 */
63     public class PriorityQueue<E> extends AbstractQueue<E>
64 dl 1.22 implements Queue<E>, java.io.Serializable {
65 dholmes 1.11
66 dl 1.31 private static final long serialVersionUID = -7720805057305804111L;
67 dl 1.30
68 tim 1.2 private static final int DEFAULT_INITIAL_CAPACITY = 11;
69 tim 1.1
70 tim 1.2 /**
71     * Priority queue represented as a balanced binary heap: the two children
72     * of queue[n] are queue[2*n] and queue[2*n + 1]. The priority queue is
73     * ordered by comparator, or by the elements' natural ordering, if
74 brian 1.6 * comparator is null: For each node n in the heap and each descendant d
75     * of n, n <= d.
76 tim 1.2 *
77 brian 1.6 * The element with the lowest value is in queue[1], assuming the queue is
78     * nonempty. (A one-based array is used in preference to the traditional
79     * zero-based array to simplify parent and child calculations.)
80 tim 1.2 *
81     * queue.length must be >= 2, even if size == 0.
82     */
83 tim 1.16 private transient Object[] queue;
84 tim 1.1
85 tim 1.2 /**
86     * The number of elements in the priority queue.
87     */
88     private int size = 0;
89 tim 1.1
90 tim 1.2 /**
91     * The comparator, or null if priority queue uses elements'
92     * natural ordering.
93     */
94 tim 1.16 private final Comparator<? super E> comparator;
95 tim 1.2
96     /**
97     * The number of times this priority queue has been
98     * <i>structurally modified</i>. See AbstractList for gory details.
99     */
100 dl 1.5 private transient int modCount = 0;
101 tim 1.2
102     /**
103 dholmes 1.21 * Creates a <tt>PriorityQueue</tt> with the default initial capacity
104 dl 1.7 * (11) that orders its elements according to their natural
105 tim 1.24 * ordering (using <tt>Comparable</tt>).
106 tim 1.2 */
107     public PriorityQueue() {
108 dholmes 1.11 this(DEFAULT_INITIAL_CAPACITY, null);
109 tim 1.1 }
110 tim 1.2
111     /**
112 dholmes 1.21 * Creates a <tt>PriorityQueue</tt> with the specified initial capacity
113 dl 1.7 * that orders its elements according to their natural ordering
114 tim 1.24 * (using <tt>Comparable</tt>).
115 tim 1.2 *
116     * @param initialCapacity the initial capacity for this priority queue.
117 dholmes 1.23 * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
118     * than 1
119 tim 1.2 */
120     public PriorityQueue(int initialCapacity) {
121     this(initialCapacity, null);
122 tim 1.1 }
123 tim 1.2
124     /**
125 dholmes 1.21 * Creates a <tt>PriorityQueue</tt> with the specified initial capacity
126 tim 1.2 * that orders its elements according to the specified comparator.
127     *
128     * @param initialCapacity the initial capacity for this priority queue.
129     * @param comparator the comparator used to order this priority queue.
130 dholmes 1.11 * If <tt>null</tt> then the order depends on the elements' natural
131     * ordering.
132 dholmes 1.15 * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less
133     * than 1
134 tim 1.2 */
135 dholmes 1.23 public PriorityQueue(int initialCapacity,
136     Comparator<? super E> comparator) {
137 tim 1.2 if (initialCapacity < 1)
138 dholmes 1.15 throw new IllegalArgumentException();
139 tim 1.16 this.queue = new Object[initialCapacity + 1];
140 tim 1.2 this.comparator = comparator;
141 tim 1.1 }
142    
143 tim 1.2 /**
144 dl 1.22 * Common code to initialize underlying queue array across
145     * constructors below.
146     */
147     private void initializeArray(Collection<? extends E> c) {
148     int sz = c.size();
149     int initialCapacity = (int)Math.min((sz * 110L) / 100,
150     Integer.MAX_VALUE - 1);
151     if (initialCapacity < 1)
152     initialCapacity = 1;
153    
154     this.queue = new Object[initialCapacity + 1];
155     }
156    
157     /**
158     * Initially fill elements of the queue array under the
159     * knowledge that it is sorted or is another PQ, in which
160 dl 1.36 * case we can just place the elements in the order presented.
161 dl 1.22 */
162     private void fillFromSorted(Collection<? extends E> c) {
163     for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
164     queue[++size] = i.next();
165     }
166    
167     /**
168 dl 1.36 * Initially fill elements of the queue array that is not to our knowledge
169     * sorted, so we must rearrange the elements to guarantee the heap
170     * invariant.
171 dl 1.22 */
172     private void fillFromUnsorted(Collection<? extends E> c) {
173     for (Iterator<? extends E> i = c.iterator(); i.hasNext(); )
174 dl 1.36 queue[++size] = i.next();
175     heapify();
176 dl 1.22 }
177    
178     /**
179     * Creates a <tt>PriorityQueue</tt> containing the elements in the
180     * specified collection. The priority queue has an initial
181     * capacity of 110% of the size of the specified collection or 1
182     * if the collection is empty. If the specified collection is an
183 tim 1.25 * instance of a {@link java.util.SortedSet} or is another
184 dl 1.22 * <tt>PriorityQueue</tt>, the priority queue will be sorted
185     * according to the same comparator, or according to its elements'
186     * natural order if the collection is sorted according to its
187     * elements' natural order. Otherwise, the priority queue is
188     * ordered according to its elements' natural order.
189 tim 1.2 *
190 dholmes 1.15 * @param c the collection whose elements are to be placed
191 tim 1.2 * into this priority queue.
192     * @throws ClassCastException if elements of the specified collection
193     * cannot be compared to one another according to the priority
194     * queue's ordering.
195 dholmes 1.15 * @throws NullPointerException if <tt>c</tt> or any element within it
196     * is <tt>null</tt>
197 tim 1.2 */
198 tim 1.16 public PriorityQueue(Collection<? extends E> c) {
199 dl 1.22 initializeArray(c);
200 dl 1.27 if (c instanceof SortedSet) {
201 dl 1.28 // @fixme double-cast workaround for compiler
202     SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c;
203 dl 1.22 comparator = (Comparator<? super E>)s.comparator();
204     fillFromSorted(s);
205 dl 1.27 } else if (c instanceof PriorityQueue) {
206 dl 1.22 PriorityQueue<? extends E> s = (PriorityQueue<? extends E>) c;
207     comparator = (Comparator<? super E>)s.comparator();
208     fillFromSorted(s);
209 tim 1.26 } else {
210 tim 1.2 comparator = null;
211 dl 1.22 fillFromUnsorted(c);
212 tim 1.2 }
213 dl 1.22 }
214    
215     /**
216     * Creates a <tt>PriorityQueue</tt> containing the elements in the
217     * specified collection. The priority queue has an initial
218     * capacity of 110% of the size of the specified collection or 1
219     * if the collection is empty. This priority queue will be sorted
220     * according to the same comparator as the given collection, or
221     * according to its elements' natural order if the collection is
222     * sorted according to its elements' natural order.
223     *
224     * @param c the collection whose elements are to be placed
225     * into this priority queue.
226     * @throws ClassCastException if elements of the specified collection
227     * cannot be compared to one another according to the priority
228     * queue's ordering.
229     * @throws NullPointerException if <tt>c</tt> or any element within it
230     * is <tt>null</tt>
231     */
232     public PriorityQueue(PriorityQueue<? extends E> c) {
233     initializeArray(c);
234     comparator = (Comparator<? super E>)c.comparator();
235     fillFromSorted(c);
236     }
237 dholmes 1.18
238 dl 1.22 /**
239     * Creates a <tt>PriorityQueue</tt> containing the elements in the
240     * specified collection. The priority queue has an initial
241     * capacity of 110% of the size of the specified collection or 1
242     * if the collection is empty. This priority queue will be sorted
243     * according to the same comparator as the given collection, or
244     * according to its elements' natural order if the collection is
245     * sorted according to its elements' natural order.
246     *
247     * @param c the collection whose elements are to be placed
248     * into this priority queue.
249     * @throws ClassCastException if elements of the specified collection
250     * cannot be compared to one another according to the priority
251     * queue's ordering.
252     * @throws NullPointerException if <tt>c</tt> or any element within it
253     * is <tt>null</tt>
254     */
255     public PriorityQueue(SortedSet<? extends E> c) {
256     initializeArray(c);
257     comparator = (Comparator<? super E>)c.comparator();
258     fillFromSorted(c);
259 tim 1.1 }
260    
261 dl 1.22 /**
262     * Resize array, if necessary, to be able to hold given index
263     */
264     private void grow(int index) {
265     int newlen = queue.length;
266     if (index < newlen) // don't need to grow
267     return;
268     if (index == Integer.MAX_VALUE)
269     throw new OutOfMemoryError();
270     while (newlen <= index) {
271     if (newlen >= Integer.MAX_VALUE / 2) // avoid overflow
272     newlen = Integer.MAX_VALUE;
273     else
274     newlen <<= 2;
275     }
276     Object[] newQueue = new Object[newlen];
277     System.arraycopy(queue, 0, newQueue, 0, queue.length);
278     queue = newQueue;
279     }
280    
281 dl 1.36
282 tim 1.2 /**
283 dl 1.42 * Inserts the specified element into this priority queue.
284 tim 1.2 *
285 dholmes 1.11 * @return <tt>true</tt>
286     * @throws ClassCastException if the specified element cannot be compared
287     * with elements currently in the priority queue according
288     * to the priority queue's ordering.
289 dholmes 1.18 * @throws NullPointerException if the specified element is <tt>null</tt>.
290 tim 1.2 */
291 dholmes 1.18 public boolean offer(E o) {
292     if (o == null)
293 dholmes 1.11 throw new NullPointerException();
294     modCount++;
295     ++size;
296    
297     // Grow backing store if necessary
298 dl 1.22 if (size >= queue.length)
299     grow(size);
300 dholmes 1.11
301 dholmes 1.18 queue[size] = o;
302 dholmes 1.11 fixUp(size);
303     return true;
304     }
305    
306 dl 1.40 public E peek() {
307 tim 1.2 if (size == 0)
308     return null;
309 tim 1.16 return (E) queue[1];
310 tim 1.1 }
311    
312 dholmes 1.23 // Collection Methods - the first two override to update docs
313 dholmes 1.11
314     /**
315 dholmes 1.23 * Adds the specified element to this queue.
316     * @return <tt>true</tt> (as per the general contract of
317     * <tt>Collection.add</tt>).
318     *
319 dl 1.40 * @throws NullPointerException if the specified element is <tt>null</tt>.
320 dholmes 1.15 * @throws ClassCastException if the specified element cannot be compared
321     * with elements currently in the priority queue according
322     * to the priority queue's ordering.
323 dholmes 1.11 */
324 dholmes 1.18 public boolean add(E o) {
325 dl 1.41 return offer(o);
326 tim 1.14 }
327 dholmes 1.11
328 dl 1.12 public boolean remove(Object o) {
329 dholmes 1.11 if (o == null)
330 dholmes 1.15 return false;
331 tim 1.2
332     if (comparator == null) {
333     for (int i = 1; i <= size; i++) {
334 tim 1.16 if (((Comparable<E>)queue[i]).compareTo((E)o) == 0) {
335 dl 1.36 removeAt(i);
336 tim 1.2 return true;
337     }
338     }
339     } else {
340     for (int i = 1; i <= size; i++) {
341 tim 1.16 if (comparator.compare((E)queue[i], (E)o) == 0) {
342 dl 1.36 removeAt(i);
343 tim 1.2 return true;
344     }
345     }
346     }
347 tim 1.1 return false;
348     }
349 tim 1.2
350 dholmes 1.23 /**
351     * Returns an iterator over the elements in this queue. The iterator
352     * does not return the elements in any particular order.
353     *
354     * @return an iterator over the elements in this queue.
355     */
356 tim 1.2 public Iterator<E> iterator() {
357 dl 1.7 return new Itr();
358 tim 1.2 }
359    
360     private class Itr implements Iterator<E> {
361 dl 1.35
362 dl 1.7 /**
363     * Index (into queue array) of element to be returned by
364 tim 1.2 * subsequent call to next.
365 dl 1.7 */
366     private int cursor = 1;
367 tim 1.2
368 dl 1.7 /**
369 dl 1.36 * Index of element returned by most recent call to next,
370     * unless that element came from the forgetMeNot list.
371     * Reset to 0 if element is deleted by a call to remove.
372 dl 1.7 */
373     private int lastRet = 0;
374    
375     /**
376     * The modCount value that the iterator believes that the backing
377     * List should have. If this expectation is violated, the iterator
378     * has detected concurrent modification.
379     */
380     private int expectedModCount = modCount;
381 tim 1.2
382 dl 1.36 /**
383     * A list of elements that were moved from the unvisited portion of
384     * the heap into the visited portion as a result of "unlucky" element
385     * removals during the iteration. (Unlucky element removals are those
386     * that require a fixup instead of a fixdown.) We must visit all of
387     * the elements in this list to complete the iteration. We do this
388     * after we've completed the "normal" iteration.
389     *
390     * We expect that most iterations, even those involving removals,
391     * will not use need to store elements in this field.
392     */
393     private ArrayList<E> forgetMeNot = null;
394    
395     /**
396     * Element returned by the most recent call to next iff that
397     * element was drawn from the forgetMeNot list.
398     */
399     private Object lastRetElt = null;
400 dl 1.35
401 dl 1.7 public boolean hasNext() {
402 dl 1.36 return cursor <= size || forgetMeNot != null;
403 dl 1.7 }
404    
405     public E next() {
406 tim 1.2 checkForComodification();
407 dl 1.36 E result;
408     if (cursor <= size) {
409     result = (E) queue[cursor];
410     lastRet = cursor++;
411     }
412     else if (forgetMeNot == null)
413 dl 1.7 throw new NoSuchElementException();
414 dl 1.36 else {
415     int remaining = forgetMeNot.size();
416     result = forgetMeNot.remove(remaining - 1);
417     if (remaining == 1)
418     forgetMeNot = null;
419     lastRet = 0;
420     lastRetElt = result;
421     }
422 tim 1.2 return result;
423 dl 1.7 }
424 tim 1.2
425 dl 1.7 public void remove() {
426 tim 1.2 checkForComodification();
427    
428 dl 1.36 if (lastRet != 0) {
429     E moved = PriorityQueue.this.removeAt(lastRet);
430     lastRet = 0;
431     if (moved == null) {
432     cursor--;
433     } else {
434     if (forgetMeNot == null)
435 dl 1.37 forgetMeNot = new ArrayList<E>();
436 dl 1.36 forgetMeNot.add(moved);
437     }
438     } else if (lastRetElt != null) {
439     PriorityQueue.this.remove(lastRetElt);
440     lastRetElt = null;
441     } else {
442     throw new IllegalStateException();
443 dl 1.35 }
444    
445 tim 1.2 expectedModCount = modCount;
446 dl 1.7 }
447 tim 1.2
448 dl 1.7 final void checkForComodification() {
449     if (modCount != expectedModCount)
450     throw new ConcurrentModificationException();
451     }
452 tim 1.2 }
453    
454 tim 1.1 public int size() {
455 tim 1.2 return size;
456 tim 1.1 }
457 tim 1.2
458     /**
459     * Remove all elements from the priority queue.
460     */
461     public void clear() {
462     modCount++;
463    
464     // Null out element references to prevent memory leak
465     for (int i=1; i<=size; i++)
466     queue[i] = null;
467    
468     size = 0;
469     }
470    
471 dl 1.40 public E poll() {
472 dl 1.36 if (size == 0)
473 dl 1.40 return null;
474 dl 1.36 modCount++;
475    
476     E result = (E) queue[1];
477     queue[1] = queue[size];
478     queue[size--] = null; // Drop extra ref to prevent memory leak
479     if (size > 1)
480     fixDown(1);
481    
482     return result;
483     }
484    
485     /**
486     * Removes and returns the ith element from queue. (Recall that queue
487     * is one-based, so 1 <= i <= size.)
488 tim 1.2 *
489 dl 1.36 * Normally this method leaves the elements at positions from 1 up to i-1,
490     * inclusive, untouched. Under these circumstances, it returns null.
491     * Occasionally, in order to maintain the heap invariant, it must move
492     * the last element of the list to some index in the range [2, i-1],
493     * and move the element previously at position (i/2) to position i.
494     * Under these circumstances, this method returns the element that was
495     * previously at the end of the list and is now at some position between
496     * 2 and i-1 inclusive.
497 tim 1.2 */
498 dl 1.36 private E removeAt(int i) {
499     assert i > 0 && i <= size;
500 tim 1.2 modCount++;
501    
502 dl 1.36 E moved = (E) queue[size];
503     queue[i] = moved;
504 tim 1.2 queue[size--] = null; // Drop extra ref to prevent memory leak
505 dl 1.35 if (i <= size) {
506 tim 1.2 fixDown(i);
507 dl 1.36 if (queue[i] == moved) {
508     fixUp(i);
509     if (queue[i] != moved)
510     return moved;
511     }
512 dl 1.35 }
513 dl 1.36 return null;
514 tim 1.1 }
515    
516 tim 1.2 /**
517     * Establishes the heap invariant (described above) assuming the heap
518     * satisfies the invariant except possibly for the leaf-node indexed by k
519     * (which may have a nextExecutionTime less than its parent's).
520     *
521     * This method functions by "promoting" queue[k] up the hierarchy
522     * (by swapping it with its parent) repeatedly until queue[k]
523     * is greater than or equal to its parent.
524     */
525     private void fixUp(int k) {
526     if (comparator == null) {
527     while (k > 1) {
528     int j = k >> 1;
529 tim 1.16 if (((Comparable<E>)queue[j]).compareTo((E)queue[k]) <= 0)
530 tim 1.2 break;
531 tim 1.16 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
532 tim 1.2 k = j;
533     }
534     } else {
535     while (k > 1) {
536 dl 1.35 int j = k >>> 1;
537 tim 1.16 if (comparator.compare((E)queue[j], (E)queue[k]) <= 0)
538 tim 1.2 break;
539 tim 1.16 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
540 tim 1.2 k = j;
541     }
542     }
543     }
544    
545     /**
546     * Establishes the heap invariant (described above) in the subtree
547     * rooted at k, which is assumed to satisfy the heap invariant except
548     * possibly for node k itself (which may be greater than its children).
549     *
550     * This method functions by "demoting" queue[k] down the hierarchy
551     * (by swapping it with its smaller child) repeatedly until queue[k]
552     * is less than or equal to its children.
553     */
554     private void fixDown(int k) {
555     int j;
556     if (comparator == null) {
557 dl 1.33 while ((j = k << 1) <= size && (j > 0)) {
558 dl 1.35 if (j<size &&
559     ((Comparable<E>)queue[j]).compareTo((E)queue[j+1]) > 0)
560 tim 1.2 j++; // j indexes smallest kid
561 dl 1.35
562 tim 1.16 if (((Comparable<E>)queue[k]).compareTo((E)queue[j]) <= 0)
563 tim 1.2 break;
564 tim 1.16 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
565 tim 1.2 k = j;
566     }
567     } else {
568 dl 1.33 while ((j = k << 1) <= size && (j > 0)) {
569 dl 1.35 if (j<size &&
570     comparator.compare((E)queue[j], (E)queue[j+1]) > 0)
571 tim 1.2 j++; // j indexes smallest kid
572 tim 1.16 if (comparator.compare((E)queue[k], (E)queue[j]) <= 0)
573 tim 1.2 break;
574 tim 1.16 Object tmp = queue[j]; queue[j] = queue[k]; queue[k] = tmp;
575 tim 1.2 k = j;
576     }
577     }
578 dl 1.36 }
579 dl 1.35
580 dl 1.36 /**
581     * Establishes the heap invariant (described above) in the entire tree,
582     * assuming nothing about the order of the elements prior to the call.
583     */
584     private void heapify() {
585     for (int i = size/2; i >= 1; i--)
586     fixDown(i);
587 tim 1.2 }
588    
589 dholmes 1.23 /**
590     * Returns the comparator used to order this collection, or <tt>null</tt>
591     * if this collection is sorted according to its elements natural ordering
592 tim 1.24 * (using <tt>Comparable</tt>).
593 dholmes 1.23 *
594     * @return the comparator used to order this collection, or <tt>null</tt>
595     * if this collection is sorted according to its elements natural ordering.
596     */
597 tim 1.16 public Comparator<? super E> comparator() {
598 tim 1.2 return comparator;
599     }
600 dl 1.5
601     /**
602     * Save the state of the instance to a stream (that
603     * is, serialize it).
604     *
605     * @serialData The length of the array backing the instance is
606     * emitted (int), followed by all of its elements (each an
607     * <tt>Object</tt>) in the proper order.
608 dl 1.7 * @param s the stream
609 dl 1.5 */
610 dl 1.22 private void writeObject(java.io.ObjectOutputStream s)
611 dl 1.5 throws java.io.IOException{
612 dl 1.7 // Write out element count, and any hidden stuff
613     s.defaultWriteObject();
614 dl 1.5
615     // Write out array length
616     s.writeInt(queue.length);
617    
618 dl 1.7 // Write out all elements in the proper order.
619 dl 1.39 for (int i=1; i<=size; i++)
620 dl 1.5 s.writeObject(queue[i]);
621     }
622    
623     /**
624     * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
625     * deserialize it).
626 dl 1.7 * @param s the stream
627 dl 1.5 */
628 dl 1.22 private void readObject(java.io.ObjectInputStream s)
629 dl 1.5 throws java.io.IOException, ClassNotFoundException {
630 dl 1.7 // Read in size, and any hidden stuff
631     s.defaultReadObject();
632 dl 1.5
633     // Read in array length and allocate array
634     int arrayLength = s.readInt();
635 tim 1.16 queue = new Object[arrayLength];
636 dl 1.5
637 dl 1.7 // Read in all elements in the proper order.
638 dl 1.39 for (int i=1; i<=size; i++)
639 dl 1.37 queue[i] = (E) s.readObject();
640 dl 1.5 }
641    
642 tim 1.1 }