JAVA Stack Detailed introduction and sample learning _java

Source: Internet
Author: User
Tags arrays
1th Part Stack Introduction
Stack is a stack. Its characteristics are: Advanced and Out (FILO).
The stack in the Java Toolkit is inherited from vectors (vector queues), because vectors are implemented through arrays, which means that stack is also implemented through arrays, not linked lists. Of course, we can also use LinkedList as a stack!

Inheritance relationships for stack

The relationship between stack and collection is shown in the following diagram:

the constructor for the stack
Stack has only one default constructor, as follows:
Copy Code code as follows:

Stack ()

Stack's API
Copy Code code as follows:

Stack is a stack, and its common APIs are as follows:
Boolean empty ()
Synchronized E Peek ()
Synchronized E pop ()
E push (E object)
synchronized int search (Object o)

Because it is stack and inherits from Vector, it also contains all the APIs in the vector.
2nd part of the stack source analysis
Stack's source code is very simple, we learn about it below.
Copy Code code as follows:

Package java.util;
Public
Class Stack<e> extends Vector<e> {
Version ID. This is for version upgrade control, this is not a bother!
Private static final long serialversionuid = 1224463164541339165L;
Constructors
Public Stack () {
}
Push function: Storing elements on top of stack
Public e push (e item) {
Store the elements on top of the stack.
The implementation of AddElement () in Vector.java
AddElement (item);
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 stack top element, Removeelementat () implementation 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);
}
Whether the stack is empty
public Boolean empty () {
return size () = = 0;
}
Find the position of "element O" on the stack: by the bottom of the stack to the top of the stack
public synchronized int search (Object o) {
Gets the element index, ELEMENTAT () specifically implemented in Vector.java
int i = LastIndexOf (o);
if (I >= 0) {
return size ()-I;
}
return-1;
}
}

Summarize:
Stack is actually implemented through arrays.
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.
When Peek is performed (that is, the top element of the stack is removed and no deletion is performed), the element that returns the end of the array.
When the pull is executed (that is, the top element of the stack is removed and the element is removed from the stack), the element at the end of the array is removed, and the element is removed from the array.
Stack inherits from Vector, meaning that the vector has properties and functions that stack has.

Part 3rd Vector example
Let's learn how to use stack using the example below
Copy Code code as follows:

Import Java.util.Stack;
Import Java.util.Iterator;
Import java.util.List;
/**
* @desc Stack test procedure. Testing usage of common APIs
*
* @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 pup stack top element, traverse the stack
Stack.pop ();
Iteratorthroughrandomaccess (stack);
Peek stack after the top element, traversing the stack
String val = (string) stack.peek ();
System.out.println ("Peek:" +val);
Iteratorthroughrandomaccess (stack);
To traverse the stack through iterator
Iteratorthroughiterator (stack);
}
/**
* Traverse stack with fast access
*/
public static void Iteratorthroughrandomaccess (List list) {
String val = null;
for (int i=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 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 ();
}
}
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.