Skip to content

Commit

Permalink
feat: 添加Stack源码分析
Browse files Browse the repository at this point in the history
  • Loading branch information
wardseptember committed Dec 6, 2020
1 parent e5ee9f4 commit 4c7898f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* [ArrayList源码分析](/docs/Java集合包/ArrayList源码分析.md)
* [LinkedList源码分析](/docs/Java集合包/LinkedList源码分析.md)
* [Vector源码解析](/docs/Java集合包/Vector源码解析.md)
* [Stack源码分析](/docs/Java集合包/Stack源码分析.md)
* [PriorityQueue源码分析](/docs/Java集合包/PriorityQueue源码分析.md)
* [ArrayQueue源码分析](/docs/Java集合包/ArrayQueue源码分析.md)
* [HashMap源码详解(基于jdk1.8和1.7)](/docs/Java集合包/HashMap详解(1.7和1.8).md)
Expand Down
88 changes: 88 additions & 0 deletions docs/Java集合包/Stack源码分析.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 简介

Stack是栈,先入后出,继承自Vector,所以是线程安全的。遗留类,不推荐使用,可用Deque模拟战。本质上还是用数组实现的,数组尾部就是栈顶。

```java
Deque<Integer> stack = new LinkedList<>();
stack.addFirst(12);
stack.pollFirst();
```

<div align="center"> <img src="https://gitee.com/wardseptember/images/raw/master/imgs/20201206215001.png" width="600"/> </div><br>

# 源码

源码比较简单

```java
package java.util;


public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
}

public E push(E item) {
// 添加到数组尾部
addElement(item);

return item;
}

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).
* @throws 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;
}

// 返回o所在的索引位置
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;
}

```

0 comments on commit 4c7898f

Please sign in to comment.