Class Stack<E>

java.lang.Object
ghidra.util.datastruct.Stack<E>
All Implemented Interfaces:
Iterable<E>
Direct Known Subclasses:
FixedSizeStack

public class Stack<E> extends Object implements 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
    Modifier and Type
    Field
    Description
    protected List<E>
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    add(E item)
    Appends the given item to the top of the stack.
    void
    Clears the stack.
    boolean
     
    get(int depth)
    Returns the element at the specified depth in this stack.
    int
     
    boolean
    Tests if this stack is empty.
    Returns an iterator over the items of the stack.
    Looks at the object at the top of this stack without removing it from the stack.
    pop()
    Removes the object at the top of this stack and returns that object as the value of this function.
    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
    Returns the number of elements in this stack.
    Returns a stream over this collection.
     

    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 Details

    • list

      protected List<E> list
  • Constructor Details

    • Stack

      public Stack()
      Creates an empty Stack.
    • Stack

      public Stack(int initialCapacity)
      Creates an empty Stack with specified capacity.
      Parameters:
      initialCapacity - the initial capacity.
    • Stack

      public Stack(Stack<E> stack)
      Copy Constructor. Creates a new stack using the items of the given stack. Only a shallow copy is performed.
      Parameters:
      stack - the stack to copy
  • Method Details

    • 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:
      depth - 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 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 Iterable<E>
      Returns:
      an iterator over the items of the stack
    • stream

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

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object