Java Collection stack source deep parsing

Source: Internet
Author: User

Profile

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.

Introduction to stack introduction of the 1th 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.

Package Java.util;publicclass Stack<e> extends Vector<e> {///version ID.    This is used for version upgrade control, not to be ignored here!    Private static final long serialversionuid = 1224463164541339165L;        Constructor public stack () {}//push function: Put the element on top of the stack public E push (e Item) {//Put the element on top of the stack.        The implementation of AddElement () is addelement (item) in Vector.java;    return item;        }//Pop function: Returns the top element of the stack and removes it from the stack public synchronized e pop () {e obj;        int len = size ();        obj = Peek ();        Delete the top element of the stack, the implementation of Removeelementat () in Vector.java removeelementat (len-1);    return obj;        }//Peek function: Returns the top element of the stack without performing a delete operation public synchronized E peek () {int len = size ();        if (len = = 0) throw new emptystackexception ();    Returns the top element of the stack, ElementAt () specifically implemented in Vector.java return ElementAt (len-1);    }//Stack is empty public boolean empty () {return size () = = 0; }//Find the position of "element O" in the stack: from the bottom of the stack to the top of the stack public synchronized int search (Object o) {//Get elementIndex, ELEMENTAT () specifically implemented in Vector.java int i = lastIndexOf (o);        if (I >= 0) {return size ()-I;    } return-1; }}

Summarize:

The stack is actually implemented by an array.
Execution push (that is, pushing elements into the stack ) is done by appending the element to the end of the array.
The peek element at the end of the array is returned when executing (that is, removing the top element of the stack without performing a delete ).
Executing (that is, removing the pop top element of the stack and removing the element from the stack ) is to remove the element at the end of the array and then remove the element 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

Import Java.util.stack;import java.util.iterator;import java.util.list;/** * Test program for @desc Stack.  Test usage of Common API * * @author Skywang */public class Stacktest {public static void main (string[] args) {Stack stack =        New Stack ();        Add 1,2,3,4,5 to the stack for (int i=1; i<6; i++) {Stack.push (string.valueof (i));        }//Traverse and print out the stack iteratorthroughrandomaccess (stack);        Find the position of "2" in the stack and output int pos = 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);        After the top element of the peek stack, traverse the stack String val = (string) stack.peek ();        System.out.println ("Peek:" +val);        Iteratorthroughrandomaccess (stack);    Traverse stack iteratorthroughiterator (stack) through iterator; }/** * Traverse stack through Quick access */public static void Iteratorthroughrandomaccess (List list) {String val = nul        L for (int i=0; i<list.size (); i++) {val = (String) list.get (i);        System.out.print (val+ "");    } System.out.println ();        }/** * Traverse stack via iterator */public static void Iteratorthroughiterator (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:

Source: http://www.cnblogs.com/skywang12345/p/3308852.html

The article is inappropriate, please correct me, you can also pay attention to my public number: 好好学java , access to high-quality learning resources.

Java Collection stack source deep parsing

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.