Class ArrayListStack<T>

  • All Implemented Interfaces:
    Stack<T>, java.io.Serializable, java.lang.Iterable<T>, java.util.Collection<T>, java.util.List<T>

    public class ArrayListStack<T>
    extends java.lang.Object
    implements Stack<T>, java.util.List<T>, java.io.Serializable
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayListStack()
      Constructs an empty stack.
      ArrayListStack​(int size)
      Constructs an empty stack of the size given.
      ArrayListStack​(java.util.ArrayList<T> list)
      Constructs the stack from the ArrayList provided.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, T element)  
      boolean add​(T element)  
      boolean addAll​(int index, java.util.Collection<? extends T> collection)  
      boolean addAll​(java.util.Collection<? extends T> collection)  
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> collection)  
      boolean empty()
      Tests if stack is empty.
      boolean equals​(java.lang.Object obj)
      Compares the specified object with this list for equality.
      T get​(int index)  
      java.util.ArrayList<T> getArrayList()
      returns the underlying ArrayList
      int hashCode()
      Returns the hash code value for this list.
      int indexOf​(java.lang.Object o)  
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      int lastIndexOf​(java.lang.Object o)  
      java.util.ListIterator<T> listIterator()  
      java.util.ListIterator<T> listIterator​(int index)  
      T peek()
      Returns item from the top of the stack.
      T pop()
      Removes and returns item from the top of the stack.
      T push​(T elem)
      Adds an item to the top of the stack.
      T remove​(int index)  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> collection)  
      boolean retainAll​(java.util.Collection<?> collection)  
      int search​(T o)
      Returns the 1-based position where an object is on this stack.
      T set​(int index, T element)  
      int size()
      Returns the size of the stack.
      java.util.List<T> subList​(int fromIndex, int toIndex)  
      java.lang.Object[] toArray()  
      java.lang.Object[] toArray​(java.lang.Object[] a)  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.List

        replaceAll, sort, spliterator
    • Constructor Detail

      • ArrayListStack

        public ArrayListStack()
        Constructs an empty stack.
      • ArrayListStack

        public ArrayListStack​(int size)
        Constructs an empty stack of the size given.
      • ArrayListStack

        public ArrayListStack​(java.util.ArrayList<T> list)
        Constructs the stack from the ArrayList provided.
    • Method Detail

      • getArrayList

        public java.util.ArrayList<T> getArrayList()
        returns the underlying ArrayList
      • push

        public T push​(T elem)
        Adds an item to the top of the stack.
        Specified by:
        push in interface Stack<T>
        Parameters:
        elem - the item to add.
        Returns:
        the item added.
      • pop

        public T pop()
              throws java.util.EmptyStackException
        Removes and returns item from the top of the stack.
        Specified by:
        pop in interface Stack<T>
        Returns:
        the former top item.
        Throws:
        java.util.EmptyStackException - if stack is empty.
      • peek

        public T peek()
               throws java.util.EmptyStackException
        Returns item from the top of the stack.
        Specified by:
        peek in interface Stack<T>
        Returns:
        the top item.
        Throws:
        java.util.EmptyStackException - if stack is empty.
      • empty

        public boolean empty()
        Tests if stack is empty.
        Specified by:
        empty in interface Stack<T>
        Returns:
        true if the stack is empty; false otherwise.
      • size

        public int size()
        Returns the size of the stack.
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.List<T>
        Specified by:
        size in interface Stack<T>
        Returns:
        the size of the stack.
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.List<T>
        Specified by:
        clear in interface Stack<T>
      • toString

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

        public boolean equals​(java.lang.Object obj)
        Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order.

        This implementation first checks if the specified object is this list. If so, it returns true; if not, it checks if the specified object is a list. If not, it returns false; if so, it iterates over both lists, comparing corresponding pairs of elements. If any comparison returns false, this method returns false. If either iterator runs out of elements before the other it returns false (as the lists are of unequal length); otherwise it returns true when the iterations complete.

        Specified by:
        equals in interface java.util.Collection<T>
        Specified by:
        equals in interface java.util.List<T>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to be compared for equality with this list.
        Returns:
        true if the specified object is equal to this list.
      • hashCode

        public int hashCode()
        Returns the hash code value for this list.

        This implementation uses exactly the code that is used to define the list hash function in the documentation for the List.hashCode method.

        Specified by:
        hashCode in interface java.util.Collection<T>
        Specified by:
        hashCode in interface java.util.List<T>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this list.
      • search

        public int search​(T o)
        Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack.
        Parameters:
        o - the desired object.
        Returns:
        the 1-based position from the top of the stack where the object is located; the return value -1 indicates that the object is not on the stack.
      • add

        public void add​(int index,
                        T element)
        Specified by:
        add in interface java.util.List<T>
      • add

        public boolean add​(T element)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.List<T>
      • addAll

        public boolean addAll​(java.util.Collection<? extends T> collection)
        Specified by:
        addAll in interface java.util.Collection<T>
        Specified by:
        addAll in interface java.util.List<T>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends T> collection)
        Specified by:
        addAll in interface java.util.List<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface java.util.List<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> collection)
        Specified by:
        containsAll in interface java.util.Collection<T>
        Specified by:
        containsAll in interface java.util.List<T>
      • get

        public T get​(int index)
        Specified by:
        get in interface java.util.List<T>
      • indexOf

        public int indexOf​(java.lang.Object o)
        Specified by:
        indexOf in interface java.util.List<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Specified by:
        isEmpty in interface java.util.List<T>
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.List<T>
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object o)
        Specified by:
        lastIndexOf in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator()
        Specified by:
        listIterator in interface java.util.List<T>
      • listIterator

        public java.util.ListIterator<T> listIterator​(int index)
        Specified by:
        listIterator in interface java.util.List<T>
      • remove

        public T remove​(int index)
        Specified by:
        remove in interface java.util.List<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.List<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> collection)
        Specified by:
        removeAll in interface java.util.Collection<T>
        Specified by:
        removeAll in interface java.util.List<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> collection)
        Specified by:
        retainAll in interface java.util.Collection<T>
        Specified by:
        retainAll in interface java.util.List<T>
      • set

        public T set​(int index,
                     T element)
        Specified by:
        set in interface java.util.List<T>
      • subList

        public java.util.List<T> subList​(int fromIndex,
                                         int toIndex)
        Specified by:
        subList in interface java.util.List<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>
      • toArray

        public java.lang.Object[] toArray​(java.lang.Object[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.List<T>