类 Vector

Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用整数索引进行访问的组件。但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。

每个向量会试图通过维护 capacitycapacityIncrement 来优化存储管理。capacity 始终至少应与向量的大小相等;这个值通常比后者大些,因为随着将组件添加到向量中,其存储将按 capacityIncrement 的大小增加存储块。应用程序可以在插入大量组件前增加向量的容量;这样就减少了增加的重分配的量。

从 Java 2 平台 v1.2 开始,已改进此类以实现 List,这样它就成为了 Java 的集合框架的一部分。与新集合的实现不同,Vector 是同步的。

由 Vector 的 iterator 和 listIterator 方法所返回的迭代器是快速失败的:如果在迭代器创建后的任意时间从结构上修改了向量(通过迭代器自身的 remove 或 add 方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生不确定行为的风险。Vector 的 elements 方法返回的 Enumeration 不是 快速失败的。

源码

成员:

protected Object[] elementData;//数组

    protected int elementCount;//添加元素的个数
    
    protected int 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;
    }

    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

    public Vector() {
        // 默认容量10
        this(10);
    }

    public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

添加元素:

public synchronized boolean add(E e) {
        modCount++;
        // 确认容量
        ensureCapacityHelper(elementCount + 1);
        // 添加元素
        elementData[elementCount++] = e;
        return true;
    }

    private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        // 是否需要扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        // 若设定了capacityIncrement则扩容量为capacityIncrement ,否则默认扩容一倍
        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);
    }

获取元素:

public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        return elementData(index);
    }

移除元素:

public synchronized E remove(int index) {
        modCount++;
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
        E oldValue = elementData(index);

        int numMoved = elementCount - index - 1;
        if (numMoved > 0)
            System.arraycopy(elementData, index+1, elementData, index,
                             numMoved);
        elementData[--elementCount] = null; // Let gc do its work

        return oldValue;
    }

常用方法:

boolean add(E o) 
 将指定元素追加到此向量的末尾。 
 void add(int index, E element) 
 在此向量的指定位置插入指定的元素。 
 boolean addAll(Collection<? extends E> c) 
 将指定 Collection 中的所有元素追加到此向量的末尾,按照指定集合的迭代器所返回的顺序追加这些元素。 
 boolean addAll(int index, Collection<? extends E> c) 
 在指定位置将指定 Collection 中的所有元素插入到此向量中。 
 void addElement(E obj) 
 将指定的组件添加到此向量的末尾,将其大小增加 1。 
 int capacity() 
 返回此向量的当前容量。 
 void clear() 
 从此向量中移除所有元素。 
 Object clone() 
 返回向量的一个副本。 
 boolean contains(Object elem) 
 测试指定的对象是否为此向量中的组件。 
 boolean containsAll(Collection<?> c) 
 如果此向量包含指定 Collection 中的所有元素,则返回 true。 
 void copyInto(Object[] anArray) 
 将此向量的组件复制到指定的数组中。 
 E elementAt(int index) 
 返回指定索引处的组件。 
 Enumeration<E> elements() 
 返回此向量的组件的枚举。 
 void ensureCapacity(int minCapacity) 
 增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。 
 boolean equals(Object o) 
 比较指定对象与此向量的相等性。 
 E firstElement() 
 返回此向量的第一个组件(位于索引 0 处的项)。 
 E get(int index) 
 返回向量中指定位置的元素。 
 int hashCode() 
 返回此向量的哈希码值。 
 int indexOf(Object elem) 
 搜索给定参数的第一个匹配项,使用 equals 方法测试相等性。 
 int indexOf(Object elem, int index) 
 搜索给定参数的第一个匹配项,从 index 处开始搜索,并使用 equals 方法测试其相等性。 
 void insertElementAt(E obj, int index) 
 将指定对象作为此向量中的组件插入到指定的 index 处。 
 boolean isEmpty() 
 测试此向量是否不包含组件。 
 E lastElement() 
 返回此向量的最后一个组件。 
 int lastIndexOf(Object elem) 
 返回指定的对象在此向量中最后一个匹配项的索引。 
 int lastIndexOf(Object elem, int index) 
 向后搜索指定的对象,从指定的索引处开始搜索,并返回一个索引。 
 E remove(int index) 
 移除此向量中指定位置的元素。 
 boolean remove(Object o) 
 移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,则元素保持不变。 
 boolean removeAll(Collection<?> c) 
 从此向量中移除包含在指定 Collection 中的所有元素。 
 void removeAllElements() 
 从此向量中移除全部组件,并将其大小设置为零。 
 boolean removeElement(Object obj) 
 从此向量中移除变量的第一个(索引最小的)匹配项。 
 void removeElementAt(int index) 
 删除指定索引处的组件。 
protected  void removeRange(int fromIndex, int toIndex) 
 从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。 
 boolean retainAll(Collection<?> c) 
 在此向量中仅保留包含在指定 Collection 中的元素。 
 E set(int index, E element) 
 用指定的元素替换此向量中指定位置处的元素。 
 void setElementAt(E obj, int index) 
 将此向量指定 index 处的组件设置为指定的对象。 
 void setSize(int newSize) 
 设置此向量的大小。 
 int size() 
 返回此向量中的组件数。 
 List<E> subList(int fromIndex, int toIndex) 
 返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。 
 Object[] toArray() 
 返回一个数组,包含此向量中以正确顺序存放的所有元素。 
<T> T[] 
 toArray(T[] a) 
 返回一个数组,包含此向量中以正确顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。 
 String toString() 
 返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式。 
 void trimToSize() 
 对此向量的容量进行微调,使其等于向量的当前大小。

总结

底层由一个可以增长的数组组成

Vector 通过 capacity (容量) 和 capacityIncrement (增长数量) 来尽量少的占用空间

扩容时默认扩大两倍,若指定capacityIncrement,则扩大capacityIncrement的数量。

最好在插入大量元素前增加 vector 容量,那样可以减少重新申请内存的次数

通过 iterator 和 lastIterator 获得的迭代器是 fail-fast 的,而通过 elements 获得的老版迭代器 Enumeration 不是 fail-fast 的

同步类,每个方法前都有同步锁 synchronized

elementCount 是动态数组的实际大小。

在 JDK 2.0 以后,经过优化,Vector 也加入了 Java 集合框架大家族

类 Stack

Stack 类表示后进先出(LIFO)的对象堆栈。它通过五个操作对类 Vector 进行了扩展 ,允许将向量视为堆栈。它提供了通常的 push 和 pop 操作,以及取栈顶点的 peek 方法、测试堆栈是否为空的 empty 方法、在堆栈中查找项并确定到栈顶距离的 search 方法。

源码

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

    /**
     * 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;
    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = 1224463164541339165L;
}

总结

栈最主要的意义就在于入栈和出栈的对称性。在 Android 开发中,我们经常需要开启、回退一个 Activity,其实这里就有栈的应用,每次开启Activity,如果不是特殊的启动模式,就会在栈顶加入一个 Activity,点击返回后,之前的 Activity 出栈 。其他场景比如递归(斐波那契数列,汉诺塔)。

Stack实际上也是通过数组去实现的。
执行push时(即,将元素推入栈中),是通过将元素追加的数组的末尾中。
执行peek时(即,取出栈顶元素,不执行删除),是返回数组末尾的元素。
执行pop时(即,取出栈顶元素,并将该元素从栈中删除),是取出数组末尾的元素,然后将该元素从数组中删除。

除了 push(),剩下的方法都是同步的