Class IndexedStack<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>

    public class IndexedStack<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    This class replicates functionality that is available in a Java Stack, but not in a C# Stack, for example the ability to access elements by direct index (0-based, counting from the bottom of the stack). It is used in place of Stack in the few places where this functionality is needed.

    The stack is iterable, and iteration is in bottom-to-top order (like Java, but unlike a C# stack where iteration is top-to-bottom)

    • Constructor Summary

      Constructors 
      Constructor Description
      IndexedStack()
      Create an empty stack with a default initial space allocation
      IndexedStack​(int size)
      Create an empty stack with a specified initial space allocation
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean contains​(T value)
      Search for an item on the stack
      T get​(int i)
      Get the item at position N in the stack, without changing the state of the stack
      int indexOf​(T value)
      Search for an item on the stack, starting from the bottom
      boolean isEmpty()
      Ask if the stack is empty
      java.util.Iterator<T> iterator()
      Iterate the stack in bottom to top order
      T peek()
      Get the item on the top of the stack, without changing the state of the stack
      T pop()
      Get the item on the top of the stack, and remove it from the stack
      void push​(T item)
      Add an item to the top of the stack
      void set​(int i, T value)
      Overwrite the item at position N in the stack
      int size()
      Get the current height of the stack
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • IndexedStack

        public IndexedStack()
        Create an empty stack with a default initial space allocation
      • IndexedStack

        public IndexedStack​(int size)
        Create an empty stack with a specified initial space allocation
        Parameters:
        size - the number of entries to be allocated
    • Method Detail

      • size

        public int size()
        Get the current height of the stack
        Returns:
        the number of items on the stack
      • isEmpty

        public boolean isEmpty()
        Ask if the stack is empty
        Returns:
        true if the stack contains no items
      • push

        public void push​(T item)
        Add an item to the top of the stack
        Parameters:
        item - the item to be added
      • peek

        public T peek()
        Get the item on the top of the stack, without changing the state of the stack
        Returns:
        the topmost item
        Throws:
        java.util.EmptyStackException - if the stack is empty
      • pop

        public T pop()
        Get the item on the top of the stack, and remove it from the stack
        Returns:
        the topmost item
        Throws:
        java.util.EmptyStackException - if the stack is empty
      • get

        public T get​(int i)
        Get the item at position N in the stack, without changing the state of the stack
        Parameters:
        i - the position of the required item, where the first item counting from the bottom of the stack is position 0 (zero)
        Returns:
        the item at the specified position
        Throws:
        java.lang.IndexOutOfBoundsException - if i is negative, or greater than or equal to the stack size
      • set

        public void set​(int i,
                        T value)
        Overwrite the item at position N in the stack
        Parameters:
        i - the position of the required item, where the first item counting from the bottom of the stack is position 0 (zero)
        value - the item to be put at the specified position
        Throws:
        java.lang.IndexOutOfBoundsException - if i is negative, or greater than or equal to the stack size
      • contains

        public boolean contains​(T value)
        Search for an item on the stack
        Parameters:
        value - the item being sought
        Returns:
        true if the stack contains an item equal to the sought item
      • indexOf

        public int indexOf​(T value)
        Search for an item on the stack, starting from the bottom
        Parameters:
        value - the item being sought
        Returns:
        the index position of the first item found that is equal to value, or -1 if no such item is found
      • iterator

        public java.util.Iterator<T> iterator()
        Iterate the stack in bottom to top order
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Returns:
        an iterator that starts at the bottom of the stack and proceeds to the top