定义


栈是Vector的一个子类,它实现了一个标准的后进先出的栈。堆栈只定义了默认构造函数,用来创建一个空栈。 堆栈除了包括由Vector定义的所有方法,也定义了自己的一些方法。

图例


在下面图片中可以看到进栈(push)和出栈(pop)的过程。简单来说,栈只有一个入口(出口),所以先进后出(后进先出)就不难理解。

常用方法


序号 方法名 描述
1 Object push() 把项压入堆栈顶部
2 Object pop() 移除堆栈顶部的对象,并作为此函数的值返回该对象
3 Object peek() 查看堆栈顶部的对象,但不从堆栈中移除它。
4 boolean empty() 测试堆栈是否为空。
5 int search() 返回对象在堆栈中的位置,以 1 为基数。

源码


package java.util;

/**
* The <code>Stack</code> class represents a last-in-first-out
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
* operations that allow a vector to be treated as a stack. The usual
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
* method to <tt>peek</tt> at the top item on the stack, a method to test
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
* the stack for an item and discover how far it is from the top.
* <p>
* When a stack is first created, it contains no items.
*
* <p>A more complete and consistent set of LIFO stack operations is
* provided by the {@link Deque} interface and its implementations, which
* should be used in preference to this class. For example:
* <pre> {@code
* Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>
*
* @author Jonathan Payne
* @since JDK1.0
*/
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
} /**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item); return item;
} /**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size(); obj = peek();
removeElementAt(len - 1); return obj;
} /**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size(); if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
} /**
* Tests if this stack is empty.
*
* @return <code>true</code> if and only if this stack contains
* no items; <code>false</code> otherwise.
*/
public boolean empty() {
return size() == 0;
} /**
* Returns the 1-based position where an object is on this stack.
* If the object <tt>o</tt> occurs as an item in this stack, this
* method returns the distance from the top of the stack of the
* occurrence nearest the top of the stack; the topmost item on the
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
* method is used to compare <tt>o</tt> to the
* items in this stack.
*
* @param o the desired object.
* @return the 1-based position from the top of the stack where
* the object is located; the return value <code>-1</code>
* indicates that the object is not on the stack.
*/
public synchronized int search(Object o) {
int i = lastIndexOf(o); if (i >= 0) {
return size() - i;
}
return -1;
} /** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = 1224463164541339165L;
}

以上源码摘自jdk1.8,如需下载jdk1.8官方文档请点击这里

参考:http://www.runoob.com/java/java-stack-class.html  https://www.cnblogs.com/leefreeman/archive/2013/05/16/3082400.html

数据结构——Java Stack 类的更多相关文章

  1. 数据结构——java Queue类

    定义 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用 图例 Que ...

  2. java数据结构 栈stack

    栈(Stack) 栈(Stack)实现了一个后进先出(LIFO)的数据结构. 你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部. 当你从栈中取元素的时候,就从栈顶 ...

  3. java源码解析——Stack类

    在java中,Stack类继承了Vector类.Vector类和我们经常使用的ArrayList是类似的,底层也是使用了数组来实现,只不过Vector是线程安全的.因此可以知道Stack也是线程安全的 ...

  4. java.util.Stack类中 empty() 和 isEmpty() 方法的作用

    最近在学习算法和数据结构,用到Java里的Stack类,但程序运行结果一直和我预料的不一样,网上也没查清楚,最后查了API,才搞明白. java.util.Stack继承类 java.util.Vec ...

  5. Java 数据结构之Stack

    Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构.Stack继承Vector,并对其进行了扩展. 用法: 1.只有一个构造函数: public Stack() {} 2.创建 ...

  6. java.util.Stack类简介

    Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用 ...

  7. java.util.Stack类中的peek()方法

    java.util.stack类中常用的几个方法:isEmpty(),add(),remove(),contains()等各种方法都不难,但需要注意的是peek()这个方法. peek()查看栈顶的对 ...

  8. Java的Stack类实现List接口真的是个笑话吗

        今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”.   当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Ja ...

  9. java.util.Stack类简介(栈)

    Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起stack具有更好的完整性和一致性,应该被优先使用 ...

随机推荐

  1. SVM总结(参考源码ml.hpp)

    如何使用,请查阅我的另一篇博客——SVM的使用 1.setType() SVM的类型,默认SVM::C_SVC.具体有C_SVC=100,NU_SVC=101,ONE_CLASS=102,EPS_SV ...

  2. centOS+DJango+mysql_nginx部署流程记录

    安装Python3.6.2: https://www.jianshu.com/p/7a76bcc401a1 安装MySQL: https://www.cnblogs.com/luohanguo/p/9 ...

  3. development tool

    Eclipse :        https://www.eclipse.org/downloads/ WebStorm:   http://www.jetbrains.com/webstorm/do ...

  4. tensorflow中的Fetch、Feed(02-3)

    import tensorflow as tf #Fetch概念 在session中同时运行多个op input1=tf.constant(3.0) #constant()是常量不用进行init初始化 ...

  5. Excel使用小技巧

    1.Excel随机设置单元格的内容为整数0或1: 在单元格中写公式:  =ROUND(RAND(),0) 2.设置某个单元格的值为1或0,根据其他3个单元格的值为0或1来确定: 在该单元格中写公式: ...

  6. JVM虚拟机内存溢出垃圾收集及类加载机制总结

    1.Java内存区域与内存溢出异常 虚拟机栈:为虚拟机执行Java方法服务 本地方法栈:为虚拟机使用到的native方法服务. Java堆:是Java虚拟机所管理的内存中最大的一块,被所有线程共享的一 ...

  7. express框架开发接口部署线上环境PM2

    1.PM2介绍 PM2是一个线上环境下,用于启动nodejs进程守护的工具,用来保证服务的稳定及分摊服务器进程和压力. 2.下载安装 npm install pm2 -g  => pm2 --v ...

  8. python 网络爬虫(一)

    一.识别网站所用技术 构建网站所使用的技术类型也会对我们如何爬取产生影响.有一个十分有用的工具可以检查网站构建的技术类型---builtwith模块.该模块的安装如下 pip install buil ...

  9. 服务端OLEVARIANT数据之后传输

    将OLEVARIANT数据流化,然后对流进行压缩,还原成OLEVARIANT以后再发送. procedure StreamToVariant(Stream: TStream; var V: OLEVa ...

  10. B. Uniqueness 删除最小区间内的元素使得剩余元素唯一

    B. Uniqueness time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...