Stack
Stack is a stack. Its features are: Filo, first in last out. The Stack in java toolkit is inherited from Vector (Vector queue). Because Vector is implemented by array, this means that Stack is also implemented by array, not chain list. Of course, we can also use LinkedList as a Stack. We have introduced the data structure of Vector in detail. We will not explain the data structure of Stack here.
Inheritance 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:
Stack's constructor
Stack has only one default constructor, as follows:
Stack()
API of Stack
boolean empty() synchronized E peek() synchronized E pop() E push(E object) synchronized int search(Object o)
Because Stack and inherit from Vector, it also contains all the API s in Vector.
Stack source code analysis
package java.util; public class Stack<E> extends Vector<E> { // Version ID. This is used for version upgrade control, no need to pay attention here! private static final long serialVersionUID = 1224463164541339165L; // Constructor public Stack() { } // push function: store elements on top of the stack public E push(E item) { // Store 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(); // Remove the stack top element. The implementation of removeElementAt() is in Vector.java removeElementAt(len - 1); return obj; } // peek function: returns the top element of the stack without deletion public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); // Return the stack top element, elementAt() is implemented in Vector.java return elementAt(len - 1); } // Is the stack 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 the element index, elementAt() is implemented in Vector.java int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } }
Stack is actually implemented through arrays.
When push is performed (that is, pushing an element onto the stack), it is done by appending the element to the end of the array.
When peek is executed (that is, taking out the top element of the stack without deleting), it returns the element at the end of the array.
When pop is executed (that is, taking out the top element of the stack and removing it from the stack), the element at the end of the array is taken out and then removed from the array.
Stack inherits from Vector, which means that stack has the attributes and functions of Vector.
import java.util.Stack; import java.util.Iterator; import java.util.List; /** * @desc Stack Test procedure. Testing the usage of common API s * * @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 pop 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) ; // Traverse the Stack through Iterator iteratorThroughIterator(stack) ; } /** * Traverse Stack through quick 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(); } /** * Traversing Stack through 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(); } }
Output result
1 2 3 4 5 the postion of 2 is:4 1 2 3 4 peek:4 1 2 3 4 1 2 3 4