Java Reference
In-Depth Information
private static final int DEFAULT_CAPACITY = 100;
1
2
3
/**
4
* Construct an empty PriorityQueue.
5
*/
6
public PriorityQueue( )
7
{
8
currentSize = 0;
9
cmp = null;
10
array = (AnyType[]) new Object[ DEFAULT_CAPACITY + 1 ];
11
}
12
13
/**
14
* Construct an empty PriorityQueue with a specified comparator.
15
*/
16
public PriorityQueue( Comparator<? super AnyType> c )
17
{
18
currentSize = 0;
19
cmp = c;
20
array = (AnyType[]) new Object[ DEFAULT_CAPACITY + 1 ];
21
}
22
23
24
/**
25
* Construct a PriorityQueue from another Collection.
26
*/
27
public PriorityQueue( Collection<? extends AnyType> coll )
28
{
29
cmp = null;
30
currentSize = coll.size( );
31
array = (AnyType[]) new Object[ ( currentSize + 2 ) * 11 / 10 ];
32
33
int i = 1;
34
for( AnyType item : coll )
35
array[ i++ ] = item;
36
buildHeap( );
37
}
figure 21.5
Constructors for the
PriorityQueue
class
1
/**
2
* Returns the smallest item in the priority queue.
3
* @return the smallest item.
4
* @throws NoSuchElementException if empty.
5
*/
6
public AnyType element( )
7
{
8
if( isEmpty( ) )
9
throw new NoSuchElementException( );
10
return array[ 1 ];
11
}
figure 21.6
The
element
routine
Search WWH ::
Custom Search