转载自  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;
}  

通过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 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类简介相关推荐

  1. 为什么子孙后代会讨厌使用java.util.Stack

    在我用无意义的重言式杀死你之前,这是要点 如果您的应用程序接近实时,或者将代码发送到Mars,则需要保留Java中默认的Stack实现. 根据LinkedList编写您自己的版本. 同样,如果您的应用 ...

  2. Java的Stack类

    Java的Stack类 1.Stack类在java.util包中: 2.继承关系 Stack继承了Vector,而Vector类底层使用数组存储数据,那么Stack对象中存储的数据也是存储在数组中的. ...

  3. JDK中提供的实现——通过 java.util.Observable 类和 java.util.Observer 接口定义了观察者模式,只要实现它们的子类就可以编写观察者模式实例

    JDK中提供的实现 在 Java 中,通过 java.util.Observable 类和 java.util.Observer 接口定义了观察者模式,只要实现它们的子类就可以编写观察者模式实例. 1 ...

  4. java的知识点17——java.util.Arrays类、多维数组

    java.util.Arrays类 Arrays类包含了:排序.查找.填充.打印内容等常见的操作. 打印数组 package cn.dym08; import java.util.Arrays;pub ...

  5. Arrays.asList()返回的ArrayList,这是Arrays里内嵌的一个私有静态类,而并不是java.util.ArrayList类

    测试代码: package array;import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; ...

  6. java集合——java.util.Properties类

    [0]README 0.1)以下全文转自 : http://trans.blog.51cto.com/503170/110227/ [1]认识properties文件 1. properties文件是 ...

  7. java.util.Random 类的 nextInt(int num )

    随机产生3个67~295的整数并找出数值居中的数 并输出中间的数 例如:100,225和200,输出200 要随机产生某个范围内的整数,用 java.util.Random 类的 nextInt(in ...

  8. java.util 1.8_JDK1.8源码(四)——java.util.Arrays 类

    java.util.Arrays 类是 JDK 提供的一个工具类,用来处理数组的各种方法,而且每个方法基本上都是静态方法,能直接通过类名Arrays调用. 1.asList public static ...

  9. java.util.zip 类 ZipEntry

    java.util.zip 类 ZipEntry java.lang.Objectjava.util.zip.ZipEntry 所有已实现的接口: Cloneable 直接已知子类: JarEntry ...

最新文章

  1. ERP系统为什么要使用集成的数据和信息平台?
  2. 怎么给当前点击的a标签添加一个样式(跳转页面后)
  3. JavaScript继承
  4. python居中对齐符号怎么打_Python字符串居中对齐
  5. android中的多媒体应用camera
  6. 和大家分享2015年我逐步形成的六个管理认识
  7. 黑苹果怎么进recovery_黑苹果怎么自定义Clover GUI启动选项 黑苹果修复Clover GUI启动选项教程...
  8. html幻灯片图片切换效果代码,javascript实现图片切换的幻灯片效果源代码
  9. 我的世界启动器java_我的世界最新启动器 无需安装JAVA
  10. 经纬财富:昭通炒现货白银技巧
  11. csgo显示连接任何官方服务器失败,csgo连接任意官方服务器失败怎么办_csgo连接官方服务器失败解决方法...
  12. 八爪鱼导出到mysql数据库_八爪鱼采集器怎么将数据导出数据库?
  13. 永久禁用software reporter 进程,占用高解决方法。【永久禁用software_reporter_tool.exe程序】
  14. Spark案例之流量统计(三种方法)
  15. 追梦算法网----团队数量
  16. ipv6 dns修改方法
  17. 大无语!!记录一下mac dpabi 安装报错:未定义函数或变量 ‘dpabi‘。Chrome浏览器!!!
  18. 我是怎么看friends练口语的(转贴)
  19. <Linux>Ubuntu使用glade创建gtk UI应用程序
  20. 【智猪博弈 Pigs‘ payoffs】

热门文章

  1. 7-24 树种统计 (25 分)(详解)map做法 map真香啊!
  2. C++ 学习之旅(8)——一文搞懂指针、引用、函数参数的传值调用、指针调用和引用调用
  3. HTML和css重要的知识点,html 和 css 基础知识点(一)(示例代码)
  4. [MyBatisPlus]通用Service接口测试通用Service
  5. Java面向对象编程(基础部分)
  6. 高等数学下-赵立军-北京大学出版社-题解-练习12.1
  7. LeetCode 143 重排链表-中等
  8. lua split实现(lua程序设计10.6练习10.1题)
  9. linux apache找不到woff2,使服务器Nginx(或者Apache)支持woff2等字体文件
  10. java的hr样式,spring+spring mvc+mybatis开发java医院HR人事管理系统(加强版本+ shiro)...