您的位置:首页 > 产品设计 > UI/UE

Stack源码分析

2017-01-14 22:09 405 查看

1, Stack

1, 概念

Stack是栈,进栈时的元素都放在栈尾,出来的顺序也是从尾部先出来,所以是后进先出。定义如下,

public class Stack<E> extends Vector<E> {
Stack继承了Vector,因此Vector的所有public方法它都可以使用。

Stack只有4个特有的方法.

2, push

Push将一个元素放在栈尾。

public E push(E object) {
addElement(object);
return object;
}

3 pop方法

弹出栈尾的元素,并在栈中删除该元素

public synchronized E pop() {
if (elementCount == 0) {
throw new EmptyStackException();
}
final int index = --elementCount;
final E obj = (E) elementData[index];
elementData[index] = null;
modCount++;
return obj;
}

4 peek方法

仅弹出栈尾的元素

public synchronized E peek() {
try {
return (E) elementData[elementCount - 1];
} catch (IndexOutOfBoundsException e) {
throw new EmptyStackException();
}
}

5 search方法

从栈尾开始查找第一个为o的元素的位置

public synchronized int search(Object o) {
final Object[] dumpArray = elementData;
final int size = elementCount;
if (o != null) {
for (int i = size - 1; i >= 0; i--) {
if (o.equals(dumpArray[i])) {
return size - i;
}
}
} else {
for (int i = size - 1; i >= 0; i--) {
if (dumpArray[i] == null) {
return size - i;
}
}
}
return -1;
}

在android中也很少使用到Stack。

2, ArrayDeque

双端队列是既可以在表的前端进行插入和删除操作,也可以在表的后端进行插入删除操作, ArrayDeque也是直接利用数组来实现的。

主要变量如下,

private transient Object[] elements;  // 保存元素的数组
private transient int head; // 指向队首的下标
private transient int tail; // 指向队尾下一个位置的下标
private static final int MIN_INITIAL_CAPACITY = 8; // 最小容量

2.1 构造方法

三个构造方法,

public ArrayDeque() { // 容量为16
elements = new Object[16];
}
public ArrayDeque(int numElements) {
allocateElements(numElements);
}

private void allocateElements(int numElements) { // 不小于numElements的最小的2的n次幂
int initialCapacity = MIN_INITIAL_CAPACITY;
// Find the best power of two to hold elements.
// Tests "<=" because arrays aren't kept full.
if (numElements >= initialCapacity) {
initialCapacity = numElements;
initialCapacity |= (initialCapacity >>>  1);
initialCapacity |= (initialCapacity >>>  2);
initialCapacity |= (initialCapacity >>>  4);
initialCapacity |= (initialCapacity >>>  8);
initialCapacity |= (initialCapacity >>> 16);
initialCapacity++;

if (initialCapacity < 0)   // Too many elements, must back off
initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
}
elements = new Object[initialCapacity];
}
public ArrayDeque(Collection<? extends E> c) { // 将集合转化为ArrayDeque
allocateElements(c.size());
addAll(c);
}

由此可见, ArrayDeque的数组容量只能是2的n次幂。

addFirst

在队首添加一个元素

addLast

在队尾添加一个元素

offerFirst

在队首添加一个元素

offerLast

在队尾添加一个元素

添加示意图如下,

addFirst代码如下,


public void addFirst(E e) {
if (e == null)
throw new NullPointerException("e == null");
elements[head = (head - 1) & (elements.length - 1)] = e;
if (head == tail) // 当头和尾相同时,就进行扩容。
doubleCapacity(); // 扩容
}

2.3 扩容

private void doubleCapacity() {
// assert head == tail;
int p = head;
int n = elements.length;
int r = n - p; // number of elements to the right of p
int newCapacity = n << 1; // 直接扩容一倍
if (newCapacity < 0)
throw new IllegalStateException("Sorry, deque too big");
Object[] a = new Object[newCapacity];
System.arraycopy(elements, p, a, 0, r);
System.arraycopy(elements, 0, a, r, p); // 复制所有元素
elements = a;
head = 0;  // 更新变量
tail = n;
}

其他一些的方法与此类似。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息