public class Stack<E>

  1. Object
  2. AbstractCollection<E>
  3. AbstractList<E>
  4. Vector<E>
  5. Stack

ImplementsCollection<E>, Iterable<E>, List<E>, RandomAccess

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.

Constructors

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

Methods

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

Inherited fields

Inherited methods

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 synchronized 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

public synchronized 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

push

public E push(E object)
Pushes the specified object onto the top of the stack.

Parameters

object E
The object to be added on top of the stack.

Returns

the object argument.

See also

search

public synchronized int search(Object o)
Returns the index of the first occurrence of the object, starting from the top of the stack.

Parameters

o Object
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.