public class ShortStack
extends java.lang.Object
implements java.lang.Cloneable
ShortStack
is a stack of short values.
Limitations:
(1) The capacity of one of these stacks can change after it's created, but
the maximum capacity is limited by the amount of free memory on the
machine. The constructor, ensureCapacity
, push
,
and trimToSize
will result in an
OutOfMemoryError
when free memory is exhausted.
(2) A stack'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/ShortStack.java
ShortLinkedStack
,
ObjectStack
,
BooleanStack
,
ByteStack
,
CharStack
,
DoubleStack
,
FloatStack
,
IntStack
,
LongStack
Constructor and Description |
---|
ShortStack()
Initialize an empty stack with an initial capacity of 10.
|
ShortStack(int initialCapacity)
Initialize an empty stack with a specified initial capacity.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Generate a copy of this stack.
|
void |
ensureCapacity(int minimumCapacity)
Change the current capacity of this stack.
|
int |
getCapacity()
Accessor method to get the current capacity of this stack.
|
boolean |
isEmpty()
Determine whether this stack is empty.
|
short |
peek()
Get the top item of this stack, without removing the item.
|
short |
pop()
Get the top item, removing it from this stack.
|
void |
push(short item)
Push a new item onto this stack.
|
int |
size()
Accessor method to determine the number of items in this stack.
|
void |
trimToSize()
Reduce the current capacity of this stack to its actual size (i.e., the
number of items it contains).
|
public ShortStack()
push
method works efficiently (without needing more
memory) until this capacity is reached.
Postcondition:
This stack is empty and has an initial capacity of 10.java.lang.OutOfMemoryError
- Indicates insufficient memory for:
new short[10]
.public ShortStack(int initialCapacity)
push
method works efficiently (without needing more
memory) until this capacity is reached.initialCapacity
- the initial capacity of this stack
Precondition:
initialCapacity
is non-negative.
Postcondition:
This stack is empty and has the given initial capacity.java.lang.IllegalArgumentException
- Indicates that initialCapacity is negative.java.lang.OutOfMemoryError
- Indicates insufficient memory for:
new short[initialCapacity]
.public java.lang.Object clone()
clone
in class java.lang.Object
ShortStack
before it can be used.java.lang.OutOfMemoryError
- Indicates insufficient memory for creating the clone.public void ensureCapacity(int minimumCapacity)
minimumCapacity
- the new capacity for this stack
Postcondition:
This stack'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 short[minimumCapacity]
.public int getCapacity()
push
method works efficiently (without needing
more memory) until this capacity is reached.public boolean isEmpty()
true
if this stack is empty;
false
otherwise.public short peek()
java.util.EmptyStackException
- Indicates that this stack is empty.public short pop()
java.util.EmptyStackException
- Indicates that this stack is empty.public void push(short item)
item
- the item to be pushed onto this stack
Postcondition:
The item has been pushed onto this stack.java.lang.OutOfMemoryError
- Indicates insufficient memory for increasing the stack's capacity.
Note:
An attempt to increase the capacity beyond
Integer.MAX_VALUE
will cause the stack to fail with an
arithmetic overflow.public int size()
public void trimToSize()
java.lang.OutOfMemoryError
- Indicates insufficient memory for altering the capacity.