数据结构——Java Stack 类
定义
栈是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 类的更多相关文章
- 数据结构——java Queue类
定义 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用 图例 Que ...
- java数据结构 栈stack
栈(Stack) 栈(Stack)实现了一个后进先出(LIFO)的数据结构. 你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部. 当你从栈中取元素的时候,就从栈顶 ...
- java源码解析——Stack类
在java中,Stack类继承了Vector类.Vector类和我们经常使用的ArrayList是类似的,底层也是使用了数组来实现,只不过Vector是线程安全的.因此可以知道Stack也是线程安全的 ...
- java.util.Stack类中 empty() 和 isEmpty() 方法的作用
最近在学习算法和数据结构,用到Java里的Stack类,但程序运行结果一直和我预料的不一样,网上也没查清楚,最后查了API,才搞明白. java.util.Stack继承类 java.util.Vec ...
- Java 数据结构之Stack
Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构.Stack继承Vector,并对其进行了扩展. 用法: 1.只有一个构造函数: public Stack() {} 2.创建 ...
- java.util.Stack类简介
Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用 ...
- java.util.Stack类中的peek()方法
java.util.stack类中常用的几个方法:isEmpty(),add(),remove(),contains()等各种方法都不难,但需要注意的是peek()这个方法. peek()查看栈顶的对 ...
- Java的Stack类实现List接口真的是个笑话吗
今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”. 当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Ja ...
- java.util.Stack类简介(栈)
Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来 Deque(双端队列)比起stack具有更好的完整性和一致性,应该被优先使用 ...
随机推荐
- 模块学习--OS
1 返回当前目录信息 >>> os.getcwd() 'D:\\7_Python\\S14' 2 改变路径 >>> os.chdir('d:\\')#os.chdi ...
- 十三 Struts2复杂类型的数据封装,List封装和Map封装
在实际开发当中,有可能遇到批量向数据库中插入记录,需要在页面中将数据封装到集合中.类似页面表达式方法 List封装: 前端JSP: <%@ page language="java&qu ...
- 无线渗透之ettercap
无线渗透之ettercap ettercap命令查看 # ettercap -h Usage: ettercap [OPTIONS] [TARGET1] [TARGET2] TARGET is in ...
- 多线程server与多client通信
鉴于ServerSocket的accept方法是阻塞的,那么只能通过多线程的方式实现多客户端连接与服务器连接 基本步骤: 1,服务端创建ServerSocket绑定端口号,循环调用accept()方法 ...
- Python流程控制-2 条件判断
条件判断 条件判断是通过一条或多条判断语句的执行结果(True或者False)来决定执行的代码块. 在Python语法中,使用if.elif和else三个关键字来进行条件判断. if语句的一般形式如下 ...
- android原始sqlite中query的复杂用法
android直接执行sql是execSQL(String sql). 这个方法可以执行任意sql语句.但是改变这个不够灵活. query这个方法可以很好的解决这个问题. 执行query查询指定的数据 ...
- 设计模式课程 设计模式精讲 9-2 原型模式coding
1 课堂演练 1.1 super.toString 作用 1.2 为什么要使用克隆方法呢 2 代码解析 2.1 代码解析1(使用原型模式之前) 2.2 代码解析2(使用原型模式默认方式(浅克隆)) 2 ...
- 关于Simulink的sample time的问题
在对simulink建模的过程中,有时候会遇到sample time出现错误的问题,比如下图是我在使用simulink自带的Recursive least square Estimator最小二乘估计 ...
- python中提取位图信息(AttributeError: module 'struct' has no attribute 'unstack')
前言 今天这篇博文有点意思,它是从一个例子出发,从而体现出在编程中的种种细节和一些知识点的运用.和从前一样,我是人,离成神还有几十万里,所以无可避免的出现不严谨的地方甚至错误,请酌情阅读. 0x00 ...
- jgrid异步数据加载
参考:https://blog.csdn.net/hurryjiang/article/details/7077725