java堆栈_Java堆栈– Java堆栈

java堆栈

Java Stack is a legacy Collection class. It extends Vector class with five operations to support LIFO (Last In First Out). It is available in Collection API since Java 1.0.

Java Stack是旧的Collection类。 它通过五个操作扩展了Vector类,以支持LIFO后进先出)。 从Java 1.0开始,它在Collection API中可用。

As Vector implements List, Stack class is also a List implementation class but does NOT support all operations of Vector or List. As Stack supports LIFO, it is also known as LIFO Lists.

由于Vector实现List ,因此Stack类也是List实现类,但不支持Vector或List的所有操作。 由于Stack支持LIFO,因此也称为LIFO列表

Java堆栈 (Java Stack)

In this post we will discuss following concepts about Stack in java.

在本文中,我们将讨论有关Java中Stack的以下概念。

  1. Java Stack

    Java堆栈
  2. Java Stack Class Diagram

    Java堆栈类图
  3. Java Stack Methods

    Java Stack方法
  4. Java Stack Basic Example

    Java Stack基本示例
  5. How Stack’s push() and pop() works Internally?

    Stack的push()和pop()在内部如何工作?
  6. Java Array to Stack Example

    Java数组堆叠示例
  7. Java List to Stack Example

    Java List to Stack示例
  8. Java Stack to List Example

    Java堆栈列出示例

Java堆栈 (Java Stack)

Java Stack is LIFO object. It extends Vector class but supports only five operations. Java Stack class has only one constructor which is empty or default constructor. So, when we create a Stack, initially it contains no items that mean Stack is empty.

Java Stack是LIFO对象。 它扩展了Vector类,但仅支持五个操作。 Java Stack类只有一个为空的构造函数或默认构造函数。 因此,当我们创建堆栈时,最初它不包含任何表示堆栈为空的项目。

Stack internally has a pointer: TOP, which refers to the top of the Stack element. If Stack is empty, TOP refers to the before first element location. If Stack is not empty, TOP refers to the top element.

堆栈内部有一个指针:TOP,它指向堆栈元素的顶部。 如果Stack为空,则TOP引用第一个元素之前的位置。 如果Stack不为空,则TOP引用顶部元素。

Java堆栈类图 (Java Stack Class Diagram)

Java Stack extends Vector class directly and implements RandomAccess, List, Collection etc. interface indirectly. It is a LIFO list.

Java Stack直接扩展Vector类,并间接实现RandomAccess,List,Collection等接口。 这是一个后进先出清单。

Java Stack方法 (Java Stack Methods)

Java Stack extends Vector class with the following five operations only.

Java Stack仅通过以下五个操作扩展Vector类。

  1. boolean empty(): Tests if this stack is empty.

    boolean empty():测试此堆栈是否为空。
  2. E peek(): Looks at the object at the top of this stack without removing it from the stack.

    E peek():在不将其从堆栈中移除的情况下,查看此堆栈顶部的对象。
  3. E pop() : Removes the object at the top of this stack and returns that object as the value of this function.

    E pop():删除此堆栈顶部的对象,并将该对象作为此函数的值返回。
  4. E push(E item) : Pushes an item onto the top of this stack.

    E push(E item):将一个项目推到此堆栈的顶部。
  5. int search(Object o) : Returns the 1-based position where an object is on this stack.

    int search(Object o):返回对象在此堆栈上的从1开始的位置。

We will discuss these operations in-depth in the coming sections with some useful examples.

在接下来的部分中,我们将通过一些有用的示例来深入讨论这些操作。

Java堆栈示例 (Java Stack Example)

It’s time to develop basic Java Stack example to explore it’s operations.

现在是时候开发基本的Java Stack示例来探讨其操作了。

import java.util.Stack;

public class StackBasicExample {
    public static void main(String a[]){
        Stack<Integer> stack = new Stack<>();
        System.out.println("Empty stack : "  + stack);
        System.out.println("Empty stack : "  + stack.isEmpty());
        // Exception in thread "main" java.util.EmptyStackException
        // System.out.println("Empty stack : Pop Operation : "  + stack.pop());
        stack.push(1001);
        stack.push(1002);
        stack.push(1003);
        stack.push(1004);
        System.out.println("Non-Empty stack : "  + stack);
        System.out.println("Non-Empty stack: Pop Operation : "  + stack.pop());
        System.out.println("Non-Empty stack : After Pop Operation : "  + stack);
        System.out.println("Non-Empty stack : search() Operation : "  + stack.search(1002));
        System.out.println("Non-Empty stack : "  + stack.isEmpty());
    }
}

