Java-Stack source code analysis and examples, java-stack source code examples

Source: Internet
Author: User

Java-Stack source code analysis and examples, java-stack source code examples
Come to the end of the paper, and never know how to do this-Lu YouWenqu that clear such, for the source of live water to Zhu Xi

Stack is a Stack with the feature of First In Last Out (FILO, First In Last Out ). Stack inherits from Vector (Vector Queue). Because Vector is implemented in the same array, Stack also uses arrays instead of linked lists. The Relationship Between Stack and Collection is as follows:

Java 8-based source code:



Public class Stack <E> extends Vector <E> {public Stack () {// create an empty Stack} public E push (E item) {// inbound Stack addElement (item ); return item;} // public synchronized E pop () {E obj; int len = size (); obj = peek (); removeElementAt (len-1 ); return obj;} // return the top element of the stack, but the public synchronized E peek () {int len = size (); if (len = 0) throw new EmptyStackException (); return elementAt (len-1);} // judge whether the stack is empty public boolean empty () {return size () = 0 ;} // search for the element and return the stack depth public synchronized int search (Object o) {int I = lastIndexOf (o); if (I> = 0) {return size () -I;} return-1;} // sequence version number private static final longSerialVersionUID= 1224464254541339165l ;}


Copyright Disclaimer: knowledge is shared, and technology is for communication. Leave a blogger's link for reprinting.

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.