博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java堆栈_Java堆栈– Java堆栈
阅读量:2531 次
发布时间:2019-05-11

本文共 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列表

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
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 : 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

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
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
stack = 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]

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
stack = 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/

你可能感兴趣的文章
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
查看>>
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>
139句
查看>>
购买《哈利波特》书籍
查看>>
谈谈一些网页游戏失败的原因到底有哪些?(转)
查看>>
备案的问题
查看>>
asp.net下ajax.ajaxMethod使用方法
查看>>
win10+mongodb安装配置
查看>>
window7修改hosts文件
查看>>
【Leetcode_easy】720. Longest Word in Dictionary
查看>>
地铁时光机第一阶段冲刺一
查看>>
Code Smell那么多,应该先改哪一个?
查看>>
站立会议02(一期)
查看>>
oracle数据库导入导出问题
查看>>