Package project2
Class ResizableArrayStack<T>
java.lang.Object
project2.ResizableArrayStack<T>
- Type Parameters:
T
- the type of the elements in the stack
- All Implemented Interfaces:
StackInterface<T>
A class of stacks whose entries are stored in an array.
The array is expanded when the stack is full.
Implements the StackInterface.
- Version:
- 1.0
- Author:
- Lindsay Kislingbury
-
Constructor Summary
ConstructorDescriptionCreates an empty stack whose initial capacity is 50.ResizableArrayStack
(int initialCapacity) Creates an empty stack having a given initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all entries from this stack.boolean
isEmpty()
Detects whether this stack is empty.peek()
Retrieves this stack's top entry.pop()
Removes and returns this stack's top entry.void
Adds a new entry to the top of this stack.int
size()
Returns the number of elements in the stack.
-
Constructor Details
-
ResizableArrayStack
public ResizableArrayStack()Creates an empty stack whose initial capacity is 50. -
ResizableArrayStack
public ResizableArrayStack(int initialCapacity) Creates an empty stack having a given initial capacity. Throws an IllegalStateException if the initial capacity is outside the range of 0 to MAX_CAPACITY.- Parameters:
initialCapacity
- The integer capacity desired.
-
-
Method Details
-
size
public int size()Returns the number of elements in the stack.- Returns:
- The number of elements in the stack.
-
push
Adds a new entry to the top of this stack.- Specified by:
push
in interfaceStackInterface<T>
- Parameters:
newEntry
- An object to be added to the stack.- Throws:
IllegalArgumentException
- if the input is null.SecurityException
- if the object is corrupt.
-
pop
Removes and returns this stack's top entry.- Specified by:
pop
in interfaceStackInterface<T>
- Returns:
- The object at the top of the stack.
- Throws:
EmptyStackException
- if the stack is emptySecurityException
- if the object is corrupt.
-
peek
Retrieves this stack's top entry.- Specified by:
peek
in interfaceStackInterface<T>
- Returns:
- The object at the top of the stack.
- Throws:
EmptyStackException
- if the stack is emptySecurityException
- if the object is corrupt.
-
isEmpty
public boolean isEmpty()Detects whether this stack is empty.- Specified by:
isEmpty
in interfaceStackInterface<T>
- Returns:
- True if the stack is empty.
-
clear
public void clear()Removes all entries from this stack.- Specified by:
clear
in interfaceStackInterface<T>
- Throws:
SecurityException
- if the object is corrupt.
-