public class ObjectArrayFIFOQueue<K> extends Object implements PriorityQueue<K>, Serializable
Instances of this class represent a FIFO queue using a backing
array in a circular way. The array is enlarged and shrunk as needed. You can use the trim() method
to reduce its memory usage, if necessary.
This class provides additional methods that implement a deque (double-ended queue).
| Modifier and Type | Field and Description |
|---|---|
protected K[] |
array
The backing array.
|
protected int |
end
The end position in
array. |
static int |
INITIAL_CAPACITY
The standard initial capacity of a queue.
|
protected int |
length
The current (cached) length of
array. |
protected int |
start
The start position in
array. |
| Constructor and Description |
|---|
ObjectArrayFIFOQueue()
Creates a new empty queue with standard initial capacity.
|
ObjectArrayFIFOQueue(int capacity)
Creates a new empty queue with given capacity.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all elements from this queue.
|
Comparator<? super K> |
comparator()
Returns the comparator associated with this queue, or
null if it uses its elements' natural ordering. |
K |
dequeue()
Dequeues the first element from the queue.
|
K |
dequeueLast()
Dequeues the last element from the queue.
|
void |
enqueue(K x)
Enqueues a new element.
|
void |
enqueueFirst(K x)
Enqueues a new element as the first element (in dequeuing order) of the queue.
|
K |
first()
Returns the first element of the queue.
|
K |
last()
Returns the last element of the queue, that is, the element the would be dequeued last (optional operation).
|
int |
size()
Returns the number of elements in this queue.
|
void |
trim()
Trims the queue to the smallest possible size.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitchanged, isEmptypublic static final int INITIAL_CAPACITY
protected transient K[] array
protected transient int length
array.protected transient int start
protected transient int end
public ObjectArrayFIFOQueue(int capacity)
capacity - the initial capacity of this queue.public ObjectArrayFIFOQueue()
public Comparator<? super K> comparator()
null if it uses its elements' natural ordering.
This implementation returns null (FIFO queues have no comparator).
comparator in interface PriorityQueue<K>null if it uses its elements' natural ordering.public K dequeue()
PriorityQueuedequeue in interface PriorityQueue<K>public K dequeueLast()
NoSuchElementException - if the queue is empty.public void enqueue(K x)
PriorityQueueenqueue in interface PriorityQueue<K>x - the element to enqueue.public void enqueueFirst(K x)
x - the element to enqueue.public K first()
PriorityQueuefirst in interface PriorityQueue<K>public K last()
PriorityQueueThis default implementation just throws an UnsupportedOperationException.
last in interface PriorityQueue<K>public void clear()
PriorityQueueclear in interface PriorityQueue<K>public void trim()
public int size()
PriorityQueuesize in interface PriorityQueue<K>Copyright © 2020. All rights reserved.