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 an ArrayList with a simplified interface for pushing and popping elements on and from the top
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class java.util.AbstractList

        modCount
    • Constructor Summary

      Constructors 
      Constructor and Description
      Stack(int size)
      Create a stack with an initial capacity of size
    • 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 removed
      int level()
      Return the index of the top element for the stack
      T pop()
      Removes the top element of the stack
      void 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.AbstractCollection

        containsAll, toString
      • Methods inherited from class java.util.AbstractList

        equals, hashCode
      • 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
      • Methods inherited from interface java.util.Collection

        parallelStream, stream
      • Methods inherited from interface java.util.List

        containsAll, equals, hashCode, of, of, of, of, of, of, of, of, of, of, of, of
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Stack

        public Stack(int size)
        Create a stack with an initial capacity of size
        Parameters:
        size - the initial capacity
    • Method Detail

      • size

        public int size()
        Returns the size of the stack
        Overrides:
        size in class java.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 an ArrayList, 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