Stack是java集合类中较为常见的一种数据类型,继承了Vector类,是一种先入后出的对象堆栈。它通过五个操作对类 <tt>Vector</tt> 进行了扩展 ,允许将向量视为堆栈。它提供了通常的push和pop操作,以及取堆栈顶点的peek方法、测试堆栈是否为空的empty方法、在堆栈中查找项并确定到堆栈顶距离的search方法。顺带研究了Jdk源码(JDk1.6)
empty:
* 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;
}
size()方法继承于
vector类
,而vertor中有一个
elementCount变量用于计算容器大小
* Returns the number of components in this vector.
*
* @return the number of components in this vector
*/
public synchronized int size() {
return elementCount;
}
peek:
/**
* 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);
}
peek方法用于查看堆栈顶部对象,但不从堆栈中移除他,返回对象顶部元素(vector中的最后一项),若堆栈为空,就抛出EmptyStackException异常。element方法来自于vector类中,代码如下:
/**
* Returns the component at the specified index.
*
* <p>This method is identical in functionality to the {@link #get(int)}
* method (which is part of the {@link List} interface).
*
* @param index an index into this vector
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
public synchronized E elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData(index);
}
在vector类中,有一个object类型的数字elementData数组来存储元素,所以要访问栈顶元素就是找到elementData中最后的那个索引值。
pop:
用于移除堆栈顶部的对象,并作为此函数的值返回该对象(即
Vector 对象中的最后一项)。若堆栈为空,就抛出EmptyStackException异常。
/**
* 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() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
可以看到,pop方法中首先是调用peek()方法,然后用removeElementAt方法移除栈顶元素。而remoElementAt方法也是vector类中的一个实现。
public synchronized void removeElementAt(int 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; /* to let gc do its work */
}
removeElementAt方法就是System.arraycopy方法,将elementData数组都向前移动一位,arraycopy中elementData即使原数组又是目标数组,index+1是原数组的起始下标,index是目标数组的起始下标,length是拷贝长度,因为vector中不一定是移除最后一个元素,所以才是从后面往前面开始复值覆盖。
push
把项压入堆栈顶部。
/**
* 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) {
addElement(item);
return item;
}
可以看到,它直接调用了vector中的addElement方法。
/**
* 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) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
addElement方法又有有几个封装好的函数组成的,说起来有点复杂,有兴趣的你可以直接去jdk源码中查看,这里就不细说了。