一:先看下类的继承关系
UML图如下:


继承关系:
public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable

RandmoAccess快速随机访问接口


二:看下成员属性
/**
* 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.
*
* <p>Any array elements following the last element in the Vector are null.
*
* @serial
*/
protected Object[] elementData;

分析:该数组缓冲区是存储vector元素,并且至少足够大到包含vector的所有元素


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

分析:vector中实际元素的数量



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

分析:vector增加的容量capacityIncrement,如果vector实际元素数量大于当前容量,vector将自动扩容,扩容的容量为当前容量加上capacityIncrement。如果没有指定增加的容量,那么vector在扩容时成倍的增长。


/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -2767605614048989439L;

分析:版本号


三:构造函数
/**
* 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);
}

分析:无参构造函数vector会初始化一个长度为10的空数组,它的标准容量增量为0(ps:也就是上面提到的capacityIncrement = 0)


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

分析:指定vector初始化长度,构造一个指定的长度空数组,它的标准容量增量为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];
//容量增加值为传进来的capacityIncrement值
this.capacityIncrement = capacityIncrement;
}

分析:指定初始化长度和容量增量值


/**
* 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) {
//将集合转为数组,赋值给Object数组elementData
elementData = c.toArray();
//vector长度等于集合长度
elementCount = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
//c.toArray方法返回的数据错误,重新拷贝
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}

分析:传入指定的集合,将集合元素赋值给vector



四:看下主要的方法
1.add()方法
/**
* Appends the specified element to the end of this Vector.
*
* @param e element to be appended to this Vector
* @return {@code true} (as specified by {@link Collection#add})
* @since 1.2
*/
public synchronized boolean add(E e) {
//抽象父类AbstractList的成员变量,修改次数记录
modCount++;
//保证容量足够大
ensureCapacityHelper(elementCount + 1);
//elementCount是当前元素个数,数组下标是从0开始的,所以elementData[elementCount]是当前需要添加元素的位置
//elementCount++
elementData[elementCount++] = e;
return true;
}
分析:这里我们可以看到vector的添加方法,直接加了个synchronized锁,添加方法是线程安全的,但是直接用synchronize也导致效率低下,所以我们现在基本不用vector但是了解它还是有必要的,因为stack栈是继承自它的。
同时,这里的添加方法,逻辑处理很简单,就是先判断下vector需不需要扩容,然后根据下标索引往数组里添加值。

我们继续看戏vector需不需要扩容方法
ensureCapacityHelper(elementCount + 1);

分析:方法参数为当前vector实际个数 + 1


/**
* This implements the unsynchronized semantics of ensureCapacity.
* Synchronized methods in this class can internally call this
* method for ensuring capacity without incurring the cost of an
* extra synchronization.
*
* @see #ensureCapacity(int)
*/
private void ensureCapacityHelper(int minCapacity) {
// overflow-conscious code
//如果传进来的值大于当前vector容量数组长度,调用grow方法
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}

分析:这里没有加锁操作,因为调用这个方法的外部方法都是加了锁保证了同步,这里就没必要再进行加锁产生额外的性能消耗。


private void grow(int minCapacity) {
// overflow-conscious code
//旧的数组容量(长度大小)
int oldCapacity = elementData.length;
//如果指定了增加容量值capacityIncrement的值大于0,那么新的容量为原来容量加上capacityIncrement值大小
//否则新的容量扩大为原来容量的两倍
int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);
//判断扩容后的容量是否小于传进来的vector最小容量,那么新的容量等于需要的最小容量
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
//如果扩容后的容量大于规定的最大值Integer.MAX_VALUE - 8
if (newCapacity - MAX_ARRAY_SIZE > 0)
//根据当前能容纳所有数据最小容量值minCapacity进行判断newCapacity的容量大小
newCapacity = hugeCapacity(minCapacity);
//将旧数组数据拷贝到新数组
elementData = Arrays.copyOf(elementData, newCapacity);
}

分析:grow方法也就是真正进行vector扩容逻辑判断方法。这里我们需要掌握两点

1.无额外的加锁操作,原理同上
2.扩容机制:
如果指定了增加容量值capacityIncrement的值大于0,那么新的容量为原来容量加上capacityIncrement值大小,否则新的容量扩大为原来容量的两倍。

2.remove()方法
/**
* Removes the element at the specified position in this Vector.
* Shifts any subsequent elements to the left (subtracts one from their
* indices). Returns the element that was removed from the Vector.
*
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()})
* @param index the index of the element to be removed
* @return element that was removed
* @since 1.2
*/
public synchronized E remove(int index) {
//修改次数加1
modCount++;
//校验下标是否越界
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
//根据索引取出旧的值
E oldValue = elementData(index);
//需要移动的元素个数
int numMoved = elementCount - index - 1;
if (numMoved > 0)
//elementData数组从index+1位置的元素开始读取元素,拷贝到elementData数组下标从index开始,长度为mumMoved
//相当于从index + 1位置往后的元素都往左移动一个下标
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
//数组末尾置为null,索引减1
elementData[--elementCount] = null; // Let gc do its work

return oldValue;
}

分析:删除操作也添加了synchronized锁,因此也是线程安全的,其它操作比较简单,直接看注释。


