1 packagejava.util;2
3 /**
4 * The Stack
class represents a last-in-first-out5 * (LIFO) stack of objects. It extends class Vector with five6 * operations that allow a vector to be treated as a stack. The usual7 * push and pop operations are provided, as well as a8 * method to peek at the top item on the stack, a method to test9 * for whether the stack is empty, and a method to search10 * the stack for an item and discover how far it is from the top.11 *
12 * When a stack is first created, it contains no items.13 *14 *
A more complete and consistent set of LIFO stack operations is15 * provided by the {@linkDeque} interface and its implementations, which16 * should be used in preference to this class. For example:17 *
{@code
18 * Deque stack = new ArrayDeque();}
19 *20 *@authorJonathan Payne21 *@sinceJDK1.022 */23 public
24 class Stack extends Vector{25 /**
26 * Creates an empty Stack.27 */
28 publicStack() {29 }30
31 /**
32 * Pushes an item onto the top of this stack. This has exactly33 * the same effect as:34 *
36 *37 *@paramitem the item to be pushed onto this stack.38 *@returnthe35 * addElement(item)
item
argument.39 *@seejava.util.Vector#addElement40 */41 publicE push(E item) {42 addElement(item);43
44 returnitem;45 }46
47 /**
48 * Removes the object at the top of this stack and returns that49 * object as the value of this function.50 *51 *@returnThe object at the top of this stack (the last item52 * of the Vector object).53 *@throwsEmptyStackException if this stack is empty.54 */
55 public synchronizedE pop() {56 E obj;57 int len =size();58
59 obj =peek();60 removeElementAt(len - 1);61
62 returnobj;63 }64
65 /**
66 * Looks at the object at the top of this stack without removing it67 * from the stack.68 *69 *@returnthe object at the top of this stack (the last item70 * of the Vector object).71 *@throwsEmptyStackException if this stack is empty.72 */
73 public synchronizedE peek() {74 int len =size();75
76 if (len == 0)77 throw newEmptyStackException();78 return elementAt(len - 1);79 }80
81 /**
82 * Tests if this stack is empty.83 *84 *@returntrue
if and only if this stack contains85 * no items; false
otherwise.86 */
87 public booleanempty() {88 return size() == 0;89 }90
91 /**
92 * Returns the 1-based position where an object is on this stack.93 * If the object o occurs as an item in this stack, this94 * method returns the distance from the top of the stack of the95 * occurrence nearest the top of the stack; the topmost item on the96 * stack is considered to be at distance 1. The equals97 * method is used to compare o to the98 * items in this stack.99 *100 *@paramo the desired object.101 *@returnthe 1-based position from the top of the stack where102 * the object is located; the return value -1
103 * indicates that the object is not on the stack.104 */
105 public synchronized intsearch(Object o) {106 int i =lastIndexOf(o);107
108 if (i >= 0) {109 return size() -i;110 }111 return -1;112 }113
114 /**use serialVersionUID from JDK 1.0.2 for interoperability*/
115 private static final long serialVersionUID = 1224463164541339165L;116 }