"Go" Java Collection series 07 stack details (source parsing) and usage examples

Source: Internet
Author: User

Overview

After learning the vector, we started to learn about stack. The stack is simple and it inherits from the vector. Learning style or the same as before, the stack has a general understanding, and then learn its source, and finally through an example to learn to use it. The content includes:
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>publicclassextends 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.

 PackageJava.util; PublicclassStack<e>extendsVector<e> {    //The version ID. This is used for version upgrade control, not to be ignored here!     Private Static Final LongSerialversionuid = 1224463164541339165L; //constructor Function     PublicStack () {}//push function: Put elements on top of the stack     Publice push (E item) {//put the element on top of the stack. //implementation of AddElement () in Vector.javaaddelement (item); returnitem; }    //Pop function: Returns the top element of the stack and removes it from the stack     Public synchronizede pop () {e obj; intLen =size (); Obj=Peek (); //Remove the top element of the stack, the implementation of Removeelementat () in Vector.javaRemoveelementat (len-1); returnobj; }    //Peek function: Returns the top element of the stack without performing a delete operation     Public synchronizedE Peek () {intLen =size (); if(len = = 0)            Throw Newemptystackexception (); //returns the top element of the stack, ElementAt () specifically implemented in Vector.java        returnElementAt (len-1); }    //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     Public synchronized intsearch (Object o) {//Gets the element index, ELEMENTAT () specifically implemented in Vector.java        inti =lastIndexOf (o); if(I >= 0) {            returnSize ()-i; }        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

ImportJava.util.Stack;ImportJava.util.Iterator;Importjava.util.List;/*** @desc Stack test procedure. Test usage of Common API * *@authorSkywang*/ Public classStacktest { Public Static voidMain (string[] args) {Stack stack=NewStack (); //adding 1,2,3,4,5 to the stack         for(intI=1; i<6; i++) {Stack.push (string.valueof (i)); }        //Traverse and print out the stackiteratorthroughrandomaccess (stack); //find the position of "2" in the stack and output        intpos = Stack.search ("2"); System.out.println ("The postion of 2 is:" +POS); //after the top element of the PUP stack, traverse the stackStack.pop ();        Iteratorthroughrandomaccess (stack); //after the top element of the peek stack, the traversal stackString val =(String) Stack.peek (); System.out.println ("Peek:" +val);        Iteratorthroughrandomaccess (stack); //to traverse a stack through iteratoriteratorthroughiterator (stack); }    /*** Traverse stack with quick access*/     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 (); }    /*** Iterate through the stack through an iterator*/     Public Static voiditeratorthroughiterator (List list) {String Val=NULL;  for(Iterator iter =list.iterator (); Iter.hasnext ();) {Val=(String) iter.next (); System.out.print (Val+" ");    } System.out.println (); }}

Operation result :

"Go" Java Collection series 07 stack details (source parsing) and usage examples

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.