Class Queue
public class Queue implements java.util.Enumeration, Cloneable
{
// Constructors
public Queue ();
public Queue (int initialcapacity);
public Queue (int initialcapacity, int growthfactor);
// Methods
public synchronized void addElement (Object newelt);
public int capacity ();
protected Object clone () throws CloneNotSupportedException;
public synchronized Queue copy ();
public synchronized Object elementFromHead (int idx);
public synchronized Object elementFromTail (int idx);
public synchronized boolean hasMoreElements ();
public synchronized Object nextElement ();
public synchronized Object pop ();
public synchronized void removeAllElements ();
public synchronized int setCapacity (int newcapacity);
public synchronized int size ();
public String toString ();
}
This class provides a queue, implemented in an array.
public Queue ();
This class constructs a queue with an initial capacity of 32 elements. When the queue is full, its size doubles.
public Queue (int initialcapacity);
Constructs a queue with the given initial capacity. When the queue is full, the capacity doubles in size.
Parameter | Description |
initialcapacity
| The size of the queue to construct (the number of elements it will hold).
|
public Queue (int initialcapacity, int growthfactor);
Constructs a queue with the given initial capacity and growth factor.
Parameter | Description |
initialcapacity
| The initial size of the queue.
|
growthfactor
| The factor to grow by when the queue is full. If the growth factor is 1, the queue will drop elements at its head as needed.
|
public synchronized void addElement (Object newelt);
Adds an element to the tail of the queue.
Return Value:
No return value.
Parameter | Description |
newelt
| The new element to add to the queue.
|
Remarks:
The queue will grow, as necessary, by the factor provided when the queue was constructed. Null is a valid element to add.
public int capacity ();
Obtains the maximum number of objects that the queue can contain without resizing.
Return Value:
Returns the maximum capacity (in objects) of the queue.
protected Object clone () throws CloneNotSupportedException;
Creates a copy of the object.
Return Value:
Returns a copy of the Queue object.
Exceptions:
CloneNotSupportedException
if the object cannot be copied.
public synchronized Queue copy ();
Duplicates the queue.
Return Value:
Returns a copy of the Queue object.
public synchronized Object elementFromHead (int idx);
Retrieves an element relative to the head of the queue.
Return Value:
Returns the requested object.
Parameter | Description |
idx
| The offset from the first item in the queue.
|
public synchronized Object elementFromTail (int idx);
Retrieves an element relative to the tail of the queue.
Return Value:
Returns the requested object.
Parameter | Description |
idx
| The offset from the last item in the queue.
|
public synchronized boolean hasMoreElements ();
Determines if the queue is empty.
Return Value:
Returns true if there are more elements in the queue; otherwise, returns false.
public synchronized Object nextElement ();
Removes an object from the head of the queue.
Return Value:
Returns true if the object was successfully removed; otherwise, returns false.
Exceptions:
NoSuchElementException
if the queue is empty.
public synchronized Object pop ();
Retrieves the element last added to the queue.
Return Value:
Returns the requested object.
public synchronized void removeAllElements ();
Empties the queue.
Return Value:
No return value.
public synchronized int setCapacity (int newcapacity);
Changes the queue's capacity.
Return Value:
Returns the actual final capacity assigned to the queue.
Parameter | Description |
newcapacity
| The new number of elements in the queue.
|
public synchronized int size ();
Obtains the number of elements in the queue.
Return Value:
Returns the size of the queue (in elements).
public String toString ();
Retrieves a string representation of the Queue object.
Return Value:
Returns a string.