Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Java Stack

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to implement the Java stack". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Stack

Stack is a linear table with limited operation, which follows the principle of first in and then out (Last-In-First-Out). For example, when we pour seasoning, the later seasoning will be used first.

The stack can only be inserted and deleted at the end of the table. One end of the opening is called the top of the stack and the other end is called the bottom of the stack. As shown in the figure:

Stack implementation of push method

The push method of the Stack that presses the item on top of the stack.

Code:

/ / push method public void push (E element) {array.add (array.size (), element);} pop method

The pop method of the stack (Stack) removes the top object of the stack and returns.

Code:

/ / pop method public E pop () {E element = array.get (array.size ()-1); array.remove (array.size ()-1); return element;} mainpublic static void main (String [] args) {/ / create stack Stack stack = new Stack (); / / push6 elements for (int I = 0; I < 5; iBI +) {stack.push (I) System.out.println (stack);} / / pop 5 elements for (int I = 0; I < 5; iTunes +) {stack.pop (); System.out.println (stack);}}

Output result:

Stack {array= [0]}

Stack {array= [0, 1]}

Stack {array= [0, 1, 2]}

Stack {array= [0, 1, 2, 3]}

Stack {array= [0, 1, 2, 3, 4]}

Stack {array= [0, 1, 2, 3]}

Stack {array= [0, 1, 2]}

Stack {array= [0, 1]}

Stack {array= [0]}

Stack {array= []}

The complete code import java.util.ArrayList;public class Stack {private ArrayList array; / / no-parameter construction public Stack () {array = new ArrayList ();} / parametric construction public Stack (int capacity) {array = new ArrayList (capacity);} / / push method public void push (E element) {array.add (array.size (), element) } / / pop method public E pop () {E element = array.get (array.size ()-1); array.remove (array.size ()-1); return element;} @ Override public String toString () {return "stack {" + "array=" + array +'}' } public static void main (String [] args) {/ / create stack Stack stack = new Stack (); / / push6 elements for (int I = 0; I < 5; iSum +) {stack.push (I); System.out.println (stack);} / / pop5 elements for (int I = 0; I < 5) ) {stack.pop (); System.out.println (stack);} "how the Java stack is implemented" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report