本文根据Android API 21
构造方法
Stack()
public Stack() {
}
构造一个默认数量的Stack.
判断当前Stack是否为空
empty()
public boolean empty() {
return isEmpty();
}
返回栈顶的与素,并不将此元素移除栈
peek()
@SuppressWarnings("unchecked")
public synchronized E peek() {
try {
return (E) elementData[elementCount - 1];
} catch (IndexOutOfBoundsException e) {
throw new EmptyStackException();
}
}
返回栈顶的与素,并将此元素移除栈
pop()
@SuppressWarnings("unchecked")
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;
}
把指定的object对象置为栈顶
push()
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;
}
总结:
- Stack支持并发操作
- Stack内部通过数组来实现数据的操作
- Stack数据的特点是先进先出。