JAVA Stack details and sample learning
- 2020-04-01 02:29:39
- OfStack
Part 1 introduction to Stack
The Stack is the Stack. Its feature is: First In Last Out.
The Stack in the Java toolkit is inherited from Vector(Vector queue), and since Vector is implemented through arrays, this means that Stack is also implemented through arrays, not linked lists. Of course, we can also use LinkedList as a stack!
Stack's inheritance relationship
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131105103107.jpg? 2013105103322 ">
The relationship between Stack and Collection is shown in the following figure:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131105103117.jpg? 2013105103346 ">
The constructor of Stack
Stack has only one default constructor, as follows:
The Stack API
Since Stack and inherit from Vector, it also contains all the apis in Vector.
Part 2 Stack source code analysis
The source code for Stack is very simple, so let's learn about it.
Conclusion:
(01) Stack is actually implemented through arrays.
A push (that is, an element is pushed onto the stack) is performed by appending the element to the end of the array.
When peek is executed (that is, when the top element of the stack is removed, without deletion), it returns the element at the end of the array.
When you pull (that is, take the top element of the stack and remove it from the stack), you take the element at the end of the array and then remove it from the array.
(02) Stack is inherited from Vector, which means that all the properties and functions owned by Vector are owned by Stack.
Part 3 Vector examples
Now let's learn how to use Stack by example
The Stack is the Stack. Its feature is: First In Last Out.
The Stack in the Java toolkit is inherited from Vector(Vector queue), and since Vector is implemented through arrays, this means that Stack is also implemented through arrays, not linked lists. Of course, we can also use LinkedList as a stack!
Stack's inheritance relationship
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131105103107.jpg? 2013105103322 ">
The relationship between Stack and Collection is shown in the following figure:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131105103117.jpg? 2013105103346 ">
The constructor of Stack
Stack has only one default constructor, as follows:
Stack()
The Stack API
Stack It's the stack, which is commonly used API As follows:
boolean empty()
synchronized E peek()
synchronized E pop()
E push(E object)
synchronized int search(Object o)
Since Stack and inherit from Vector, it also contains all the apis in Vector.
Part 2 Stack source code analysis
The source code for Stack is very simple, so let's learn about it.
package java.util;
public
class Stack<E> extends Vector<E> {
//Version ID. This is for version upgrade control, not required here!
private static final long serialVersionUID = 1224463164541339165L;
//The constructor
public Stack() {
}
//Push function: stores an element at the top of the stack
public E push(E item) {
//Stores the element at the top of the stack.
//The implementation of addElement() is 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();
//Removing the top element of the stack, removeElementAt() is implemented in vect.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 () is 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: number from bottom to top of the stack
public synchronized int search(Object o) {
//Get element index, elementAt() is implemented in vector.java
int i = lastIndexOf(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
}
Conclusion:
(01) Stack is actually implemented through arrays.
A push (that is, an element is pushed onto the stack) is performed by appending the element to the end of the array.
When peek is executed (that is, when the top element of the stack is removed, without deletion), it returns the element at the end of the array.
When you pull (that is, take the top element of the stack and remove it from the stack), you take the element at the end of the array and then remove it from the array.
(02) Stack is inherited from Vector, which means that all the properties and functions owned by Vector are owned by Stack.
Part 3 Vector examples
Now let's learn how to use Stack by example
import java.util.Stack;
import java.util.Iterator;
import java.util.List;
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));
}
//Iterate over and print out the stack
iteratorThroughRandomAccess(stack) ;
//Find the position of "2" in the stack and print it
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 peek stack top element, traverse the stack
String val = (String)stack.peek();
System.out.println("peek:"+val);
iteratorThroughRandomAccess(stack) ;
//Iterator to iterate over the Stack
iteratorThroughIterator(stack) ;
}
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();
}
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();
}
}