向量(Vector)

public class Vector <E>
extends AbstractList <E>
implements List<E>, RandomAccess, Cloneable, Serializable
The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

Vector,继承自AbstractList,实现了List<E>, RandomAccess, Cloneable, Serializable。
是一个可增长的"数组类",向量和数组很像,可以用整数下标索引。但它可以放大/缩小容量、加添、删除元素。

Fields,Vector提供的3个属性变量:

protected Object[] elementData;
//The array buffer into which the components of the vector are stored.

protected int elementCount;
//The number of valid components in this Vector object.

protected int capacityIncrement;
//The amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity

类java.util.AbstractList中声明的字段:modCount

Constructor 4个构造方法:
在这里插入图片描述

    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() {
        this(10);
    }
  public Vector(Collection<? extends E> c) {
        elementData = c.toArray();
        elementCount = elementData.length;
        // defend against c.toArray (incorrectly) not returning Object[]
        // (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
    }

主要 Methody一览:
在这里插入图片描述

1、添加操作

1.1 调用 add(index,element) 方法,将指定元素插入到此向量的指定位置,源码为:

    public void add(int index, E element) {
        insertElementAt(element, index);
    }

调用 boolean add​(E e),将指定的元素追加到此向量的末尾。
再看 insertElementAt(element, index)

    public synchronized void insertElementAt(E obj, int index) {
        if (index > elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount);
        }
        modCount++;
        final int s = elementCount;
        Object[] elementData = this.elementData;
        if (s == elementData.length)
            elementData = grow();
        System.arraycopy(elementData, index,
                         elementData, index + 1,
                         s - index);
        elementData[index] = obj;
        elementCount = s + 1;
    }

1.2 或者调用 addElement(E obj),将指定的组件添加到此向量的末尾,将其大小增加1。

    public synchronized void addElement(E obj) {
        modCount++;
        add(obj, elementData, elementCount);
    }

再看 add(E e, Object[] elementData, int s)

    private void add(E e, Object[] elementData, int s) {
        if (s == elementData.length)
            elementData = grow();
        elementData[s] = e;
        elementCount = s + 1;
    }

2、修改操作

调用 set(int index, E element) 方法,修改下标为index的元素,并将修改前的元素返回。

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

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

3、访问操作

调用 get(int index) 方法,返回此向量中指定位置的元素。

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

        return elementData(index);
    }
    @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];
    }

4、删除操作

4.1 调用带索引的方法: remove(int index) ,移除此向量中指定位置的元素。内部主要是使用了arraycopy,进行移位的复制。

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

4.2 调用带元素的删除方法:remove(Object o),移除此向量中指定元素的第一次出现,如果该向量不包含该元素,则该元素将保持不变。
跳转到方法 removeElement

    public boolean remove(Object o) {
        return removeElement(o);
    }
    public synchronized boolean removeElement(Object obj) {
       modCount++;
       int i = indexOf(obj);
       if (i >= 0) {
           removeElementAt(i);
           return true;
       }
       return false;
   }
    public synchronized void removeElementAt(int index) {
       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);
       }
       modCount++;
       elementCount--;
       elementData[elementCount] = null; /* to let gc do its work */
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值