Class Stack<E>

  • Type Parameters:
    E - the type of elements stored in the map

    public class Stack<E>
    extends Object
    A simple, streamlined implementation of Stack. The implementation is not threadsafe.
    See Also:
    CollectionFactory.newStack()
    • Constructor Summary

      Constructors 
      Constructor Description
      Stack()
      Normal constructor supporting an initial size of 20.
      Stack(int initialSize)  
    • Constructor Detail

      • Stack

        public Stack()
        Normal constructor supporting an initial size of 20.
      • Stack

        public Stack(int initialSize)
        Parameters:
        initialSize - the initial size of the internal array (which will be expanded as necessary). For best efficiency, set this to the maximum depth of the stack.
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Returns true if the stack is empty.
      • getDepth

        public int getDepth()
        Returns the number of items currently in the stack.
      • clear

        public void clear()
        Clears the stack, the same as popping off all elements.
      • push

        public void push(E item)
        Pushes a new item onto the stack.
      • pop

        public E pop()
        Pops the top element off the stack and returns it.
        Returns:
        the top element of the stack
        Throws:
        IllegalStateException - if the stack is empty
      • peek

        public E peek()
        Returns the top element of the stack without affecting the stack.
        Returns:
        top element on the stack
        Throws:
        IllegalStateException - if the stack is empty
      • toString

        public String toString()
        Describes the stack, listing the element in order of depth (top element first).
        Overrides:
        toString in class Object
        Returns:
        string description of the stack
      • getSnapshot

        public Object[] getSnapshot()
        Returns a snapshot of the current state of the stack as an array of objects. The first object is the deepest in the stack, the last object is the most shallowest (most recently pushed onto the stack). The returned array may be manipulated (it is a copy).
        Returns:
        the stack as an object array