Output:

输出:

Empty stack : []
Empty stack : true
Non-Empty stack : [1001, 1002, 1003, 1004]
Non-Empty stack: Pop Operation : 1004
Non-Empty stack : After Pop Operation : [1001, 1002, 1003]
Non-Empty stack : search() Operation : 2
Non-Empty stack : false

Stack的push()和pop()操作如何在内部工作? (How Stack’s push() and pop() operations works Internally?)

As we know Stack’s push() and pop() are most frequently used Stack operations. The push() operation is used to insert an element into a Stack at the top. The pop() operation is used to remove the top element from a Stack.

众所周知,Stack的push()和pop()是最常用的Stack操作。 push()操作用于将元素插入顶部的Stack中。 pop()操作用于从堆栈中删除顶部元素。

Stack data structure has one internal property: top to refer top element of that stack. If Stack is empty, this top refers to the before first element as shown below:

堆栈数据结构具有一个内部属性:top引用该堆栈的top元素。 如果Stack为空,则此顶部指的是第一个元素之前的元素,如下所示:

As shown in the below diagram, Stack’s Push operation always inserts new element at the top of the Stack.

java stack example

如下图所示,堆栈的Push操作始终在堆栈顶部插入新元素。

As shown in the below diagram, Stack’s Pop operation always removes an element from the top of the Stack.

stack java program

如下图所示,堆栈的弹出操作始终从堆栈顶部删除元素。

Stack’s isEmpty() operation returns True if there are no elements, otherwise, it returns False.

如果没有元素,堆栈的isEmpty()操作将返回True,否则返回False。

Java数组堆叠示例 (Java Array to Stack Example)

Let us explore on “How to create a Stack object with a given Int array” here.

让我们在这里探讨“如何使用给定的Int数组创建Stack对象”。

import java.util.Stack;

public class ArrayToStackExample {
    public static void main(String a[]){
        Integer[] intArr = { 1001,1002,1003,1004};
        Stack<Integer> stack = new Stack<>();
        for(Integer i : intArr){
            stack.push(i);
        }
        System.out.println("Non-Empty stack : "  + stack);
    }
}

Output:-

输出:-

Non-Empty stack : [1001, 1002, 1003, 1004]

Java List to Stack示例 (Java List to Stack Example)

Let us explore on “How to create a Stack object with a given List of Integers” here.

让我们在这里探讨“如何使用给定的整数列表创建Stack对象”。

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class ListToStackExample {
    public static void main(String a[]){
        Stack<Integer> stack = new Stack<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println("Non-Empty stack addAll Operation : "  + stack.addAll(list));
        System.out.println("Non-Empty stack : "  + stack);
    }
}

Output:-

输出:-

Non-Empty stack : true
Non-Empty stack : [1, 2, 3]

Java堆栈列出示例 (Java Stack to List Example)

Let us explore on “How to create a List object with a Stack of Integers” here.

让我们在这里探讨“如何使用整数堆栈创建List对象”。

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class StackBasicExample {
    public static void main(String a[]){
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        List<Integer> list = new ArrayList<>();
        list.addAll(stack);
        System.out.println("Non-Empty stack : "  + stack);
        System.out.println("Non-Empty List : "  + list);
    }
}

Output:-

输出:-

Non-Empty stack : [1, 2, 3]
Non-Empty List : [1, 2, 3]

That’s all of a quick roundup on Stack in Java. I hope these Java Stack basics and examples will help you in getting started with Stack programming.

这就是Java中Stack的快速总结。 我希望这些Java Stack基础知识和示例将帮助您入门Stack编程。

Thank you for reading my tutorials. Please drop me a comment if you like my tutorials or have any issues or suggestions or any type errors.

感谢您阅读我的教程。 如果您喜欢我的教程或有任何问题或建议或任何类型错误,请给我评论。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/13401/java-stack

java堆栈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值