数据结构——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具有更好的完整性和一致性,应该被优先使用 ...
 
随机推荐
- python 静态方法,类方法,类下面的函数区别
			
@clssmenthod(类方法) 与 @staticmethod(静态方法) 与类下面的函数的区别: 1.@classmethod修饰的方法def name(cls)需要通过cls参数传递当前类本身 ...
 - 笔记-python-standard library-8.3.collections
			
笔记-python-standard library-8.3.collections 1. collections简介 Source code: Lib/collections/__init ...
 - 5 JSON&与JavaScript转换&JavaScript:void(0)&JavaScript代码规范
			
JSON:JavaScript Object Notation JS对象简谱 一种轻量级的数据交换格式,用于存储和传输数据的格式,通常用于服务端向网页传递数据 是独立的语言,易于理解 JSON语法 ...
 - Windows 10 20H1版名称被定为Windows 10 Version 2004版以示区分
			
导读 我们知道Windows 10 20H1 版目前的开发工作已经接近完成,当前微软主要通过新版本来修复部分已知的问题. 而名称上面按照以往规律推算应该是 Windows 10 Version 200 ...
 - 【pwnable.kr】 asm
			
一道写shellcode的题目, #include <stdio.h> #include <string.h> #include <stdlib.h> #inclu ...
 - 7.12 Varnish体系结构
			
备注:应用比较小,采用的架构模式 Varnish + 基本业务功能 但是一个问题是所有的资源在一台服务器上,反向代理特别多,缓存数据特别大,导致一台机器资源不够,考虑机器的拆分 Nginx 的反向代 ...
 - 寒假所做事情日志-Office重新激活
			
日期:2020.01.18 博客期:127 星期六 好吧,今天出了一趟远门,将近傍晚才回来.任务目标其实相当于什么也没做,但回来发现Office居然过期了,老师给的那些文件居然无法修改了,于是乎剩下的 ...
 - Python学习第七课——集合(set) 和 字符串拼接
			
集合(set) # 2 无序 # 3 集合中元素必须是不可变类型 # 定义集合 s = {1,2,3,4,5} print(s) # 输出结果 {1, 2, 3, 4, 5} # 1 集合由不同元素组 ...
 - java程序中的经常出现的的异常处理课后总结
			
一.JDK中常见的异常情况 1.常见异常总结图 2.java中异常分类 Throwable类有两个直接子类: (1)Exception:出现的问题是可以被捕获的 (2)Error:系统错误,通常由JV ...
 - css  盒子模型简介
			
盒子模型 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...