In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use stack in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use stack in Java.
Stack is a simple data structure used to store data, which is very similar to linked lists and sequential lists, but the biggest difference lies in the access operation of data. The insert and delete operations of the stack are only allowed to be performed on one end, so the end that allows the operation is called the top of the stack, and the one that does not allow the operation is called the bottom of the stack. Inserting elements is called push, deleting elements is called pop, and stacks without elements are empty.
Stack is a subclass of Vector and implements a standard last-in, first-out stack.
The stack defines only the default constructor, which is used to create an empty stack.
Stack ()
The stack can inherit all the methods of Vector, and it also has some methods of its own.
Instance code
Import java.util.*;public class StackDemo {static void showpush (Stack st, int a) {st.push (new Integer (a)); System.out.println ("push (" + a + ")); System.out.println (" stack: "+ st);} static void showpop (Stack st) {System.out.print (" pop-> "); Integer a-(Integer) st.pop () System.out.println (a); System.out.println ("stack:" + st);} public static void main (String args []) {Stack st = new Stack () System.out.println ("stack:" + st); shawpush (st,42); showpush (st,66); showpush (st,99); showpop (st); showpop (st) Showpop (st); try {showpop (st);} catch (EmptystackException e) {System.out.println ("empty stack");}
Running result:
Stack: [] push (42) stack: [42] push (66) stack: [42 push 66] push (99) stack: [42 66stack 66] pop-> 66stack: [42] pop-> 42stack: [] pop-> empty stack so far, I believe you have a deeper understanding of "how to use stack in Java". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.