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 use stack in Java

2025-03-17 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.

Share To

Development

  • How to write the notes for html5

    This article mainly explains "how to write the notes of html5". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write comments on html5. The way html5 notes are written is:

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

    12
    Report