JAVASE - Vector详解

JAVASE - Vector详解

特性

在这里插入图片描述
RandomAccess:支持随机访问,即下标访问
Cloneable:支持拷贝
Serializable:支持序列化
线程安全:
Vector是线程安全的集合,它的安全是由于所有方法上添加了synchronized关键词,由于synchronized锁的是方法,如果在使用Vector集合时产生了复合操作还是会出现安全问题。

属性

	//存储数据的数组
    protected Object[] elementData;

    //存储数据的个数
    protected int elementCount;

   //手动设置扩容量默认为0,也可以通过构造方法设置
    protected int capacityIncrement;

	// 序列化编号
    private static final long serialVersionUID = -2767605614048989439L;

构造方法

//initialCapacity数组长度  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);
    }
//无参构造函数存储数据长度默认为10
public Vector() {
        this(10);
    }
//将Collection中的数据复制出来
public Vector(Collection<? extends E> c) {
        Object[] a = c.toArray();
        elementCount = a.length;
        //为什么要判断Class 是否为ArrayList 因为,在ArrayList 中的toArray就是使用用的
        //Arrays.copyOf方法所以不需要在重新进行复制
        if (c.getClass() == ArrayList.class) {
            elementData = a;
        } else {
            elementData = Arrays.copyOf(a, elementCount, Object[].class);
        }
    }

主要方法

添加数据

public synchronized boolean add(E e) {
		//修改modCount
        modCount++;
        //判断是否需要扩容
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }

指点下表添加数据

public void add(int index, E element) {
        insertElementAt(element, index);
    }
    
    public synchronized void insertElementAt(E obj, int index) {
    //修改modCount
        modCount++;
        //判断是否越界
        if (index > elementCount) {
            throw new ArrayIndexOutOfBoundsException(index
                                                     + " > " + elementCount);
        }
        //判断是否需要扩容
        ensureCapacityHelper(elementCount + 1);
        //将从指定下标开始到最后的数组复制到 指定下标+1的位置,因此index和index+1位置的数据是一样的。
        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
        //将index的位置的值进行修改
        elementData[index] = obj;
        elementCount++;
    }

GET方法

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

        return elementData(index);
    }

SET方法

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

        E oldValue = elementData(index);
        elementData[index] = element;
        //返回老值
        return oldValue;
    }

remove 方法

public boolean remove(Object o) {
        return removeElement(o);
    }
    
    public synchronized boolean removeElement(Object obj) {
    //修改modCount
        modCount++;
	//判断元素是否存在
        int i = indexOf(obj);
        if (i >= 0) {
        	//删除数据
            removeElementAt(i);
            return true;
        }
        return false;
    }

    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);
        }
        //数组size--
        elementCount--;
        //将最后一位数据致null
        elementData[elementCount] = null; /* to let gc do its work */
    }

指定下标删除

public synchronized E remove(int index) {
		//修改modCount
        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);
        //修改数组size 并将最后一位赋null值
        elementData[--elementCount] = null; // Let gc do its work
		//返回老数据
        return oldValue;
    }

扩容(两倍增长)

private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        //扩容两倍增长(capacityIncrement 默认为0)
        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);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值