31 |
|
* @author Josh Bloch |
32 |
|
*/ |
33 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
34 |
< |
implements Queue<E> { |
34 |
> |
implements Queue<E>, |
35 |
> |
java.io.Serializable { |
36 |
|
private static final int DEFAULT_INITIAL_CAPACITY = 11; |
37 |
|
|
38 |
|
/** |
97 |
|
public PriorityQueue(int initialCapacity, Comparator<E> comparator) { |
98 |
|
if (initialCapacity < 1) |
99 |
|
initialCapacity = 1; |
100 |
< |
queue = new E[initialCapacity + 1]; |
100 |
> |
queue = (E[]) new Object[initialCapacity + 1]; |
101 |
|
this.comparator = comparator; |
102 |
|
} |
103 |
|
|
126 |
|
Integer.MAX_VALUE - 1); |
127 |
|
if (initialCapacity < 1) |
128 |
|
initialCapacity = 1; |
129 |
< |
queue = new E[initialCapacity + 1]; |
129 |
> |
queue = (E[]) new Object[initialCapacity + 1]; |
130 |
|
|
130 |
– |
/* Commented out to compile with generics compiler |
131 |
|
|
132 |
|
if (initialElements instanceof Sorted) { |
133 |
|
comparator = ((Sorted)initialElements).comparator(); |
134 |
|
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
135 |
|
queue[++size] = i.next(); |
136 |
|
} else { |
137 |
– |
*/ |
138 |
– |
{ |
137 |
|
comparator = null; |
138 |
|
for (Iterator<E> i = initialElements.iterator(); i.hasNext(); ) |
139 |
|
add(i.next()); |
217 |
|
* elements of the priority queue will be returned by this iterator in the |
218 |
|
* order specified by the queue, which is to say the order they would be |
219 |
|
* returned by repeated calls to <tt>poll</tt>. |
220 |
< |
* |
220 |
> |
* |
221 |
|
* @return an <tt>Iterator</tt> over the elements in this priority queue. |
222 |
|
*/ |
223 |
|
public Iterator<E> iterator() { |
278 |
|
|
279 |
|
/** |
280 |
|
* Returns the number of elements in this priority queue. |
281 |
< |
* |
281 |
> |
* |
282 |
|
* @return the number of elements in this priority queue. |
283 |
|
*/ |
284 |
|
public int size() { |
299 |
|
if (element == null) |
300 |
|
throw new NullPointerException(); |
301 |
|
modCount++; |
302 |
+ |
++size; |
303 |
|
|
304 |
|
// Grow backing store if necessary |
305 |
< |
if (++size == queue.length) { |
306 |
< |
E[] newQueue = new E[2 * queue.length]; |
307 |
< |
System.arraycopy(queue, 0, newQueue, 0, size); |
305 |
> |
while (size >= queue.length) { |
306 |
> |
E[] newQueue = (E[]) new Object[2 * queue.length]; |
307 |
> |
System.arraycopy(queue, 0, newQueue, 0, queue.length); |
308 |
|
queue = newQueue; |
309 |
|
} |
310 |
|
|
413 |
|
* @return the comparator associated with this priority queue, or |
414 |
|
* <tt>null</tt> if it uses its elements' natural ordering. |
415 |
|
*/ |
416 |
< |
Comparator comparator() { |
416 |
> |
public Comparator comparator() { |
417 |
|
return comparator; |
418 |
|
} |
419 |
|
|
451 |
|
|
452 |
|
// Read in array length and allocate array |
453 |
|
int arrayLength = s.readInt(); |
454 |
< |
queue = new E[arrayLength]; |
454 |
> |
queue = (E[]) new Object[arrayLength]; |
455 |
|
|
456 |
|
// Read in all elements in the proper order. |
457 |
|
for (int i=0; i<size; i++) |