Package uk.ac.qmul.eecs.ccmi.utilities
Class Stack<T>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<T>
-
- uk.ac.qmul.eecs.ccmi.utilities.Stack<T>
-
- Type Parameters:
T
- The type of objects contained in this stack
public class Stack<T> extends java.util.ArrayList<T>
The stack is anArrayList
with a simplified interface for pushing and popping elements on and from the top- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description Stack(int size)
Create a stack with an initial capacity ofsize
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description T
current()
Returns a reference to the top element of the stack, the element is not removedint
level()
Return the index of the top element for the stackT
pop()
Removes the top element of the stackvoid
push(T n)
Adds a new element at the top of the stack.int
size()
Returns the size of the stack-
Methods inherited from class java.util.ArrayList
add, add, addAll, addAll, clear, clone, contains, ensureCapacity, forEach, get, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeIf, removeRange, replaceAll, retainAll, set, sort, spliterator, subList, toArray, toArray, trimToSize
-
-
-
-
Constructor Detail
-
Stack
public Stack(int size)
Create a stack with an initial capacity ofsize
- Parameters:
size
- the initial capacity
-
-
Method Detail
-
size
public int size()
Returns the size of the stack- Overrides:
size
in classjava.util.ArrayList<T>
- Returns:
- the size of the stack
-
level
public int level()
Return the index of the top element for the stack- Returns:
- index of the top element for the stack or -1 if the stack is empty
-
push
public void push(T n)
Adds a new element at the top of the stack. Like in anArrayList
, the same object can be inserted more than once in the stack- Parameters:
n
- the new element to add
-
pop
public T pop()
Removes the top element of the stack- Returns:
- the removed element or
null
if the stack is empty
-
current
public T current()
Returns a reference to the top element of the stack, the element is not removed- Returns:
- the top element of the stack
-
-