博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Vector源码分析
阅读量:7034 次
发布时间:2019-06-28

本文共 2188 字,大约阅读时间需要 7 分钟。

hot3.png

import java.util.List;import java.util.Vector;public class VectorTest {    public static void main(String[] args) {        List
vector = new Vector<>(); vector.add("abc"); }}

查看Vector构造方法:

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

查看this(10)

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

再继续看this(initialCapacity,0)

/**     * Constructs an empty vector with the specified initial capacity and     * capacity increment.     *     * @param   initialCapacity     the initial capacity of the vector     * @param   capacityIncrement   the amount by which the capacity is     *                              increased when the vector overflows     * @throws IllegalArgumentException if the specified initial capacity     *         is negative     */    public Vector(int initialCapacity, int capacityIncrement) {        super();        if (initialCapacity < 0)            throw new IllegalArgumentException("Illegal Capacity: "+                                               initialCapacity);        this.elementData = new Object[initialCapacity];        this.capacityIncrement = capacityIncrement;    }

此时initialCapacity为10,capacityIncrement为0,this.elementData为对象数组

/**     * The array buffer into which the components of the vector are     * stored. The capacity of the vector is the length of this array buffer,     * and is at least large enough to contain all the vector's elements.     *     * 

Any array elements following the last element in the Vector are null. * * @serial */ protected Object[] elementData;

所以Vector底层采用的是数组来存放元素的,大部分的public方法都是synchronized的。即同步的。使用不当会造成性能问题。

Vector和ArrayList底层数据结构都是数组。

转载于:https://my.oschina.net/duanvincent/blog/761992

你可能感兴趣的文章
【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之二
查看>>
Windows Server 2012体验之卸载辅助域控制器
查看>>
【虚拟化实战】VM设计之三内存资源控制
查看>>
HDS:转型关键还是私有云
查看>>
【COCOS2D-HTML5 开发之三】示例项目附源码及运行的GIF效果图
查看>>
乔布斯辞世:“指尖上”的创新启发世界(新华国际时评)
查看>>
CES2012归来谈:软件为王的新一代消费电子业
查看>>
新站收录记录
查看>>
cocos2d-x笔记 ccTouchesBegan、ccTouchesMoved、ccTouchesEnded
查看>>
maven解决.lastUpdated maven无法下载jar
查看>>
http_load安装与测试参数分析 - 追求自由自在的编程 - ITeye技术网站
查看>>
每天学点GDB 15
查看>>
常用Git代码托管服务分享
查看>>
leetcode -- Word Break
查看>>
PYTHONPATH 可以跨版本 方便使用 (本文为windows方法)转~
查看>>
Oracle 动态视图5 V$SESSION_LONGOPS
查看>>
C中字符串分割函数strtok的一个坑
查看>>
虚拟主机配置
查看>>
强大的数据库查询工具Database.NET 9.4.5018.42
查看>>
仿人人滑动菜单
查看>>