总结:vector因为为了线程安全采用直接加synchronized锁,导致了性能是比较低下的,在业务上用到vector的场景不多,所以我们就简单分析,了解一下就好。
到此,vector分析就告一段落了。


有疑问,扫我二维码添加微信,欢迎骚扰!
坚持做一件事,一起学习。

jdk1.8-Vector的更多相关文章

  1. Java容器解析系列(4) ArrayList Vector Stack 详解

    ArrayList 这里关于ArrayList本来都读了一遍源码,并且写了一些了,突然在原来的笔记里面发现了收藏的有相关博客,大致看了一下,这些就是我要写的(╹▽╹),而且估计我还写不到博主的水平,这 ...

  2. LinkedList、ArrayList、Vector三者的关系与区别?

    LinkedList.ArrayList.Vector三者的关系与区别? 区分ArrayList,Vector,LinkedList的区别 ArrayList,Vector的区别: 1.出现版本:Ar ...

  3. 【原】Java学习笔记026 - 集合

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:从三国演义中 ...

  4. Java基础——集合(持续更新中)

    集合框架 Java.util.Collection Collection接口中的共性功能 1,添加 booblean add(Object obj);  往该集合中添加元素,一次添加一个 boolea ...

  5. 超详细的java集合讲解

    1 集合 1.1 为什么会出现集合框架 [1] 之前的数组作为容器时,不能自动拓容 [2] 数值在进行添加和删除操作时,需要开发者自己实现添加和删除. 1.2 Collection接口 1.2.1 C ...

  6. java集合详解(附栈,队列)

    1 集合 1.1 为什么会出现集合框架 [1] 之前的数组作为容器时,不能自动拓容 [2] 数值在进行添加和删除操作时,需要开发者自己实现添加和删除. 1.2 Collection接口 1.2.1 C ...

  7. 给jdk写注释系列之jdk1.6容器(10)-Stack&Vector源码解析

    前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈.      什么是栈呢,我就不找它具体的定义了,直接举个例子,栈就相当于一个很窄的木桶 ...

  8. JDK1.8源码阅读系列之三:Vector

    本篇随笔主要描述的是我阅读 Vector 源码期间的对于 Vector 的一些实现上的个人理解,用于个人备忘,有不对的地方,请指出- 先来看一下 Vector 的继承图: 可以看出,Vector 的直 ...

  9. 学习JDK1.8集合源码之--Vector

    1. Vector简介 Vector是JDK1.0版本就推出的一个类,和ArrayList一样,继承自AbstractList,实现了List.RandomAccess.Cloneable.java. ...

  10. Stack&Vector源码分析 jdk1.6

    参照:http://www.cnblogs.com/tstd/p/5104099.html Stack(Fitst In Last Out) 1.定义 public class Stack<E& ...

随机推荐

  1. shell进阶--流程

    由于条件判断和循环跟其他语言都大同小异,学过编程的话很好理解,这里只贴出格式,不具体写用法了.(select菜单会详细讲一下) 条件判断 if条件判断 普通if条件判断: 嵌套if条件判断: 循环 f ...

  2. mysqldump 原理

    (3)分析general.log日志: [root@zstedu data]# cat zstedu.log mysqld, Version: 5.7.22-log (MySQL Community ...

  3. MySQL字符集或字符序

        字符集基础 字符集:数据库中的字符集包含两层含义 各种文字和符号的集合,包括各国家文字,标点符号,图形符号,数字等. 字符的编码方式,即二进制数据与字符的映射规则:   字符集分类: ASCI ...

  4. fullpage.js最后一屏不满一屏时,滚动方式

    这两天公司网页改版用到fullpage.js这个滚屏插件,页面内容整屏的滚动,不成问题,各种设置在网上也都有文档.而我遇到的问题就是,页面内容不满屏的时候,和上面的内容放一块就太挤,单独放一屏就太空, ...

  5. 高并发下的Nginx优化

    高并发下的Nginx优化 2014-08-08 13:30 mood Nginx    过去谈过一些关于Nginx的常见问题; 其中有一些是关于如何优化Nginx. 很多Nginx新用户是从Apach ...

  6. HDU 6033 - Add More Zero | 2017 Multi-University Training Contest 1

    /* HDU 6033 - Add More Zero [ 简单公式 ] | 2017 Multi-University Training Contest 1 题意: 问 2^n-1 有几位 分析: ...

  7. js中window.event对象

    event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有意义.比如,fromEleme ...

  8. CF1155D Beautiful Array 贪心,dp

    CF115DBeautiful Array 题目大意:给一个有n个元素的a数组,可以选择其中一个区间的所有数都乘上x,也可以不选,求最大子序列和. 如果没有前面的操作,就是只求最大子序列和,我们都知道 ...

  9. 日照学习提高班day3测试

    A 思路: 一看到'#''.'什么的就想到搜索怪我怪我... 这道题勉强说是搜索别打我qwq 1)因为不重复,所以首先要判断是否%5==0,若不满足,直接输出NO 2)弄个vis数组记录是否被搜过,如 ...

  10. HDU 5119 Happy Matt Friends ——(背包DP)

    题意:有最多40个数字,取任意个数字他们的异或和>=k则是可行的方案,问有多少种可行的方案. 分析:dp[now][j]表示当前这个值的种类数,那么转移方程为dp[now][j] = dp[pr ...