Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来

Deque(双端队列)比起Stack具有更好的完整性和一致性,应该被优先使用

  1. E push(E item)
  2. 把项压入堆栈顶部。
  3. E pop()
  4. 移除堆栈顶部的对象,并作为此函数的值返回该对象。
  5. E peek()
  6. 查看堆栈顶部的对象,但不从堆栈中移除它。
  7. boolean empty()
  8. 测试堆栈是否为空。
  9. int search(Object o)
  10. 返回对象在堆栈中的位置,以 1 为基数。

Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组( a growable array of objects)那么这个数组的哪里作为Stack的栈顶,哪里作为Stack的栈底?

答案只能从源代码中寻找,jdk1.6:

  1. public class Stack<E> extends Vector<E> {
  2. /**
  3. * Creates an empty Stack.
  4. */
  5. public Stack() {
  6. }
  7.  
  8. /**
  9. * Pushes an item onto the top of this stack. This has exactly
  10. * the same effect as:
  11. * <blockquote><pre>
  12. * addElement(item)</pre></blockquote>
  13. *
  14. * @param item the item to be pushed onto this stack.
  15. * @return the <code>item</code> argument.
  16. * @see java.util.Vector#addElement
  17. */
  18. public E push(E item) {
  19. addElement(item);
  20.  
  21. return item;
  22. }
  23.  
  24. /**
  25. * Removes the object at the top of this stack and returns that
  26. * object as the value of this function.
  27. *
  28. * @return The object at the top of this stack (the last item
  29. * of the <tt>Vector</tt> object).
  30. * @exception EmptyStackException if this stack is empty.
  31. */
  32. public synchronized E pop() {
  33. E obj;
  34. int len = size();
  35.  
  36. obj = peek();
  37. removeElementAt(len - 1);
  38.  
  39. return obj;
  40. }
  41.  
  42. /**
  43. * Looks at the object at the top of this stack without removing it
  44. * from the stack.
  45. *
  46. * @return the object at the top of this stack (the last item
  47. * of the <tt>Vector</tt> object).
  48. * @exception EmptyStackException if this stack is empty.
  49. */
  50. public synchronized E peek() {
  51. int len = size();
  52.  
  53. if (len == 0)
  54. throw new EmptyStackException();
  55. return elementAt(len - 1);
  56. }
  57.  
  58. /**
  59. * Tests if this stack is empty.
  60. *
  61. * @return <code>true</code> if and only if this stack contains
  62. * no items; <code>false</code> otherwise.
  63. */
  64. public boolean empty() {
  65. return size() == 0;
  66. }
  67.  
  68. /**
  69. * Returns the 1-based position where an object is on this stack.
  70. * If the object <tt>o</tt> occurs as an item in this stack, this
  71. * method returns the distance from the top of the stack of the
  72. * occurrence nearest the top of the stack; the topmost item on the
  73. * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
  74. * method is used to compare <tt>o</tt> to the
  75. * items in this stack.
  76. *
  77. * @param o the desired object.
  78. * @return the 1-based position from the top of the stack where
  79. * the object is located; the return value <code>-1</code>
  80. * indicates that the object is not on the stack.
  81. */
  82. public synchronized int search(Object o) {
  83. int i = lastIndexOf(o);
  84.  
  85. if (i >= 0) {
  86. return size() - i;
  87. }
  88. return -1;
  89. }
  90.  
  91. /** use serialVersionUID from JDK 1.0.2 for interoperability */
  92. private static final long serialVersionUID = 1224463164541339165L;
  93. }

通过注释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类为线程安全类(多个方法调用而产生的数据不一致问题属于原子性问题的范畴)

  1. public class Test {
  2. public static void main(String[] args) {
  3. Stack<String> s = new Stack<String>();
  4. System.out.println("------isEmpty");
  5. System.out.println(s.isEmpty());
  6. System.out.println("------push");
  7. s.push("1");
  8. s.push("2");
  9. s.push("3");
  10. Test.it(s);
  11. System.out.println("------pop");
  12. String str = s.pop();
  13. System.out.println(str);
  14. Test.it(s);
  15. System.out.println("------peek");
  16. str = s.peek();
  17. System.out.println(str);
  18. Test.it(s);
  19. System.out.println("------search");
  20. int i = s.search("2");
  21. System.out.println(i);
  22. i = s.search("1");
  23. System.out.println(i);
  24. i = s.search("none");
  25. System.out.println(i);
  26. }
  27.  
  28. public static void it(Stack<String> s){
  29. System.out.print("iterator:");
  30. Iterator<String> it = s.iterator();
  31. while(it.hasNext()){
  32. System.out.print(it.next()+";");
  33. }
  34. System.out.print("\n");
  35. }
  36. }

