Class Stack<E>

  • All Implemented Interfaces:
    java.lang.Iterable<E>
    Direct Known Subclasses:
    FixedSizeStack

    public class Stack<E>
    extends java.lang.Object
    implements java.lang.Iterable<E>

    The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class ArrayList with five operations that allow an array list to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

    When a stack is first created, it contains no items.

    Note: This implementation is not synchronized!

    • Field Summary

      Fields 
      ChangeModifier and Type Field Description
      NEWprotected java.util.List<E> list  
    • Constructor Summary

      Constructors 
      ChangeConstructor Description
      Stack()
      Creates an empty Stack.
      Stack​(int initialCapacity)
      Creates an empty Stack with specified capacity.
      Stack​(Stack<E> stack)
      Copy Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      ChangeModifier and Type Method Description
      void add​(E item)
      Appends the given item to the top of the stack.
      void clear()
      Clears the stack.
      boolean equals​(java.lang.Object obj)  
      E get​(int depth)
      Returns the element at the specified depth in this stack.
      int hashCode()  
      boolean isEmpty()
      Tests if this stack is empty.
      java.util.Iterator<E> iterator()
      Returns an iterator over the items of the stack.
      E peek()
      Looks at the object at the top of this stack without removing it from the stack.
      E pop()
      Removes the object at the top of this stack and returns that object as the value of this function.
      E push​(E item)
      Pushes an item onto the top of this stack.
      int search​(E o)
      Returns the position where an object is on this stack.
      int size()
      Returns the number of elements in this stack.
      java.util.stream.Stream<E> stream()
      Returns a stream over this collection.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

        forEach, spliterator
    • Field Detail

      • list NEW

        protected java.util.List<E> list

Constructor Detail

  • Method Detail

    • isEmpty

      public boolean isEmpty()
      Tests if this stack is empty.
    • peek

      public E peek()
      Looks at the object at the top of this stack without removing it from the stack.
    • pop

      public E pop()
      Removes the object at the top of this stack and returns that object as the value of this function.
    • push

      public E push​(E item)
      Pushes an item onto the top of this stack.
      Parameters:
      item - the object to push onto the stack.
    • search

      public int search​(E o)
      Returns the position where an object is on this stack.
      Parameters:
      o - the object to search for.
    • size

      public int size()
      Returns the number of elements in this stack.
      Returns:
      the number of elements in this stack
    • get

      public E get​(int depth)
      Returns the element at the specified depth in this stack. 0 indicates the bottom of the stack. size()-1 indicates the top of the stack.
      Parameters:
      index - the depth in the stack.
      Returns:
      the element at the specified depth in this stack
    • add

      public void add​(E item)
      Appends the given item to the top of the stack.
      Parameters:
      item - the new top of the stack
    • clear

      public void clear()
      Clears the stack. All items will be removed.
    • iterator

      public java.util.Iterator<E> iterator()
      Returns an iterator over the items of the stack. The iterator starts from the bottom of the stack.
      Specified by:
      iterator in interface java.lang.Iterable<E>
      Returns:
      an iterator over the items of the stack
    • stream

      public java.util.stream.Stream<E> stream()
      Returns a stream over this collection.
      Returns:
      a stream over this collection.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • equals

      public boolean equals​(java.lang.Object obj)
      Overrides:
      equals in class java.lang.Object
    • toString

      public java.lang.String toString()
      Overrides:
      toString in class java.lang.Object