public class ArrayStack<E> extends java.lang.Object implements Stack<E>
FullStackException| Modifier and Type | Field and Description |
|---|---|
protected int |
capacity
The capacity (i.e. the length) of the array used to implement the stack.
|
static int |
CAPACITY
Default array capacity.
|
protected E[] |
S
A generic array used to stores the elements of the stack.
|
protected int |
top
Index of the top element of the stack in the array.
|
| Constructor and Description |
|---|
ArrayStack()
Initializes the stack to use an array of default length.
|
ArrayStack(int capacity)
Initializes the stack to use an array of given length.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
isEmpty()
Tests whether the stack is empty.
|
E |
pop()
Removes the top element from the stack.
|
void |
push(E element)
Inserts an element at the top of the stack.
|
int |
size()
Returns the number of elements in the stack.
|
E |
top()
Inspects the element at the top of the stack.
|
java.lang.String |
toString()
Returns a string representation of the stack as a list of elements,
with the top element at the end: [ ...
|
protected int capacity
public static final int CAPACITY
protected E[] S
protected int top
public ArrayStack()
public ArrayStack(int capacity)
capacity - the array.public int size()
public boolean isEmpty()
public void push(E element) throws FullStackException
push in interface Stack<E>element - element to be inserted.FullStackException - if the array storing the elements is full.public E top() throws EmptyStackException
top in interface Stack<E>EmptyStackException - if the stack is empty.public E pop() throws EmptyStackException
pop in interface Stack<E>EmptyStackException - if the stack is empty.public java.lang.String toString()
toString in class java.lang.Object