Java learning notes-custom implementation stack set

Source: Internet
Author: User
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 ");}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.