Vector底层原理

简介: Vector底层原理

Vector源码分析

Vector于ArrayList类似同样是数组类型,但是是线程安全的,为什么线程安全?在增删改方法中都加上了synchronized关键字

成员变量

protected Object[] elementData;//存储ArrayList元素的临时数组
protected int elementCount;//存储元素的个数
protected int capacityIncrement;//扩容容量

构造函数

1.无参构造函数

默认初始容量10

public Vector() {
        this(10);
    }

2.有参构造函数

指定Vector的初始容量和扩容时的增长系数为0

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

指定Vector的初始容量和扩容时的增长系数大小

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

根据传入集合创建一个Vector数组

public Vector(Collection<? extends E> c) {
    Object[] a = c.toArray();
    elementCount = a.length;
    if (c.getClass() == ArrayList.class) {
        elementData = a;
    } else {
        elementData = Arrays.copyOf(a, elementCount, Object[].class);
    }
}

增加方法

addElement(E obj)

public synchronized void addElement(E obj) {
        modCount++;//修改数+1
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = obj;//在数组中设置元素位置
    }

判断是否需要扩容

private void ensureCapacityHelper(int minCapacity) {
    //当前元素个数大于数组容量就扩容
    if (minCapacity - elementData.length > 0)
        grow(minCapacity);
}

扩容方法

private void grow(int minCapacity) {
        int oldCapacity = elementData.length;//10
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                         capacityIncrement : oldCapacity);//0>0?0:10
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

以上代码表示,默认会创建一个10长度的数组,当第11个参数添加的时候,进入grow方法,oldCapacity=10,newCapacity=10+10=20,最后copy数组。因此Vector扩容容量为原来的两倍。

插入方法

insertElementAt(E obj, int index)方法

在指定位置插入

public synchronized void insertElementAt(E obj, int index) {
    modCount++;
    //判断下标索引是否合理
    if (index > elementCount) {
        throw new ArrayIndexOutOfBoundsException(index
                                                 + " > " + elementCount);
    }
    //判断是否需要扩容
    ensureCapacityHelper(elementCount + 1);
    //数组Copy 把当前位置留出来,index到后面的元素全部后移一个
    System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
    //插入当前元素
    elementData[index] = obj;
    //数量+1
    elementCount++;
}

删除方法

removeElementAt(int index)方法

删除指定索引位置的方法

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);
    }
    //数组容元素-1
    elementCount--;
    //最后多余的位置置为空
    elementData[elementCount] = null;
}

removeAllElements()

public synchronized void removeAllElements() {
    modCount++;//修改次数
    //遍历删除元素
    for (int i = 0; i < elementCount; i++)
        elementData[i] = null;//赋值为空
    elementCount = 0;//元素个数为0
}

其他方法

elementAt(int index)

根据索引获取元素

public synchronized E elementAt(int index) {
    if (index >= elementCount) {
        throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
    }
    return elementData(index);
}

firstElement()

获取第一个元素

public synchronized E firstElement() {
    if (elementCount == 0) {
        throw new NoSuchElementException();
    }
    return elementData(0);
}

lastElement()

获取最后一个元素

public synchronized E lastElement() {
    if (elementCount == 0) {
        throw new NoSuchElementException();
    }
    return elementData(elementCount - 1);
}

setElementAt(E obj, int index)

根据索引位置设置元素

public synchronized void setElementAt(E obj, int index) {
    if (index >= elementCount) {
        throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                 elementCount);
    }
    elementData[index] = obj;
}

indexOf(Object o, int index)

在指定范围内查找元素索引

public synchronized int indexOf(Object o, int index) {
 //判断对象是否为空,返回-1
    if (o == null) {
        for (int i = index ; i < elementCount ; i++)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = index ; i < elementCount ; i++)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;//未匹配
}

lastIndexOf(Object o, int index)

在指定范围内倒序查找元素索引

public synchronized int lastIndexOf(Object o, int index) {
    if (index >= elementCount)
        throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
    if (o == null) {
        for (int i = index; i >= 0; i--)
            if (elementData[i]==null)
                return i;
    } else {
        for (int i = index; i >= 0; i--)
            if (o.equals(elementData[i]))
                return i;
    }
    return -1;
}
相关文章
|
SQL Java 测试技术
在Spring boot中 使用JWT和过滤器实现登录认证
在Spring boot中 使用JWT和过滤器实现登录认证
1388 0
|
6月前
|
JSON BI 调度
Python + 大模型行业资讯自动化摘要流水线完整工程实现方案
Python + 大模型行业资讯自动化摘要流水线完整工程实现方案
|
机器学习/深度学习 人工智能 运维
|
机器学习/深度学习 Python
堆叠集成策略的原理、实现方法及Python应用。堆叠通过多层模型组合,先用不同基础模型生成预测,再用元学习器整合这些预测,提升模型性能
本文深入探讨了堆叠集成策略的原理、实现方法及Python应用。堆叠通过多层模型组合,先用不同基础模型生成预测,再用元学习器整合这些预测,提升模型性能。文章详细介绍了堆叠的实现步骤,包括数据准备、基础模型训练、新训练集构建及元学习器训练,并讨论了其优缺点。
1251 3
|
存储 弹性计算 数据管理
阿里云对象存储OSS怎么收费?存储包流量包还是按量?
阿里云对象存储OSS收费有两种计费模式,即包年包月和按量付费,包年包月是指购买存储包、流量包来抵扣OSS产生的存储费核流量费,OSS标准(LRS)存储包100GB优惠价33元、500GB存储包半年162元、OSS存储包40GB一年9元,OSS流量包100G 49元/月
9813 2
阿里云对象存储OSS怎么收费?存储包流量包还是按量?
|
存储 监控 NoSQL
项目没亮点?那就来学下pk功能设计吧
它的流程是这样,主播点击申请pk按钮,匹配其他同时申请pk的主播,粉丝通过送礼给心爱的主播提高pk进度条,pk结束后失败的一方主播接受惩罚。但进度条数据主要是提供给在pk开始后才进来直播间的观众,这类人进行直播间后,客户端调用pk进度的查询接口,获取最新的pk进度条。每场pk都有倒计时,这里我们在pk匹配成功时就在Redis里设置一个倒计时键值对,该键值对的初始值是本场pk的总pk时间。,我们给他设计为WebSocket数据实时推送,只要主播的进度有增加,把增加的数值推送到所有在pk直播间的用户。
774 1
项目没亮点?那就来学下pk功能设计吧
|
JSON JavaScript 数据格式
postman如何模拟浏览器发送post请求,意思是替代Vue向后端发送一个请求
postman如何模拟浏览器发送post请求,意思是替代Vue向后端发送一个请求
|
机器学习/深度学习 并行计算 PyTorch
深度学习|如何确定 CUDA+PyTorch 版本
深度学习|如何确定 CUDA+PyTorch 版本
3247 0
|
Java 持续交付 Docker
十一、Docker搭建部署SpringCloud微服务项目Demo(二)
十一、Docker搭建部署SpringCloud微服务项目Demo