|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectplanet.util.Queue
public class Queue
An implementation of queues based on arrays. The head of the queue starts out at the head of the array, allowing the queue to grow and shrink in constant time. This queue implementation is ideal for applications that require a queue with a known maximum size that expands in constant time.
Example usage:
To compute the sum of the unicode value of every character in the standard input we could use the following:
public static void main(String[] arguments) { int charsInInput = QueueExample.countChars(argument);Queue
q = newQueue(charsInInput)
; int unicodeSum = 0; if(arguments.length > 0){ for(int i=0; i < arguments.length; i++){ for(int j=0; j < arguments[i].length(); j++){ q.#remove(new Character(arguments[i].charAt(j)))
; } } } while(!q.isEmpty()
){ char c = ((Character)q.remove()
).charValue(); unicodeSum+=Character.getNumericValue(c); } System.out.println("Total Value: " + unicodeSum); }
Field Summary | |
---|---|
protected int |
count
current size of queue |
protected java.lang.Object[] |
data
The references to values stored within the queue. |
protected int |
head
index of the head of queue. |
Constructor Summary | |
---|---|
Queue(int size)
Construct a queue holding at most size elements. |
Method Summary | |
---|---|
void |
add(java.lang.Object value)
Add a value to the tail of the queue. |
void |
clear()
Remove all the values from the queue. |
java.lang.Object |
get()
Fetch the value at the head of the queue. |
boolean |
isEmpty()
Determine if the queue is empty. |
boolean |
isFull()
Determines if the queue is not able to accept any new values. |
java.lang.Object |
remove()
Remove a value from the head of the queue. |
int |
size()
Determine the number of elements within the queue Postconditions: returns the number of elements in the queue |
java.lang.String |
toString()
Construct a string representation of the queue. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected java.lang.Object[] data
protected int head
protected int count
Constructor Detail |
---|
public Queue(int size)
size
- The maximum size of the queue.Method Detail |
---|
public void add(java.lang.Object value) throws QueueFull
value
- The value added.
QueueFull
public java.lang.Object remove()
public java.lang.Object get()
public int size()
public void clear()
public boolean isFull()
public boolean isEmpty()
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |