Name
Stack
Synopsis
A class that represent a last-in, first-out push down stack. Items on the stack
can be arbitrary Java objects. The push() method is used to
add another item to the top of the stack. The uppermost item on the stack can
be removed using the pop() method, which returns the item
to the caller. The peek() method returns the uppermost
object without removing it from the stack. If either
peek() or pop() is called when the
stack is empty, an EmptyStackException is thrown.
This situation can be detected in advance by checking the
empty() method, which returns true
when there are no items on the stack.
The search() method can be used to determine whether a
given object is in the stack. If the object is found, its distance from the
top of the stack is returned, where the topmost object is considered to be
at distance 1. If the object is not found in the stack, -1 is returned.
The Stack class does not provide any methods that allow
direct access to any item other than the topmost element . However, the
methods of its Vector superclass can be used to access
and remove any item on the stack. (This practice is not recommended, however,
since it breaks the encapsulation of the data within the stack.)
public class Stack extends Vector {
// Public Constructors
public Stack();
// Public Instance Methods
public boolean empty();
public Object peek(); // synchronized public Object ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access