# Class Stack<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.Vector<E>
java.util.Stack<E>

All Implemented Interfaces:
  `Iterable<E>, Collection<E>, List<E>, RandomAccess`

public class Stack<E> extends Vector<E>
`Stack` is a Last-In/First-Out(LIFO) data structure which represents a stack of objects. It enables users to pop to and push from the stack, including null objects. There is no limit to the size of the stack.

- ## Field Summary

  ### Fields inherited from class Vector

  `capacityIncrement, elementCount, elementData`
  Modifier and Type
  Field
  Description
  `protected int`
  `capacityIncrement`
  How many elements should be added to the vector when it is detected that it needs to grow to accommodate extra entries.
  `protected int`
  `elementCount`
  The number of elements or the size of the vector.
  `protected Object[]`
  `elementData`
  The elements of the vector.

  ### Fields inherited from class AbstractList

  `modCount`
  Modifier and Type
  Field
  Description
  `protected int`
  `modCount`
  A counter for changes to the list.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Stack()`
  Constructs a stack with the default size of `Vector`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `empty()`
  Returns whether the stack is empty or not.
  `E`
  `peek()`
  Returns the element at the top of the stack without removing it.
  `E`
  `pop()`
  Returns the element at the top of the stack and removes it.
  `E`
  `push(E object)`
  Pushes the specified object onto the top of the stack.
  `int`
  `search(Object o)`
  Returns the index of the first occurrence of the object, starting from the top of the stack.

  ### Methods inherited from class Vector

  `add, add, addAll, addAll, addElement, capacity, clear, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, toString, trimToSize`
  Modifier and Type
  Method
  Description
  `void`
  `add(int location, E object)`
  Adds the specified object into this vector at the specified location.
  `boolean`
  `add(E object)`
  Adds the specified object at the end of this vector.
  `boolean`
  `addAll(int location, Collection<? extends E> collection)`
  Inserts the objects in the specified collection at the specified location in this vector.
  `boolean`
  `addAll(Collection<? extends E> collection)`
  Adds the objects in the specified collection to the end of this vector.
  `void`
  `addElement(E object)`
  Adds the specified object at the end of this vector.
  `int`
  `capacity()`
  Returns the number of elements this vector can hold without growing.
  `void`
  `clear()`
  Removes all elements from this vector, leaving it empty.
  `boolean`
  `contains(Object object)`
  Searches this vector for the specified object.
  `boolean`
  `containsAll(Collection<?> collection)`
  Searches this vector for all objects in the specified collection.
  `void`
  `copyInto(Object[] elements)`
  Attempts to copy elements contained by this `Vector` into the corresponding elements of the supplied `Object` array.
  `E`
  `elementAt(int location)`
  Returns the element at the specified location in this vector.
  `Enumeration<E>`
  `elements()`
  Returns an enumeration on the elements of this vector.
  `void`
  `ensureCapacity(int minimumCapacity)`
  Ensures that this vector can hold the specified number of elements without growing.
  `boolean`
  `equals(Object object)`
  Compares the specified object to this vector and returns if they are equal.
  `E`
  `firstElement()`
  Returns the first element in this vector.
  `E`
  `get(int location)`
  Returns the element at the specified location in this vector.
  `int`
  `hashCode()`
  Returns an integer hash code for the receiver.
  `int`
  `indexOf(Object object)`
  Searches in this vector for the index of the specified object.
  `int`
  `indexOf(Object object, int location)`
  Searches in this vector for the index of the specified object.
  `void`
  `insertElementAt(E object, int location)`
  Inserts the specified object into this vector at the specified location.
  `boolean`
  `isEmpty()`
  Returns if this vector has no elements, a size of zero.
  `E`
  `lastElement()`
  Returns the last element in this vector.
  `int`
  `lastIndexOf(Object object)`
  Searches in this vector for the index of the specified object.
  `int`
  `lastIndexOf(Object object, int location)`
  Searches in this vector for the index of the specified object.
  `E`
  `remove(int location)`
  Removes the object at the specified location from this vector.
  `boolean`
  `remove(Object object)`
  Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.
  `boolean`
  `removeAll(Collection<?> collection)`
  Removes all occurrences in this vector of each object in the specified Collection.
  `void`
  `removeAllElements()`
  Removes all elements from this vector, leaving the size zero and the capacity unchanged.
  `boolean`
  `removeElement(Object object)`
  Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.
  `void`
  `removeElementAt(int location)`
  Removes the element found at index position `location` from this `Vector`.
  `protected void`
  `removeRange(int start, int end)`
  Removes the objects in the specified range from the start to the, but not including, end index.
  `boolean`
  `retainAll(Collection<?> collection)`
  Removes all objects from this vector that are not contained in the specified collection.
  `E`
  `set(int location, E object)`
  Replaces the element at the specified location in this vector with the specified object.
  `void`
  `setElementAt(E object, int location)`
  Replaces the element at the specified location in this vector with the specified object.
  `void`
  `setSize(int length)`
  Sets the size of this vector to the specified size.
  `int`
  `size()`
  Returns the number of elements in this vector.
  `List<E>`
  `subList(int start, int end)`
  Returns a List of the specified portion of this vector from the start index to one less than the end index.
  `Object[]`
  `toArray()`
  Returns a new array containing all elements contained in this vector.
  `<T> T[]`
  `toArray(T[] contents)`
  Returns an array containing all elements contained in this vector.
  `String`
  `toString()`
  Returns the string representation of this vector.
  `void`
  `trimToSize()`
  Sets the capacity of this vector to be the same as the size.

  ### Methods inherited from class AbstractList

  `iterator, listIterator, listIterator`
  Modifier and Type
  Method
  Description
  `Iterator<E>`
  `iterator()`
  Returns an iterator on the elements of this list.
  `ListIterator<E>`
  `listIterator()`
  Returns a ListIterator on the elements of this list.
  `ListIterator<E>`
  `listIterator(int location)`
  Returns a list iterator on the elements of this list.

  ### Methods inherited from class Object

  `clone, getClass, notify, notifyAll, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  `final Class`
  `getClass()`
  Returns the runtime class of an object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `final void`
  `wait()`
  Causes current thread to wait until another thread invokes the method or the method for this object.
  `final void`
  `wait(long timeout)`
  Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.
  `final void`
  `wait(long timeout, int nanos)`
  Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

- ## Constructor Details

  - ### Stack

    public Stack()
    Constructs a stack with the default size of `Vector`.

- ## Method Details

  - ### empty

    public boolean empty()
    Returns whether the stack is empty or not.
    Returns:
      `true` if the stack is empty, `false` otherwise.

  - ### peek

    public E peek()
    Returns the element at the top of the stack without removing it.
    Returns:
      the element at the top of the stack.
    Throws:
      `EmptyStackException` - if the stack is empty.
    See Also:
      - `pop()`

  - ### pop

    public E pop()
    Returns the element at the top of the stack and removes it.
    Returns:
      the element at the top of the stack.
    Throws:
      `EmptyStackException` - if the stack is empty.
    See Also:
      - `peek()`
      - `push(E)`

  - ### push

    public E push(E object)
    Pushes the specified object onto the top of the stack.
    Parameters:
      `object` - The object to be added on top of the stack.
    Returns:
      the object argument.
    See Also:
      - `peek()`
      - `pop()`

  - ### search

    public int search(Object o)
    Returns the index of the first occurrence of the object, starting from the top of the stack.
    Parameters:
      `o` - the object to be searched.
    Returns:
      the index of the first occurrence of the object, assuming that the topmost object on the stack has a distance of one.