编程语言
首页 > 编程语言> > Java基础篇之第()幕——Vector

Java基础篇之第()幕——Vector

作者:互联网

文章目录

一、概述

在这里插入图片描述
是一个矢量队列。
继承了AbstractList,实现了List接口,所以它是一个队列,支持相应的添加、删除、修改、遍历等功能;
实现了RandmoAccess接口,就提供了随机访问的功能,是Java中用来被List实现的,为List提供快速访问功能,就是可以通过序号快速获取对象。
实现了Cloneable接口,实现了clone()函数,能被克隆。

二、源码

1、属性

protected Object[] elementData;

    /**
     * The number of valid components in this {@code Vector} object.
     * Components {@code elementData[0]} through
     * {@code elementData[elementCount-1]} are the actual items.
     *
     * @serial
     */
    protected int elementCount;

    /**
     * The amount by which the capacity of the vector is automatically
     * incremented when its size becomes greater than its capacity.  If
     * the capacity increment is less than or equal to zero, the capacity
     * of the vector is doubled each time it needs to grow.
     *
     * @serial
     */
    protected int capacityIncrement;

(1)elementData

Object[]类型的数组,保存了添加到Vector的元素,是个动态数组,如果初始化时没指定大小,会默认10,随着元素增加,容量也动态增长。

(2)elementCount

动态数组的实际大小。

(3)capacityIncrement

动态数组的增长系数,创建Vector时如果指定了该值,每次增长时都按这个大小来增加。具体增长方式看ensureCapacity()方法。

2、构造函数

	public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

    /**
     * Constructs an empty vector with the specified initial capacity and
     * with its capacity increment equal to zero.
     *
     * @param   initialCapacity   the initial capacity of the vector
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }

    /**
     * Constructs an empty vector so that its internal data array
     * has size {@code 10} and its standard capacity increment is
     * zero.
     */
    public Vector() {
        this(10);
    }

    /**
     * Constructs a vector containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this
     *       vector
     * @throws NullPointerException if the specified collection is null
     * @since   1.2
     */
    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);
    }

分别是:
capacity是默认容量大小,capacityIncrement是每次Vector容量增加时的增量;
capacity是默认容量大小,增加数据导致容量增加时,每次容量都增加一倍;
默认构造函数;
创建一个包含Collection的Vector。

3、主要方法

通过一个数组来保存数据。默认容量10。
当容量不足时,容量增加,如果增加系数不为0,就按增加系数来增加,不然就是增加一倍。
克隆函数可以把全部元素克隆到一个数组里去。

4、遍历

其中索引随机访问最快,迭代器最慢。

(1)迭代器

即Iterator。

	Integer value = null;
	Iterator<int> size = vec.iterator();
	while(size.hasNext()){
		value = size.next();
	}

(2)随机访问

通过索引值(因为实现了RandomAccess接口)。

	Integer value = null;
	int size = vec.size();
	for (int i=0; i<size; i++) {
	    value = (Integer)vec.get(i);        
	}

(3)for

	Integer value = null;
	for (Integer integ:vec) {
	    value = integ;
	}

(4)Enumeration

	Integer value = null;
	Enumeration enu = vec.elements();
	while (enu.hasMoreElements()) {
	    value = (Integer)enu.nextElement();
	}

标签:capacityIncrement,vector,Java,elementData,基础,value,Vector,capacity
来源: https://blog.csdn.net/HelenAndLi/article/details/117468510