Interface Stack

All Known Subinterfaces:
BitStack, ByteStack, CharStack, DoubleStack, FloatStack, IntStack, LongStack, MutableArray, MutableBitArray, MutableByteArray, MutableCharArray, MutableDoubleArray, MutableFloatArray, MutableIntArray, MutableLongArray, MutableObjectArray<E>, MutableObjectInPlaceArray<E>, MutablePArray, MutablePFixedArray, MutablePFloatingArray, MutablePIntegerArray, MutablePNumberArray, MutableShortArray, ObjectStack<E>, ShortStack

public interface Stack

Resizable stack of any elements.

Stack is a restricted version (inheritor) of MutableArray interface, allowing only access to the top element.

Please keep in mind: this package does not contain classes that implements Stack but not implements MutableArray. It means that the following operation usually works successfully:

 void someMyMethod(Stack stack) {
     MutableArray a = (MutableArray)stack;
     ... // access to any non-top elements
 }

Of course, it is not an example of good programming style, and there are no guarantees that such operator will not throw ClassCastException. But such an operation is usually posssible. Please compare:

 void someMyMethod(Array readOnlyArray) {
     MutableArray a = (MutableArray)readOnlyArray;
     ... // attempt to modify elements
 }

This code will throw ClassCastException, if the caller of someMyMethod does not forget to use asImmutable() method for creating readOnlyArray argument.

If this stack elements are primitive values (byte, short, etc.), the stack must implement one of BitStack, CharStack, ByteStack, ShortStack, IntStack, LongStack, FloatStack, DoubleStack subinterfaces. In other case, the stack must implement ObjectStack subinterface.

Objects, implementing this interface, are not thread-safe, but are thread-compatible and can be synchronized manually if multithread access is necessary.

Author:
Daniel Alievsky
  • Method Summary

    Modifier and Type
    Method
    Description
    Removes all elements from the stack
    long
    Returns the number of elements in this stack.
    Removes the element at the top of this stack and returns it, or throws EmptyStackException if the stack is empty.
    void
    Appends value element to the top of this stack.
    void
    Removes the element at the top of this stack and returns it, or throws EmptyStackException if the stack is empty.