java1.7集合源码阅读: Stack
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的更多相关文章
- java1.7集合源码阅读: Vector
Vector是List接口的另一实现,有非常长的历史了,从jdk1.0开始就有Vector了,先于ArrayList出现,与ArrayList的最大区别是:Vector 是线程安全的,简单浏览一下Ve ...
- 关于java1.7集合源码阅读
工作中每天都会和java集合打交道,虽然以前也看过jdk源码的实现,但有些东西时间长了还是会遗忘,或者有些实现在新版本中有了新的变化,俗话说"温故而知新",所以打算再阅读一下相关源 ...
- java1.7集合源码阅读:LinkedList
先看看类定义: public class LinkedList<E> extends AbstractSequentialList<E> implements List< ...
- java1.7集合源码阅读:ArrayList
ArrayList是jdk1.2开始新增的List实现,首先看看类定义: public class ArrayList<E> extends AbstractList<E> i ...
- java1.7集合源码阅读:ArrayBlockingQueue
ArrayBlockingQueue是一个先进先出线程安全的队列,队列头部是进入队列时间最长的元素,队尾是进入队列时间最短的元素,同时队列的最大容量是固定的. 先看类定义: public class ...
- 【JDK1.8】JDK1.8集合源码阅读——IdentityHashMap
一.前言 今天我们来看一下本次集合源码阅读里的最后一个Map--IdentityHashMap.这个Map之所以放在最后是因为它用到的情况最少,也相较于其他的map来说比较特殊.就笔者来说,到目前为止 ...
- 【JDK1.8】JDK1.8集合源码阅读——总章
一.前言 今天开始阅读jdk1.8的集合部分,平时在写项目的时候,用到的最多的部分可能就是Java的集合框架,通过阅读集合框架源码,了解其内部的数据结构实现,能够深入理解各个集合的性能特性,并且能够帮 ...
- 【JDK1.8】JDK1.8集合源码阅读——HashMap
一.前言 笔者之前看过一篇关于jdk1.8的HashMap源码分析,作者对里面的解读很到位,将代码里关键的地方都说了一遍,值得推荐.笔者也会顺着他的顺序来阅读一遍,除了基础的方法外,添加了其他补充内容 ...
- 【JDK1.8】JDK1.8集合源码阅读——ArrayList
一.前言 在前面几篇,我们已经学习了常见了Map,下面开始阅读实现Collection接口的常见的实现类.在有了之前源码的铺垫之后,我们后面的阅读之路将会变得简单很多,因为很多Collection的结 ...
随机推荐
- Spring异常累计(1)Spring注解与扫描,NoUniqueBeanDefinitionException
spring中可以使用注解机制,代替传统的在xml中配置一个bean. 如 <pre name="code" class="java">@Compo ...
- GridView 控件中如何绑定 CheckBoxList
需求:设计这样一个页面,在页面上可以选择和展示各省份对应的文明城市? 思路:一次性查询出所需的记录(查询数据库的操作不宜写到 C# 代码的循环语句中),并保存到全局变量中,之后根据条件过滤出需要的.可 ...
- java common-io jar API
import org.apache.commons.beanutils.BeanUtils;public class Person { private String name; public Stri ...
- 在C#中使用C++编写的类
现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况下,有很多开发人员就面临了如何在C#中使用C++开发好的类 ...
- 用DotNetBar设计的 Gradient Buttons 漂亮按钮
http://www.webdesignerwall.com/demo/css-buttons.html public class GradientButtons : DevComponents ...
- linux 下 文件权限和文件主
文件与文件夹的权限和所有者 1.chmod -R 755 file 777 含义与来源: 777含义:分别为:所有者.同组用户.其他用户 7的来源:文件有三种操作模式:读4.写2.执行1,分别值为42 ...
- 【MySQL】主备复制
复制对于mysql的重要性不言而喻,mysql集群的负载均衡,读写分离和高可用都是基于复制实现.下文主要从4个方面展开,mysql的异步复制,半同步复制和并行复制,最后会简单聊下第三方复制工具.由于生 ...
- Linux free命令详解(转)
解释一下Linux上free命令的输出.(转自:http://www.cnblogs.com/coldplayerest/archive/2010/02/20/1669949.html) 下面是fre ...
- TNS-12547 Linux Error: 104: Connection reset by pe (转载)
TNS-12547 Linux Error: 104: Connection reset by peer 解决过程参考:http://blog.chinaunix.net/u/7121/showart ...
- 解决:jquery ajax非首次请求Server端获取cookie值中文乱码问题
HttpCookie cookie = new HttpCookie("RealName", HttpUtility.UrlEncode("你想要设置的值")) ...