Java Collection Series 07 stack details (source parsing) and usage examples

Source: Internet
Author: User

http://www.cnblogs.com/skywang12345/p/3308852.html 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>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.

View Code

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 Import java.util.Stack; 2 Import Java.util.Iterator; 3 Import java.util.List; 4 5/** 6 * Test procedure for @desc stack.         To test the usage of the Common API 7 * 8 * @author Skywang 9 */10 public class Stacktest {All public static void main (string[] args) {13 Stack stack = new stack (), 14//Add 1,2,3,4,5 to the stack for (int i=1; i<6; i++) {stack.         Push (String.valueof (i)); 17}18 19//Traverse and print out the stack iteratorthroughrandomaccess (stack); 21 22         Find the position of "2" in the stack, and output a Stack.search ("2") for pos = $ System.out.println ("The Postion of 2 is:" +pos); 25 26 After the top element of the PUP stack, traverse the stack stack.pop (); iteratorthroughrandomaccess (stack), after the top element of the peek stack, Stack String val = (string) stack.peek (); System.out.println ("Peek:" +val); iteratorthroughrandom      Access (Stack), 34 35//through iterator to traverse Stack36 iteratorthroughiterator (stack); 37}38 39/**40 * Traverse Stack41 */42 pu via quick accessBlic static void iteratorthroughrandomaccess (List list) {String val = null;44 for (int i=0; I&LT;LIST.S Ize (); i++) {val = (String) list.get (i); System.out.print (val+ "");}48 System.out. println (); 49}50 51/**52 * Iterates through an iterator Stack53 */54 public static void Iteratorthroughiterator (List List ) {The String val = null;57 for (Iterator iter = List.iterator (); Iter.hasnext ();) {val = (String) iter.next (); System.out.print (val+ "");}61 System.out.print ln (); 62}63 64}

Operation result :

Java Collection Series 07 stack details (source parsing) and usage examples

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.