结果:

  1. ------isEmpty
  2. true
  3. ------push
  4. iterator:1;2;3;
  5. ------pop
  6. 3 --栈顶是数组最后一个
  7. iterator:1;2;
  8. ------peek
  9. 2 --pop取后删掉,peek只取不删
  10. iterator:1;2;
  11. ------search
  12. 1 --以1为基数,即栈顶为1
  13. 2 --和栈顶见的距离为2-1=1
  14. -1 --不存在于栈中

Stack并不要求其中保存数据的唯一性,当Stack中有多个相同的item时,调用search方法,只返回与查找对象equal并且离栈顶最近的item与栈顶间距离(见源码中search方法说明)

java.util.Stack类简介的更多相关文章

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

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

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

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

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

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

  4. Java中的栈:java.util.Stack类

    public class Stack<E>extends Vector<E>Stack 类表示后进先出(LIFO)的对象堆栈.它通过五个操作对类 Vector 进行了扩展 ,允 ...

  5. java集合类——Stack类

    查看java的API文档,Stack继承Vector类. 栈的特点是后进先出. API中Stack自身的方法不多,基本跟栈的特点有关. import java.util.Stack; public c ...

  6. java.util.Stack(栈)的简单使用

    import java.util.Stack; import org.junit.Before; import org.junit.Test; /** * Stack(栈)继承了Vector类,底层实 ...

  7. Java实现Stack类

    Java实现Stack类 import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Sc ...

  8. Java.util.ArrayDeque类

    java.util.ArrayDeque 类提供了可调整大小的阵列,并实现了Deque接口.以下是关于阵列双端队列的要点: 数组双端队列没有容量限制,使他们增长为必要支持使用. 它们不是线程安全的;如 ...

  9. 为什么 java.util.Stack不被官方所推荐使用!

    Java 为什么不推荐使用 Stack 呢? 因为 Stack 是 JDK 1.0 的产物.它继承自 Vector,Vector 都不被推荐使用了,你说 Stack 还会被推荐吗? 当初 JDK1.0 ...

随机推荐

  1. URAL1012. K-based Numbers. Version 2

    链接 考查大数 正好拿来学习下JAVA JAVA好高端.. import java.io.*; import java.math.*; import java.text.*; import java. ...

  2. Hibernate包及相关工具包下载地址

    Hibernate包及相关工具包下载地址: http://prdownloads.sourceforge.net/hibernate/ 这里包含所有hibernate各个版本的包下载,且提供了 Mid ...

  3. WordPress Suco Themes ‘themify-ajax.php’任意文件上传漏洞

    漏洞名称: WordPress Suco Themes ‘themify-ajax.php’任意文件上传漏洞 CNNVD编号: CNNVD-201311-403 发布时间: 2013-11-28 更新 ...

  4. ☀【JS】eval

    避免使用 eval <!doctype html> <html lang="zh-CN"> <head> <meta charset=&q ...

  5. Hadoop datanode无法启动的错误

    在启动Hadoop分布式部署的过程中,发现datanode没有正常启动,查看了一下日志发现报错: java.io.IOException: Incompatible clusterIDs in /op ...

  6. 关于在SharePoint2013中弹出模态化窗口的问题及关闭事件。

    js: /*弹出对话框方法开始*//** 弹窗方法(需要回传值时,自定义回调方法)* @url: 弹出窗口页面url* @width: 宽度* @height: 高度* @callback: 回调函数 ...

  7. 关于iTerm和Zsh

    关于iTerm和Zsh 终于我还是踏入了Zsh的行列,时间有点紧张,要开始做毕设了,关于Zsh和iTerm的好处我就不多说了哈,以后有时间再聊哈 Installation install Zsh 使用 ...

  8. Msys下gcc的配置

    打开文件/etc/profile,添加如下路径, C_INCLUDE_PATH=/e/msys/1.0/include export C_INCLUDE_PATH CPLUS_INCLUDE_PATH ...

  9. Linux经久不衰的应用程序

    Linux里面的应用程序一贯以高安全性,高性价比(功能/所占空间),此次记录一下Linux里面比较常用的而且经久不衰的应用程序. Shell:               bash(它结合了 csh ...

  10. Java 常见异常及趣味解释

    java.lang ArithmeticException 你正在试图使用电脑解决一个自己解决不了的数学问题,请重新阅读你的算术表达式并再次尝试. ArrayIndexOutOfBoundsExcep ...