java.util
Class Stack<E>
- java.lang.Object
-
- java.util.Vector<E>
-
- java.util.Stack<E>
-
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.
-
-
Constructor Summary
Constructors Constructor and Description Stack()
Constructs a stack with the default size ofVector
.
-
Method Summary
Methods Modifier and Type Method and 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 java.util.Vector
addElement, capacity, contains, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, removeAllElements, removeElement, removeElementAt, setElementAt, setSize, size, toString, trimToSize
-
-
-
-
Method Detail
-
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)
-
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.
-
-