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方法说明)

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.util.Stack不被官方所推荐使用!

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

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

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

  7. 用 LinkedList 实现一个 java.util.Stack 栈

    用 LinkedList 实现一个 java.util.Stack 栈 import java.util.LinkedList; public class Stack<E> { priva ...

  8. java集合类——Stack类

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

  9. Java的Stack类实现List接口真的是个笑话吗

        今天在网上闲逛时看到了这样一个言论,说“Java的Stack类实现List接口的设计是个笑话”.   当然作者这篇文章的重点不是这个,原本我也只是一笑置之,然而看评论里居然还有人附和,说“Ja ...

随机推荐

  1. C#函数库

    //读取文本文件并返回内容不同的那一行         public static String different(String sOldFile, String sNewFile)         ...

  2. steam账号分享工具、迅游账号分享工具说明:

    steam账号分享工具.迅游账号分享工具说明: 用于网吧经营者将自己购买的迅游加速器账号或者是steam平台账号免费分享给有需要的网吧顾客使用. steam账号分享工具.迅游账号分享工具由“服务端”和 ...

  3. 限时购校验小工具&dubbo异步调用实现限

    本文来自网易云社区 作者:张伟 背景 限时购是网易考拉目前比较常用的促销形式,但是前期创建一个限时购活动时需要各个BU按照指定的Excel格式进行选品提报,为了保证提报数据准确,运营需要人肉校验很多信 ...

  4. Android 9 Pie震撼来袭 同步登陆WeTest

    WeTest 导读 2018年8月7日,Google对外发布最新 Android 9.0 正式版系统,并宣布系统版本Android P 被正式命名为代号“Pie”,最新系统已经正式推送包括谷歌Pixe ...

  5. Qt-QML-C++交互实现文件IO系统-后继-具体文件IO的实现

    在上一篇文章中,我大致将这个QML中的文件IO类搭出了大致的框架,那么,今天抽时间写了一点文件的读写,这里我使用的文件流来读写文件. 文件结构如图 在QML中调用这个类了,就见简单的读取了一个JSON ...

  6. JVM--Java类加载机制

    一.什么是类的加载 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其存放在运行时数据区的方法区内,然后在java堆区创建一个java.lang.Class对象,用来封装类在方法区内 ...

  7. 孤荷凌寒自学python第八十三天初次接触ocr配置tesseract环境

    孤荷凌寒自学python第八十三天初次接触ocr配置tesseract环境 (完整学习过程屏幕记录视频地址在文末) 学习Python我肯定不会错过图片文字的识别,当然更重要的是简单的验证码识别了,今天 ...

  8. leetcode7_C++整数反转

      给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 输出:  示例 2: 输入: - 输出: - 示例 3: 输入: 输出: 注意: 假设我们的环境只能存 ...

  9. js经典试题之w3规范系列

    js经典试题之w3规范系列 1:w3c 制定的 javascript 标准事件模型的正确的顺序? 答案:事件捕获->事件处理->事件冒泡 解析:先事件捕获从windows > doc ...

  10. Daily Scrum 10

    今天我们小组开会内容分为以下部分: part 1: 经过反复思考,对于上次组会确定的在系统中加入娱乐版块进行了更进一步的商讨; part 2:继续探讨算法实现: part 3:进行明日的任务分配; ◆ ...