Class Stack<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.Vector<E>
java.util.Stack<E>
- All Implemented Interfaces:
Iterable<E>, Collection<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.-
Field Summary
Fields inherited from class Vector
capacityIncrement, elementCount, elementDataModifier and TypeFieldDescriptionprotected intHow many elements should be added to the vector when it is detected that it needs to grow to accommodate extra entries.protected intThe number of elements or the size of the vector.protected Object[]The elements of the vector.Fields inherited from class AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanempty()Returns whether the stack is empty or not.peek()Returns the element at the top of the stack without removing it.pop()Returns the element at the top of the stack and removes it.Pushes the specified object onto the top of the stack.intReturns the index of the first occurrence of the object, starting from the top of the stack.Methods inherited from class 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, trimToSizeModifier and TypeMethodDescriptionvoidAdds the specified object into this vector at the specified location.booleanAdds the specified object at the end of this vector.booleanaddAll(int location, Collection<? extends E> collection) Inserts the objects in the specified collection at the specified location in this vector.booleanaddAll(Collection<? extends E> collection) Adds the objects in the specified collection to the end of this vector.voidaddElement(E object) Adds the specified object at the end of this vector.intcapacity()Returns the number of elements this vector can hold without growing.voidclear()Removes all elements from this vector, leaving it empty.booleanSearches this vector for the specified object.booleancontainsAll(Collection<?> collection) Searches this vector for all objects in the specified collection.voidAttempts to copy elements contained by thisVectorinto the corresponding elements of the suppliedObjectarray.elementAt(int location) Returns the element at the specified location in this vector.elements()Returns an enumeration on the elements of this vector.voidensureCapacity(int minimumCapacity) Ensures that this vector can hold the specified number of elements without growing.booleanCompares the specified object to this vector and returns if they are equal.Returns the first element in this vector.get(int location) Returns the element at the specified location in this vector.inthashCode()Returns an integer hash code for the receiver.intSearches in this vector for the index of the specified object.intSearches in this vector for the index of the specified object.voidinsertElementAt(E object, int location) Inserts the specified object into this vector at the specified location.booleanisEmpty()Returns if this vector has no elements, a size of zero.Returns the last element in this vector.intlastIndexOf(Object object) Searches in this vector for the index of the specified object.intlastIndexOf(Object object, int location) Searches in this vector for the index of the specified object.remove(int location) Removes the object at the specified location from this vector.booleanRemoves the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.booleanremoveAll(Collection<?> collection) Removes all occurrences in this vector of each object in the specified Collection.voidRemoves all elements from this vector, leaving the size zero and the capacity unchanged.booleanremoveElement(Object object) Removes the first occurrence, starting at the beginning and moving towards the end, of the specified object from this vector.voidremoveElementAt(int location) Removes the element found at index positionlocationfrom thisVector.protected voidremoveRange(int start, int end) Removes the objects in the specified range from the start to the, but not including, end index.booleanretainAll(Collection<?> collection) Removes all objects from this vector that are not contained in the specified collection.Replaces the element at the specified location in this vector with the specified object.voidsetElementAt(E object, int location) Replaces the element at the specified location in this vector with the specified object.voidsetSize(int length) Sets the size of this vector to the specified size.intsize()Returns the number of elements in this vector.subList(int start, int end) Returns a List of the specified portion of this vector from the start index to one less than the end index.Object[]toArray()Returns a new array containing all elements contained in this vector.<T> T[]toArray(T[] contents) Returns an array containing all elements contained in this vector.toString()Returns the string representation of this vector.voidSets the capacity of this vector to be the same as the size.Methods inherited from class AbstractList
iterator, listIterator, listIteratorModifier and TypeMethodDescriptioniterator()Returns an iterator on the elements of this list.Returns a ListIterator on the elements of this list.listIterator(int location) Returns a list iterator on the elements of this list.Methods inherited from class Object
clone, getClass, notify, notifyAll, wait, wait, waitModifier and TypeMethodDescriptionprotected Objectclone()final ClassgetClass()Returns the runtime class of an object.final voidnotify()Wakes up a single thread that is waiting on this object's monitor.final voidWakes up all threads that are waiting on this object's monitor.final voidwait()Causes current thread to wait until another thread invokes the method or the method for this object.final voidwait(long timeout) Causes current thread to wait until either another thread invokes the method or the method for this object, or a specified amount of time has elapsed.final voidwait(long timeout, int nanos) Causes current thread to wait until another thread invokes the method or the method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
-
Constructor Details
-
Stack
public Stack()Constructs a stack with the default size ofVector.
-
-
Method Details
-
empty
public boolean empty()Returns whether the stack is empty or not.- Returns:
trueif the stack is empty,falseotherwise.
-
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
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
-
search
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.
-