Code example of stack in Java
The stack class in Java inherits from vector and its feature is last in first out
Examples of stacking in and out:
Java code example of example diagram:
Program running results:
edcba
Methods in stack class:
Official website API:
Method analysis:
Empty(): judge whether the stack is empty. If it is empty, return true; otherwise, return false
Peek(): takes out the top element of the stack, but does not remove the element from the stack
Pop (): take out the top element of the stack and remove it from the stack
Push (eitem): element stacking
Search (objecto): find the element position in the stack. The position is calculated from the top of the stack to the bottom. The top of the stack is 1,
Count down to the position of the searched element. If the searched element does not exist in the stack, it returns - 1.
Examples of these methods:
Program running results:
The above methods are the methods that stack inherits from the vector extension. Because stack inherits from vector, which is the non private method in vector
It is also a method of the stack class.
Methods in vector, official API_ 1.8:
summary
The above is all about the code examples of stack in Java. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!