Stack of Java Collections

Source: Internet
Author: User

Stack is simple, it inherits from Vector
Introduction to the 1th section of the stack
2nd section Stack source parsing (based on jdk1.6.0_45)
Part 3rd Vector Example

Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3308852.html

Introduction to the 1th section of the stack

About Stack

Stack is a stack. Its characteristics are: advanced post-Exit (FILO, first in the last out).

The stack in the Java Toolkit is inherited from vectors (vector queues), since vectors are implemented by arrays, which means thatstacks are also implemented through arrays , not linked lists . Of course, we can also use LinkedList as a stack! In the "vector detail of the Java Collection series 06 (source parsing) and usage examples", the data structure of the vector has been described in detail, and the data structure of the stack is no longer explained.

The inheritance relationship of stack

Java.lang.Object?     Java.util.abstractcollection<e>   ?     Java.util.abstractlist<e>       ?     Java.util.vector<e>           ?     Java.util.stack<e>public class Stack<e> extends Vector<e> {}

the relationship between stack and collection is as follows:

Constructor for Stack

The Stack has only one default constructor , as follows:

Stack ()

Stack's API

Stack is the stack, and its common API is as follows:

             Boolean       empty () synchronized e             peek () synchronized e             pop ()             e             push (E object) synchronized int           search (Object o)

Since the stack and inherits from the vector, It also contains all the APIs in the vector.

2nd section Stack source parsing (based on jdk1.6.0_45)

Stack source code is very simple, below we learn it.

1  PackageJava.util;2 3  Public4 classStack<e>extendsVector<e> {5     //The version ID. This is used for version upgrade control, not to be ignored here! 6     Private Static Final LongSerialversionuid = 1224463164541339165L;7 8     //constructor Function9      PublicStack () {Ten     } One  A     //push function: Put elements on top of the stack -      Publice push (E item) { -         //put the element on top of the stack.  the         //implementation of AddElement () in Vector.java - addelement (item); -  -         returnitem; +     } -  +     //Pop function: Returns the top element of the stack and removes it from the stack A      Public synchronizedE Pop () { at E obj; -         intLen =size (); -  -obj =Peek (); -         //Remove the top element of the stack, the implementation of Removeelementat () in Vector.java -Removeelementat (len-1); in  -         returnobj; to     } +  -     //Peek function: Returns the top element of the stack without performing a delete operation the      Public synchronizedE Peek () { *         intLen =size (); $ Panax Notoginseng         if(len = = 0) -             Throw Newemptystackexception (); the         //returns the top element of the stack, ElementAt () specifically implemented in Vector.java +         returnElementAt (len-1); A     } the  +     //whether the stack is empty -      Public Booleanempty () { $         returnSize () = = 0; $     } -  -     //find the position of "element O" in the stack: the number of top of the stack from the bottom of the stack the      Public synchronized intsearch (Object o) { -         //Gets the element index, ELEMENTAT () specifically implemented in Vector.javaWuyi         inti =lastIndexOf (o); the  -         if(I >= 0) { Wu             returnSize ()-i; -         } About         return-1; $     } -}

Summary :

The stack is actually implemented by an array.
When the push is executed (that is, the element is pushed into the stack ), it is by appending the element to the end of the array.
The element at the end of the array is returned when Peek is executed (that is, the top element of the stack is removed and no deletion is performed ).
When the pop is executed (that is, the top element of the stack is removed, and the element is deleted from the stack ), the element at the end of the array is taken out, and then the element is removed from the array.
Stack inherits from Vector, which means that the properties and functions owned by the vector are owned by the stack.

part 3rd Vector example

Here we learn how to use the stack using an example

1 ImportJava.util.Stack;2 ImportJava.util.Iterator;3 Importjava.util.List;4 5 /**6 * @desc Stack test procedure. Test usage of common APIs7  *8  * @authorSkywang9  */Ten  Public classStacktest { One  A      Public Static voidMain (string[] args) { -Stack stack =NewStack (); -         //adding 1,2,3,4,5 to the stack the          for(intI=1; i<6; i++) { - Stack.push (string.valueof (i)); -         } -  +         //Traverse and print out the stack - iteratorthroughrandomaccess (stack); +  A         //find the position of "2" in the stack and output at         intpos = Stack.search ("2"); -System.out.println ("The Postion of 2 is:" +POS); -  -         //after the top element of the PUP stack, traverse the stack - Stack.pop (); - iteratorthroughrandomaccess (stack); in  -         //after the top element of the peek stack, the traversal stack toString val =(String) Stack.peek (); +System.out.println ("Peek:" +val); - iteratorthroughrandomaccess (stack); the  *         //to traverse a stack through iterator $ iteratorthroughiterator (stack);Panax Notoginseng     } -  the     /** + * Traverse stack with quick access A      */ the      Public Static voiditeratorthroughrandomaccess (List list) { +String val =NULL; -          for(inti=0; I<list.size (); i++) { $val =(String) list.get (i); $System.out.print (val+ ""); -         } - System.out.println (); the     } - Wuyi     /** the * Iterate through the stack through an iterator -      */ Wu      Public Static voiditeratorthroughiterator (List list) { -  AboutString val =NULL; $          for(Iterator iter =list.iterator (); Iter.hasnext ();) { -val =(String) Iter.next (); -System.out.print (val+ ""); -         } A System.out.println (); +     } the  -}

Operation result :

Stack of Java Collections

Related Article

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.