public class ArrayQueue<E>
extends java.lang.Object
implements java.lang.Cloneable
ArrayQueue<E>
is a queue of references to E objects.
Limitations:
(1) The capacity of one of these queues can change after it's created, but
the maximum capacity is limited by the amount of free memory on the
machine. The constructor, add
, clone
,
and union
will result in an
OutOfMemoryError
when free memory is exhausted.
(2) A queue's capacity cannot exceed the maximum integer 2,147,483,647
(Integer.MAX_VALUE
). Any attempt to create a larger capacity
results in a failure due to an arithmetic overflow.
Java Source Code for this class:
http://www.cs.colorado.edu/~main/edu/colorado/collections/ArrayQueue.java
LinkedQueue
Constructor and Description |
---|
ArrayQueue()
Initialize an empty queue with an initial capacity of 10.
|
ArrayQueue(int initialCapacity)
Initialize an empty queue with a specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E item)
Insert a new item in this queue.
|
ArrayQueue<E> |
clone()
Generate a copy of this queue.
|
void |
ensureCapacity(int minimumCapacity)
Change the current capacity of this queue.
|
int |
getCapacity()
Accessor method to get the current capacity of this queue.
|
boolean |
isEmpty()
Determine whether this queue is empty.
|
E |
remove()
Get the front item, removing it from this queue.
|
int |
size()
Accessor method to determine the number of items in this queue.
|
void |
trimToSize()
Reduce the current capacity of this queue to its actual size (i.e., the
number of items it contains).
|
public ArrayQueue()
add
method works efficiently (without needing more
memory) until this capacity is reached.
Postcondition:
This queue is empty and has an initial capacity of 10.java.lang.OutOfMemoryError
- Indicates insufficient memory for:
new Object[10]
.public ArrayQueue(int initialCapacity)
add
method works efficiently (without needing more
memory) until this capacity is reached.initialCapacity
- the initial capacity of this queue
Precondition:
initialCapacity
is non-negative.
Postcondition:
This queue is empty and has the given initial capacity.java.lang.IllegalArgumentException
- Indicates that initialCapacity is negative.java.lang.OutOfMemoryError
- Indicates insufficient memory for:
new Object[initialCapacity]
.public ArrayQueue<E> clone()
clone
in class java.lang.Object
ArrayQueue
before it can be used.java.lang.OutOfMemoryError
- Indicates insufficient memory for creating the clone.public void add(E item)
item
- the item to be pushed onto this queue
Postcondition:
The item has been pushed onto this queue.java.lang.OutOfMemoryError
- Indicates insufficient memory for increasing the queue's capacity.
Note:
An attempt to increase the capacity beyond
Integer.MAX_VALUE
will cause the queue to fail with an
arithmetic overflow.public void ensureCapacity(int minimumCapacity)
minimumCapacity
- the new capacity for this queue
Postcondition:
This queue's capacity has been changed to at least minimumCapacity
.
If the capacity was already at or greater than minimumCapacity
,
then the capacity is left unchanged.java.lang.OutOfMemoryError
- Indicates insufficient memory for: new Object[minimumCapacity]
.public int getCapacity()
add
method works efficiently (without needing
more memory) until this capacity is reached.public boolean isEmpty()
true
if this queue is empty;
false
otherwise.public E remove()
java.util.NoSuchElementException
- Indicates that this queue is empty.public int size()
public void trimToSize()
java.lang.OutOfMemoryError
- Indicates insufficient memory for altering the capacity.