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;
}

通过peek()方法注释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 StackTest {
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");
StackTest.t(s);
System.out.println("------pop");
String str = s.pop();
System.out.println(str);
StackTest.t(s);
System.out.println("------peek");
str = s.peek();
System.out.println(str);
StackTest.t(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 t(Stack<String> s){
System.out.println("iterator");
Iterator<String> it = s.iterator();
while(it.hasNext()){
System.out.print(it.next()+" ");
}
System.out.println("\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方法说明)

最新文章

  1. IOS中CocoaPods安装与使用
  2. 问题解决——使用GP-3120TN打印条形码标签
  3. 当As3遇见Swift(三)
  4. The Blacksmith学习的相关资源
  5. 菜鸟学sql,Oracle数据库结构比较
  6. 信驰达携“Zigbee Light Link灯控方案”亮相第18届广州国际照明展
  7. Volley : &quot;参数param:{ inoutNo:inoutNo ,whcode:’’}
  8. git 修改commit信息
  9. VMWare-NAT模式实现局域网其他主机对虚拟机访问
  10. NOSQL之【WIN7的安装配置】
  11. AJAX中文乱码PHP完美解决(IE和Firefox兼容)
  12. poj1915 BFS
  13. MyEclipse 2013 导入MyEclipse 9.0的EJB项目时,需要注意
  14. (三)ajax请求不同源之nginx反向代理跨域
  15. HDU 3948 The Number of Palindromes(Manacher+后缀数组)
  16. sql常用问题(一)
  17. Bootstrap的插件
  18. 廖雪峰Java5集合-3Map-1使用Map
  19. 20145307陈俊达_安卓逆向分析_Xposed的hook技术研究
  20. centos jdk 下载

热门文章

  1. mysql 优化笔记
  2. PHP MySQL 安全方案
  3. libevent学习七(bufferevent)
  4. node环境清空控制台的代码
  5. python+selenium 环境配置
  6. requests,lxml爬启信宝
  7. Linux命令应用大词典-第23章 进程和服务管理
  8. TPO-14 C1 Locate a political book
  9. &lt;cerrno&gt;
  10. unity发布自定义分辨率