java.util.Stack类简介
Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来
Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用
E push(E item)
把项压入堆栈顶部。
E pop()
移除堆栈顶部的对象,并作为此函数的值返回该对象。
E peek()
查看堆栈顶部的对象,但不从堆栈中移除它。
boolean empty()
测试堆栈是否为空。
int search(Object o)
返回对象在堆栈中的位置,以 1 为基数。
Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组( a growable array of objects)那么这个数组的哪里作为Stack的栈顶,哪里作为Stack的栈底?
答案只能从源代码中寻找,jdk1.6:
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).
* @exception 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).
* @exception 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;
}
通过注释The object at the top of this stack (the last item of the Vector object,可以发现数组(Vector)的最后一位即为Stack的栈顶
pop、peek以及search方法本身进行了同步
push方法调用了父类的addElement方法
empty方法调用了父类的size方法
Vector类为线程安全类
综上,Stack类为线程安全类(多个方法调用而产生的数据不一致问题属于原子性问题的范畴)
public class Test {
public static void main(String[] args) {
Stack<String> s = new Stack<String>();
System.out.println("------isEmpty");
System.out.println(s.isEmpty());
System.out.println("------push");
s.push("1");
s.push("2");
s.push("3");
Test.it(s);
System.out.println("------pop");
String str = s.pop();
System.out.println(str);
Test.it(s);
System.out.println("------peek");
str = s.peek();
System.out.println(str);
Test.it(s);
System.out.println("------search");
int i = s.search("2");
System.out.println(i);
i = s.search("1");
System.out.println(i);
i = s.search("none");
System.out.println(i);
}
public static void it(Stack<String> s){
System.out.print("iterator:");
Iterator<String> it = s.iterator();
while(it.hasNext()){
System.out.print(it.next()+";");
}
System.out.print("\n");
}
}
结果:
------isEmpty
true
------push
iterator:1;2;3;
------pop
3 --栈顶是数组最后一个
iterator:1;2;
------peek
2 --pop取后删掉,peek只取不删
iterator:1;2;
------search
1 --以1为基数,即栈顶为1
2 --和栈顶见的距离为2-1=1
-1 --不存在于栈中
Stack并不要求其中保存数据的唯一性,当Stack中有多个相同的item时,调用search方法,只返回与查找对象equal并且离栈顶最近的item与栈顶间距离(见源码中search方法说明)
java.util.Stack类简介的更多相关文章
- 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.util.Stack类中 empty() 和 isEmpty() 方法的作用
最近在学习算法和数据结构,用到Java里的Stack类,但程序运行结果一直和我预料的不一样,网上也没查清楚,最后查了API,才搞明白. java.util.Stack继承类 java.util.Vec ...
- Java中的栈:java.util.Stack类
public class Stack<E>extends Vector<E>Stack 类表示后进先出(LIFO)的对象堆栈.它通过五个操作对类 Vector 进行了扩展 ,允 ...
- java集合类——Stack类
查看java的API文档,Stack继承Vector类. 栈的特点是后进先出. API中Stack自身的方法不多,基本跟栈的特点有关. import java.util.Stack; public c ...
- java.util.Stack(栈)的简单使用
import java.util.Stack; import org.junit.Before; import org.junit.Test; /** * Stack(栈)继承了Vector类,底层实 ...
- Java实现Stack类
Java实现Stack类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Sc ...
- Java.util.ArrayDeque类
java.util.ArrayDeque 类提供了可调整大小的阵列,并实现了Deque接口.以下是关于阵列双端队列的要点: 数组双端队列没有容量限制,使他们增长为必要支持使用. 它们不是线程安全的;如 ...
- 为什么 java.util.Stack不被官方所推荐使用!
Java 为什么不推荐使用 Stack 呢? 因为 Stack 是 JDK 1.0 的产物.它继承自 Vector,Vector 都不被推荐使用了,你说 Stack 还会被推荐吗? 当初 JDK1.0 ...
随机推荐
- 六月计划#1A(6.1-6.8)
5/35 数学 BZOJ_[HNOI2008]_Cards_(置换+Burnside引理+乘法逆元+费马小定理+快速幂) BZOJ_1005_ [HNOI2008]_明明的烦恼_(组合数学+purfe ...
- Linux kernel 拒绝服务漏洞
漏洞名称: Linux kernel 拒绝服务漏洞 CNNVD编号: CNNVD-201311-020 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等级: 漏洞类型: ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
- LoadRunner 你不知道的事之——内存使用
LoadRunner的使用相信大家很熟悉,但是可能很少有人去关注一个Vuser 在以线程模式和进程模式下的内存开销情况,下面通过个人的试验得出一组数据供大家参考,只有你真正了解了,才能做的更深入. 测 ...
- [TSOI2005]Exhibit
问题描述 博览馆正在展出由世上最佳的 M 位画家所画的图画. wangjy 想到博览馆去看这几位大师的作品. 可是,那里的博览馆有一个很奇怪的规定,就是在购买门票时必须说明两个数字, a 和 b ,代 ...
- Fatjar成功安装记录
1.FatJar安装方式 (1)在线安装 具体网址http://kurucz-grafika.de/fatjar (2)离线安装 将fatjar的jar放到plugins文件夹中,重启 2. 安装失败 ...
- 五指CMS发布1.4版本,更多的新功能
五指cms v1.4变更: 新增内容手动分页新增百度地图新增订单管理模块新增订单地址管理增加Microsoft YaHei字体新增推广邀请模块新增私密下载,下载函数 新增百度地图新增筛选功能 修正全局 ...
- VellCar(barracuda buggy)
这是我的新目标,不过是外国人设计的,youtube上好多相关视频,只是在中国被墙了,FQ能看到一个不一样的世界,附:FQ教程.国外好多玩车的都是选择这辆车,因为它结构紧凑,制作相对简单,但越野性能强悍 ...
- Storm系列(二)系统结构及重要概念
在Storm的集群里面有两种节点:控制节点和工作节点,控制节点上面运行Nimbus进程,Nimbus负责在集群里面分配计算任务,并且监控状态.每一个工作节点上面运行Supervisor进程,Super ...
- 【Java基础】增强for循环要注意陷阱
什么是增强for循环 增强for循环是一种简单模式的for循环,为了方便数组和集合的遍历而存在. int[] arr = new int[]{1, 2, 3, 4, 5, 6}; for (int a ...