JAVA集合二 ——list(03 Vector、Stack)
*特别声明:
*本文只是备忘录。
JAVA中List的实现主要有ArrayList、vector、stack、LinkedList,以及一个抽象的类AbstractList.
1、Vector
#Vector可以看作是一个线程安全版本的ArrayList。Vector在一些操作方法上加上了synchronized关键字。
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
#但是Vector和ArrayList也有些不一样,不一样之处主要在扩容上。
Vector中有一个属性capacityIncrement,定义的是每次扩容增加的容量。可以通过构造方法进行设置。
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
this.capacityIncrement = capacityIncrement;
}
不指定扩容量的构造方法,默认扩容量是0:
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
我们上篇说过,new ArrayList()默认的长度是0,那么 new Vector()呢。答案是10,new的时候直接设置为10。
public Vector() {
this(10);
}
通过几个构造方法,我们可以知道,当 Vecetor v = new Vector()的时候,初始化的数组长度是10,默认扩容量是0。
说了这么半天的扩容量,让我们看看扩容量的作用和Vector如何扩容。
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
扩容方法主要在这句:
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
什么意思?仔细读一下。
如果扩容量大于0,每次数组扩容的值就是扩容量;如果不大于0,每次扩容的就是翻倍。
2、Stack
Stack继承自Vector,定义的是先进后出(FILO)的结构。这点和Queue 队列先进先出(FIFO)相反。
Stack的结构和特性都与Vector相同,只提供一个无参数的构造方法。同时提供了自己的一些方法来保证FILO。
主要方法如下:
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
boolean empty() | 返回堆栈是否为空 |
Object peek( ) | 查看堆栈顶部的对象,但不从堆栈中移除它。 |
Object pop( ) | 移除堆栈顶部的对象,并作为此函数的值返回该对象。 |
Object push(Object element) | 把对象压入堆栈顶部。 |
int search(Object element) | 返回对象在堆栈中的位置,以 1 为基数。栈顶为1。 |
当然Stack对外是一种FILO的数据模型,但是真实的内部数据结构是和Vector一样的,查看源码的时候需要注意。