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

本文共 3688 字,大约阅读时间需要 12 分钟。

堆栈类 (Stack Class)

  • Stack class is available in java.util package.

    堆栈类在java.util包中可用。

  • The Stack Class is child class of Vector class.

    堆栈类是向量类的子类。

  • As we know that Stack is LIFO (Last-In-First-Out) data structure.

    众所周知,Stack是LIFO(后进先出)数据结构。

  • Here the above point given LIFO means last inserted element will be popped first.

    此处给出LIFO的上述点意味着最后插入的元素将首先弹出。

  • Stack class contains only one constructor i.e. Default Constructor.

    堆栈类仅包含一个构造函数,即默认构造函数。

  • The syntax of default constructor is given below:

    默认构造函数的语法如下:

    Stack st = new Stack();
  • Stack class defines various methods to perform various task onto the stack. The names of the stack class methods is given below:

    堆栈类定义了各种方法来执行各种任务到堆栈上。 堆栈类方法的名称如下:

    1. void push(Object obj)
    2. Object pop()
    3. Object peek()
    4. boolean empty()
    5. int search(Object obj)
  • Now, we will see the purpose of the Stack methods given above and we will study one by one.

    现在,我们将看到上面给出的Stack方法的目的,并且我们将一一研究。

i) void push(Object obj)

i)void push(Object obj)

  • This method is used to push an element into the stack.

    此方法用于将元素压入堆栈。

  • This method takes one parameter in the method of the Stack and the parameter is the object is to be inserted into the Stack.

    此方法在Stack方法中使用一个参数,该参数是要插入到Stack中的对象。

ii) Object pop()

ii)对象pop()

  • This method is used to remove an element from the Stack and then it returns to the top of the Stack.

    此方法用于从堆栈中删除元素,然后返回到堆栈顶部。

  • This method does not pass any parameter in the method of the Stack.

    此方法在Stack方法中不传递任何参数。

iii) boolean empty()

iii)布尔型empty()

  • This method is used to check whether the Stack is empty or not.

    此方法用于检查堆栈是否为空。

  • We don't pass any object as a parameter in the method of the Stack.

    在Stack方法中,我们不传递任何对象作为参数。

  • The return type of this method is Boolean so it returns true if Stack is empty.

    此方法的返回类型为Boolean,因此如果Stack为空,则返回true。

iv) Object peek()

iv)对象peek()

  • This method is used to return top of the Stack.

    此方法用于返回堆栈的顶部。

  • We don't pass any object as a parameter in the method of the Stack.

    在Stack方法中,我们不传递任何对象作为参数。

v) int search(Object obj)

v)int搜索(Object obj)

  • This method is used to search an element into the stack.

    此方法用于将元素搜索到堆栈中。

  • We pass only one object as a parameter in the method of the Stack and the parameter is the object is to be searched into the Stack.

    我们仅将一个对象作为参数传递给Stack方法,而该参数是要在Stack中搜索的对象。

  • The return type of this method is int so if an object is found in Stack so it returns offset or address from the top of the Stack else it returns -1 if an object is not found onto the Stack.

    此方法的返回类型为int,因此,如果在堆栈中找到一个对象,则它从堆栈顶部返回偏移量或地址,否则,如果在堆栈上未找到对象,则返回-1。

Example:

例:

// Java program to demonstrate the behavior of the Stack.import java.util.*;class StackClass {
public static void main(String[] args) {
// Creating an instance of Stack Stack st = new Stack(); // By using push() method to push few elements onto the Stack st.push(10); st.push(20); st.push(30); st.push(40); st.push(50); // Display Current Stack System.out.println("Current Stack List:" + st); // By using pop() method to remove an element from the // stack and return top of the Stack System.out.println("The Popped Element:" + st.pop()); // By using peek() method to return top of the Stack System.out.println("The top of the Stack:" + st.peek()); // By using search() method to search an element from the Stack System.out.println("Search element is:" + st.search(30)); System.out.println("Search element not found:" + st.search(60)); }}

Output

输出量

E:\Programs>javac StackClass.javaE:\Programs>java StackClassCurrent Stack List:[10, 20, 30, 40, 50]The Popped Element:50The top of the Stack:40Search element is:2Search element not found:-1

翻译自:

转载地址:http://upvzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_36、SpringBoot整合mybatis之事务处理实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>