public class Stack<E>
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
From AbstractList
Inherited methods
From 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
From Collection
From List
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.
push
public E push(E object)Pushes the specified object onto the top of the stack.
Parameters
objectE- The object to be added on top of the stack.
Returns
the object argument.
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
oObject- 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.