Stack类也是List接口的一种实现,也是一个有着非常长历史的实现,从jdk1.0开始就有了这个实现。

Stack是一种基于后进先出队列的实现(last-in-first-out (LIFO)),实际上jdk也提供了有关队列的其他实现,这里就先看看Stack的实现:

类定义:

public class Stack<E> extends Vector<E> { //从类定义看,Stack是线程安全的
.....
}

看看Stack提供的一些CRUD方法:

    /**
* 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) { // push方法是在队尾增加一个元素
addElement(item); return item;
} /**
* Adds the specified component to the end of this vector,
* increasing its size by one. The capacity of this vector is
* increased if its size becomes greater than its capacity.
*
* <p>This method is identical in functionality to the
* {@link #add(Object) add(E)}
* method (which is part of the {@link List} interface).
*
* @param obj the component to be added
*/
public synchronized void addElement(E obj) { //Vector中的方法
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}

重点注意一下pop方法:

     /**
* 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).
* @throws EmptyStackException if this stack is empty.
*/
public synchronized E pop() { //pop方法是获取并删除队尾的元素
E obj;
int len = size(); obj = peek(); //见后续peek()方法
removeElementAt(len - 1); return obj;
}
/**
* Deletes the component at the specified index. Each component in
* this vector with an index greater or equal to the specified
* {@code index} is shifted downward to have an index one
* smaller than the value it had previously. The size of this vector
* is decreased by {@code 1}.
*
* <p>The index must be a value greater than or equal to {@code 0}
* and less than the current size of the vector.
*
* <p>This method is identical in functionality to the {@link #remove(int)}
* method (which is part of the {@link List} interface). Note that the
* {@code remove} method returns the old value that was stored at the
* specified position.
*
* @param index the index of the object to remove
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
public synchronized void removeElementAt(int index) { //Vector中的方法,删除指定index元素
modCount++;
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
elementCount--;
elementData[elementCount] = null;
}

Stack 的peek方法,只是获取元素,但并不会去做删除处理,当队列中没有元素的时候,会报EmptyStackException异常:

    public synchronized E peek() {
int len = size(); if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}

Stack 比较简单,大部分实现都在Vector中,可参考:java1.7集合源码阅读: Vector

java1.7集合源码阅读: Stack的更多相关文章

  1. java1.7集合源码阅读: Vector

    Vector是List接口的另一实现,有非常长的历史了,从jdk1.0开始就有Vector了,先于ArrayList出现,与ArrayList的最大区别是:Vector 是线程安全的,简单浏览一下Ve ...

  2. 关于java1.7集合源码阅读

    工作中每天都会和java集合打交道,虽然以前也看过jdk源码的实现,但有些东西时间长了还是会遗忘,或者有些实现在新版本中有了新的变化,俗话说"温故而知新",所以打算再阅读一下相关源 ...

  3. java1.7集合源码阅读:LinkedList

    先看看类定义: public class LinkedList<E> extends AbstractSequentialList<E> implements List< ...

  4. java1.7集合源码阅读:ArrayList

    ArrayList是jdk1.2开始新增的List实现,首先看看类定义: public class ArrayList<E> extends AbstractList<E> i ...

  5. java1.7集合源码阅读:ArrayBlockingQueue

    ArrayBlockingQueue是一个先进先出线程安全的队列,队列头部是进入队列时间最长的元素,队尾是进入队列时间最短的元素,同时队列的最大容量是固定的. 先看类定义: public class ...

  6. 【JDK1.8】JDK1.8集合源码阅读——IdentityHashMap

    一.前言 今天我们来看一下本次集合源码阅读里的最后一个Map--IdentityHashMap.这个Map之所以放在最后是因为它用到的情况最少,也相较于其他的map来说比较特殊.就笔者来说,到目前为止 ...

  7. 【JDK1.8】JDK1.8集合源码阅读——总章

    一.前言 今天开始阅读jdk1.8的集合部分,平时在写项目的时候,用到的最多的部分可能就是Java的集合框架,通过阅读集合框架源码,了解其内部的数据结构实现,能够深入理解各个集合的性能特性,并且能够帮 ...

  8. 【JDK1.8】JDK1.8集合源码阅读——HashMap

    一.前言 笔者之前看过一篇关于jdk1.8的HashMap源码分析,作者对里面的解读很到位,将代码里关键的地方都说了一遍,值得推荐.笔者也会顺着他的顺序来阅读一遍,除了基础的方法外,添加了其他补充内容 ...

  9. 【JDK1.8】JDK1.8集合源码阅读——ArrayList

    一.前言 在前面几篇,我们已经学习了常见了Map,下面开始阅读实现Collection接口的常见的实现类.在有了之前源码的铺垫之后,我们后面的阅读之路将会变得简单很多,因为很多Collection的结 ...

随机推荐

  1. 如何取Android设备日志

    安装Android SDK 运行 adb 命令 adb devices 查看链接的设备 adb logcat 日志相关

  2. IOS5中的Safari不兼容Javascript中的Date问题

    在IOS5以上版本(不包含IOS5)中的Safari浏览器能正确解释出Javascript中的 new Date('2016-06-07') 的日期对象. 但是在IOS5版本里面的Safari解释ne ...

  3. 数据库之mysql存储程序

    什么时候会用到存储过程 1.存储过程只在创造时进行编译,以后每次执行存储过程都不需再重新编译,而一般 SQL 语句每执行一次就编译一次,所以使用存储过程可提高数据库执行速度2.当对数据库进行复杂操作时 ...

  4. sqlserver 跨服务器访问数据

    需求:两个一模一样的表,分别分布在两个服务器的数据库上,现在要在一个表中,查看这两个表的内容,并让Id排序 1:在本地数据库查询分析器中,运行以下两段语句: --创建链接服务器 exec sp_add ...

  5. 使用pt-stalk分析MySQL的性能波动 (转)

    简介 在MySQL服务器出现短暂(5~30秒)的性能波动的时候,一般的性能监控工具都很难抓住故障现场,也就很难收集对应较细粒度的诊断信息.另外,如果这种波动出现的频率很低,例如几天才一次,我们也很难人 ...

  6. SQL总结(三)其他查询

    SQL总结(三)其他查询 其他常用的SQL,在这里集合. 1.SELECT INTO 从一个表中选取数据,然后把数据插入另一个表中.常用于创建表的备份或者用于对记录进行存档. 语法: SELECT c ...

  7. struts1 Demo

    每次都会忘记一些东西,反复查找原因,其实struts1很简单,可是不去巩固也很容易忘记并且犯错误.这是一个最简单的登录Demo. 1.建立web工程,引入struts1.2包 2.建package:a ...

  8. Shell脚本中执行sql语句操作mysql

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  9. web中session与序列化的问题

    最近在写网上商城项目的时候学习了一个关于session的序列化问题,过来总结一下. 众所周知,session是服务器端的一种会话技术,只要session没有关闭,一个会话就会保持.这里先引出一个问题: ...

  10. silverlight简单数据绑定1

    数据绑定是用户界面与数据源之间的媒介:通过绑定可以使数据在界面和数据源之间传递交流.数据绑定由System.Windows.Data命名空间的Binding对象完成. 创建绑定的数据对象类. .cs类 ...