Class ArrayListStack


  • public class ArrayListStack
    extends Object
    The ArrayListStack class represents a last-in-first-out (LIFO) stack of objects. It encapsulates class ArrayList with four operations that allow a 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, and a method to test for whether the stack is empty

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

    Version:
    $Revision: 1.3 $ on $Date: 2005/12/25 04:12:09 $
    Author:
    Darpan Dinker, $Author: tcfujii $
    • Constructor Detail

      • ArrayListStack

        public ArrayListStack​(int size)
        Creates a stack with the given initial size
      • ArrayListStack

        public ArrayListStack()
        Creates a stack with a default size
    • Method Detail

      • size

        public int size()
        Provides the current size of the stack.
        Returns:
        int return the current size.
      • push

        public void push​(Object obj)
        Pushes an item onto the top of this stack. This method will internally add elements to the ArrayList if the stack is full.
        Parameters:
        obj - the object to be pushed onto this stack.
        See Also:
        ArrayList.add(E, java.lang.Object[], int)
      • pop

        public Object pop()
        Removes the object at the top of this stack and returns that object as the value of this function.
        Returns:
        The object at the top of this stack (the last item of the ArrayList object). Null if stack is empty.
      • empty

        public boolean empty()
        Tests if this stack is empty.
        Returns:
        true if and only if this stack contains no items; false otherwise.
      • peek

        public Object peek()
        Looks at the object at the top of this stack without removing it from the stack.
        Returns:
        the object at the top of this stack (the last item of the ArrayList object). Null if stack is empty.