stack源码分析

简介:
## 一、简介
![stack类图.png](http://upload-images.jianshu.io/upload_images/1541350-5fe08669097e7ac0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

栈是数据结构中一种很重要的数据结构类型,因为栈的后进先出功能是实际的开发中有很多的应用场景。Java API中提供了栈(Stacck)的实现。Stack类继承了Vector类,而Vector类继承了AbstractList抽象类,实现了List类,Cloneable接口,RandomAcces接口以及Serializable接口。
## 二、源码阅读
#### 1.构造方法
```java
public Stack() {
}
```
创建一个空栈。
#### 2.入栈push
```java
public E push(E item) {
    addElement(item);
    return item;
}
```
```java
public synchronized void addElement(E obj) {
    modCount++;
    ensureCapacityHelper(elementCount + 1);
    elementData[elementCount++] = obj;
}
```
入栈是一个同步的方法,调用Vector的**addElement**方法,也是一个同步方法,先将修改次数加一,之后调用**ensureCapacityHelper**确认数组有足够的空间能够容纳新的元素。最后将元素新增到数组,即Vector的末尾。
#### 3.出栈pop
```java
public synchronized E pop() {
    E       obj;
    int     len = size();
    obj = peek();
    removeElementAt(len - 1);

    return obj;
}
```
出栈同样是一个同步方法,先定义一个泛型对象obj,获取到数组长度len,然后调用**peek()**方法,获取栈顶的元素赋值给obj,然后删除栈顶元素。
```java
public synchronized E peek() {
    int     len = size();

    if (len == 0)
        throw new EmptyStackException();
    return elementAt(len - 1);
}
```
很显然,peek()方法直接调用了Vector的elementAt方法,该方法不删除栈顶的元素。
#### 4.判断栈是否为空
```java
/**
 * 通过数组长度判断栈是否为空。
 *
 * @return  <code>true</code> if and only if this stack contains
 *          no items; <code>false</code> otherwise.
 */
public boolean empty() {
    return size() == 0;
}
```
#### 5.查询元素到栈顶的距离
```java
/**
 * 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;
}
```
一个同步方法,找到指定元素o到栈顶的距离,可以看到用到了lastIndexOf方法,如果找不到元素,则返回-1。
## 三、总计
通过源码我们可以看到Vector底层是一个数组,说明Stack的实现是通过数组来实现的,然后通过对数组的操作来模仿栈的各种功能。而且在源码中Vector的很多方法都是synchronized 的,也就是说是线程安全,所以说在多线程中是可以安全使用的,不过这样效率上肯定是会降低的。
目录
相关文章
|
1月前
|
安全 Java 容器
Java Review - Queue和Stack 源码解读
Java Review - Queue和Stack 源码解读
41 0
|
1月前
|
设计模式 存储 C++
C++初阶(十五)Stack和Queue
C++初阶(十五)Stack和Queue
45 0
|
8月前
|
存储 安全 Java
【面试题精讲】Vector 和 Stack 的区别?
【面试题精讲】Vector 和 Stack 的区别?
|
1月前
|
编译器 C++ 容器
【STL】stack与queue的底层原理及其实现
【STL】stack与queue的底层原理及其实现
|
7月前
|
存储 设计模式 C++
【C++杂货铺】探索stack和queue的底层实现
【C++杂货铺】探索stack和queue的底层实现
66 0
|
C语言 C++ 容器
C++中stack的用法(超详细,入门必看)
⭐一、stack的简介 stack的中文译为堆栈,堆栈一种数据结构。C语言中堆栈的定义及初始化以及一些相关操作实现起来较为繁琐,而C++的stack让这些都变得简便易实现。因为C++中有许多关于stack的方法函数。 堆栈(stack)最大的特点就是先进后出(后进先出)。就是说先放入stack容器的元素一定要先等比它后进入的元素出去后它才能出去。呃这样说可能有点绕哈哈,举个生活中的例子吧。 某一天,天气炎热,你买了一个冰淇淋甜筒,而这个冰淇淋甜筒在制作过程时冰淇淋是不是先进入到甜筒的底部然后到最上面呢,但是你在吃的过程中要从最上面吃起到最后才能吃到甜筒底部的冰淇淋,但底部的冰淇淋是先进入甜筒
459 0
【Java基础】栈(Stack) & 队列(Queue)
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表。
181 0
|
设计模式 C++ 容器
【C++修炼之路】12. stack && queue类
【C++修炼之路】12. stack && queue类
【C++修炼之路】12. stack && queue类
|
C++ 容器
C++学习笔记(十八)——stack和queue
C++学习笔记(十八)——stack和queue
C++学习笔记(十八)——stack和queue
|
算法 API 调度
p-queue 源码解读
前言在钉钉表格的开放场景中,需要对用户在沙箱中执行的操作进行合理的调度,我们选择了 p-queue 这个简洁但功能强大的工具库。最近需要对不同类型的任务进行更复杂的并发调度,希望通过学习源码的方式,掌握其中的核心逻辑,确保业务功能的顺利实现。什么是 p-queuep-queue 是一个异步队列管理库。对于需要控制执行速度或数量的异步操作很有用,比如:与 REST API 交互或执行 CPU / 内
384 0