Public class mystack <E> {private object [] elements; // declare the private int size of an object-type array; // the actual number of elements in the array public mystack () {elements = new object [10]; // The initial length is 10} // check whether the stack is empty public Boolean empty () {return size = 0; // if the number of elements is 0, true is returned. Otherwise, false is returned .} // View the object public e PEEK () at the top of the stack {// if no element is returned, nullif (empty () {return NULL ;} // If an element exists, return the last return (e) elements [size-1];} // remove the object public e POP () at the top of the stack () {e = PEEK (); // obtain the backup elements [size-1] = NULL for the last element; // assign a null value to the last element of the array. Size --; // reduce the number of elements by 1 return E;} // press the item to the top of the stack. Public E push (E item) {ensurecapacity (size + 1); // check the capacity of elements [size ++] = item; // Save the passed elements, the number of elements increases by 1 return item;} // check whether the capacity is sufficient. If not, create a new array private void ensurecapacity (INT size) {int Len = elements. length; // obtain the current length of the array if (size> Len) {int newlen = (LEN * 3)/2 + 1; // the size of each array expansion elements = arrays. copyof (elements, newlen) ;}// returns the position of the object in the stack. The value is 1-based public int search (Object O) {int Index = lastindexof (o );/ /Obtain the subscript Return Index =-1? Of the element in the array? Index: Size-index;} // search for the following method: Private int lastindexof (Object O) {If (empty () {Throw new emptystackexception (); // If the array is empty, a custom exception is thrown.} // If (O = NULL) when the input element is empty) {for (INT I = size-1; I> = 0; I --) {If (elements [I] = NULL) {return I ;}} // when not empty} else {for (INT I = size-1; I> = 0; I --) {If (O. equals (elements [I]) {return I ;}} return-1; // not found, return-1} // custom exception Private Static class emptystackexception extends runtimeexception {public emptystackexception () {super ("Stack is empty ");}}}