Home > Java > javaTutorial > What does java stack mean?

What does java stack mean?

(*-*)浩
Release: 2019-05-27 16:45:07
Original
4812 people have browsed it

Java Stack class

What does java stack mean?

stack (stack) is a subclass of Vector, which implements a standard last-in-first-out stack.

public class Stack<E>extends Vector<E>
Copy after login

The stack only defines a default constructor, which is used to create an empty stack. In addition to all the methods defined by Vector, the stack also defines some of its own methods.

The Stack class represents a last-in-first-out (LIFO) stack of objects.

It extends class Vector with five operations, allowing vectors to be treated as stacks. It provides the usual push and pop operations, as well as the peek method to take the stack vertex, the empty method to test whether the stack is empty, and the search method to find an item in the stack and determine the distance to the top of the stack.

When a stack is first created, it contains no items.

Direct Stack() creates an empty stack

Example:

import java.util.*;
 
public class StackDemo {
 
    static void showpush(Stack<Integer> st, int a) {
        st.push(new Integer(a));
        System.out.println("push(" + a + ")");
        System.out.println("stack: " + st);
    }
 
    static void showpop(Stack<Integer> 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<Integer> st = new Stack<Integer>();
        System.out.println("stack: " + st);
        showpush(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");
        }
    }
}
Copy after login

Result:

stack: [ ]
push(42)
stack: [42]
push(66)
stack: [42, 66]
push(99)
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack: [42]
pop -> 42
stack: [ ]
pop -> empty stack
Copy after login

The above is the detailed content of What does java stack mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template