本文共 7298 字,大约阅读时间需要 24 分钟。
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 since Java 1.0.
Java Stack是旧的Collection类。 它通过五个操作扩展了Vector类,以支持LIFO ( 后进先出)。 从Java 1.0开始,它在可用。
As Vector implements , 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实现 ,因此Stack类也是List实现类,但不支持Vector或List的所有操作。 由于Stack支持LIFO,因此也称为LIFO列表 。
In this post we will discuss following concepts about Stack in 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 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 extends Vector class with the following five operations only.
Java Stack仅通过以下五个操作扩展Vector类。
We will discuss these operations in-depth in the coming sections with some useful examples.
在接下来的部分中,我们将通过一些有用的示例来深入讨论这些操作。
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[]){ Stackstack = 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 : trueNon-Empty stack : [1001, 1002, 1003, 1004]Non-Empty stack: Pop Operation : 1004Non-Empty stack : After Pop Operation : [1001, 1002, 1003]Non-Empty stack : search() Operation : 2Non-Empty stack : false
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.
如下图所示,堆栈的Push操作始终在堆栈顶部插入新元素。
As shown in the below diagram, Stack’s Pop operation always removes an element from the top of the Stack.
如下图所示,堆栈的弹出操作始终从堆栈顶部删除元素。
Stack’s isEmpty() operation returns True if there are no elements, otherwise, it returns False.
如果没有元素,堆栈的isEmpty()操作将返回True,否则返回False。
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}; Stackstack = new Stack<>(); for(Integer i : intArr){ stack.push(i); } System.out.println("Non-Empty stack : " + stack); }}
Output:-
输出:-
Non-Empty stack : [1001, 1002, 1003, 1004]
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[]){ Stackstack = new Stack<>(); List 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 : trueNon-Empty stack : [1, 2, 3]
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[]){ Stackstack = new Stack<>(); stack.push(1); stack.push(2); stack.push(3); List 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:
参考:
翻译自:
java堆栈
转载地址:http://jqlzd.baihongyu.com/