List除了ArrayList和LinkedList之外,还有一个最常用的就是Vector。

Vector在中文的翻译是矢量,向量,所以大家喜欢把Vector叫做矢量数组,或者向量数组。

其实就底层实现来说Vector与ArrayList的实现大同小异,都是使用数组作为底层的存储器,在上面进行了一些列的操作封装,而且都实现了List的数据接口。

最主要的区别就是Vector的大部分操作增加了线程同步的功能,这也是Vector与其他List最大不同的地方,Vector是线程安全的。

  1. protected Object[] elementData; //数组作为底层存储器
  2.  
  3. protected int elementCount; //记录了当前有多少个元素了
  4.  
  5. protected int capacityIncrement; //每次扩展的时候需要扩展的数量
  6.  
  7. public Vector(int initialCapacity, int capacityIncrement) {
  8. super();
  9. if (initialCapacity < 0)
  10. throw new IllegalArgumentException("Illegal Capacity: "+
  11. initialCapacity);
  12. this.elementData = new Object[initialCapacity];
  13. this.capacityIncrement = capacityIncrement;
  14. }
  15.  
  16. public Vector(int initialCapacity) {
  17. this(initialCapacity, 0);
  18. }
  19.  
  20. public Vector() {
  21. this(10);
  22. }
  23.  
  24. public Vector(Collection<? extends E> c) {
  25. elementData = c.toArray();
  26. elementCount = elementData.length;
  27. // c.toArray might (incorrectly) not return Object[] (see 6260652)
  28. if (elementData.getClass() != Object[].class)
  29. elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
  30. }

这里面提供了与ArrayList相同的各种操作,下面简单说明一下:

1.复制内容到指定的数组,如果数组不够大,则抛出越界错误。

  1. public synchronized void copyInto(Object[] anArray) {
  2. System.arraycopy(elementData, 0, anArray, 0, elementCount);
  3. }

这里使用了System.arraycopy,后面还有很多地方使用了这个函数,其时间复杂度是o(n)。

2.在使用Vector的过程中,会出现需要扩容的情况,扩容之后又删除元素,就会造成很多元素空间被浪费的情况,如果担心浪费空间,就可以调用下面的函数进行无用空间trim:

  1. public synchronized void trimToSize() {
  2. modCount++;
  3. int oldCapacity = elementData.length;
  4. if (elementCount < oldCapacity) {
  5. elementData = Arrays.copyOf(elementData, elementCount);
  6. }
  7. }

可以看出这里就是开辟了一个正好需要的空间,把元素都拷贝到这个空间中,原来的数组空间会在vm空闲的时候进行回收。

3.上面说到了在数组容量不够的时候,会进行扩容,这里列出了一系列的包括扩容和获取与判断数组大小的函数:

  1. public synchronized void ensureCapacity(int minCapacity) {//进行容量调整
  2. if (minCapacity > 0) {
  3. modCount++;
  4. ensureCapacityHelper(minCapacity);
  5. }
  6. }
  7.  
  8. private void ensureCapacityHelper(int minCapacity) {//进行容量调整
  9. // overflow-conscious code
  10. if (minCapacity - elementData.length > 0)//只有指定的容量大于元素个数的时候才进行容量调整
  11. grow(minCapacity);
  12. }
  13.  
  14. private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  15.  
  16. private void grow(int minCapacity) {//扩容操作
  17. // overflow-conscious code
  18. int oldCapacity = elementData.length;//目前的容量
  19. int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
  20. capacityIncrement : oldCapacity);//计算目标容量,如果指定了每次扩展的量,直接增加,如果没有就直接翻倍
  21. if (newCapacity - minCapacity < 0)
  22. newCapacity = minCapacity;//如果目标容量小于指定的容量,则调整为指定容量,注意:也就是说如果指定的容量没有达到自动计算的目标容量,则直接采计算的目标容量
  23. if (newCapacity - MAX_ARRAY_SIZE > 0)
  24. newCapacity = hugeCapacity(minCapacity);//如果超过了容量上限,则直接使用最大上限
  25. elementData = Arrays.copyOf(elementData, newCapacity);//开辟一个新空间,拷贝数据到新空间,旧空间将被VM自动回收
  26. }
  27.  
  28. private static int hugeCapacity(int minCapacity) {
  29. if (minCapacity < 0) // overflow
  30. throw new OutOfMemoryError();
  31. return (minCapacity > MAX_ARRAY_SIZE) ?//其实不明白只有8的差距,为什么这么纠结
  32. Integer.MAX_VALUE :
  33. MAX_ARRAY_SIZE;
  34. }
  35.  
  36. public synchronized void setSize(int newSize) {//设置当前Vector的元素数量
  37. modCount++;
  38. if (newSize > elementCount) {//比当前的元素个数大,就扩容,还不一定需要扩,看ensureCapacityHelper就明白
  39. ensureCapacityHelper(newSize);
  40. } else {
  41. for (int i = newSize ; i < elementCount ; i++) {//如果比当前的元素个数少,就把多出来的元素都抹掉
  42. elementData[i] = null;
  43. }
  44. }
  45. elementCount = newSize;//调整当前元素的个数
  46. }
  47.  
  48. public synchronized int capacity() {
  49. return elementData.length;
  50. }
  51.  
  52. public synchronized int size() {
  53. return elementCount;
  54. }
  55.  
  56. public synchronized boolean isEmpty() {
  57. return elementCount == 0;
  58. }

上面的就是整个的扩容和设置元素数量等的一些操作函数的解释。

4.查找对象所在的index是List的一个重要的操作,其对应的主要的函数如下所示:

  1. public boolean contains(Object o) {//判断一个对象o是否在Vector中
  2. return indexOf(o, 0) >= 0;
  3. }
  4.  
  5. public int indexOf(Object o) {//判断一个对象o在Vector中第一次出现的位置
  6. return indexOf(o, 0);
  7. }
  8.  
  9. public synchronized int indexOf(Object o, int index) {//判断一个对象在Vector中的index之后第一次出现的位置
  10. if (o == null) {
  11. for (int i = index ; i < elementCount ; i++)
  12. if (elementData[i]==null)
  13. return i;
  14. } else {
  15. for (int i = index ; i < elementCount ; i++)//使用遍历对比的方式进行判断
  16. if (o.equals(elementData[i]))
  17. return i;
  18. }
  19. return -1;
  20. }
  21.  
  22. public synchronized int lastIndexOf(Object o) {//判断在Vector中自后向前查找对象o的第一次出现的位置
  23. return lastIndexOf(o, elementCount-1);
  24. }
  25.  
  26. public synchronized int lastIndexOf(Object o, int index) {
  27. if (index >= elementCount)
  28. throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
  29.  
  30. if (o == null) {
  31. for (int i = index; i >= 0; i--)
  32. if (elementData[i]==null)
  33. return i;
  34. } else {
  35. for (int i = index; i >= 0; i--)//自后向前倒着遍历这个Vector查找
  36. if (o.equals(elementData[i]))
  37. return i;
  38. }
  39. return -1;
  40. }

可以看出,只要涉及到确定Vector中对象o的位置,就需要遍历对比查找,而遍历对比查找的时间复杂度是o(n)

5.根据index对Vector进行操作也有一系列的函数:

  1. public synchronized E elementAt(int index) {//根据index获取Vector中的元素
  2. if (index >= elementCount) {
  3. throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
  4. }
  5.  
  6. return elementData(index);
  7. }
  8.  
  9. public synchronized E firstElement() {//获取第一个元素
  10. if (elementCount == 0) {
  11. throw new NoSuchElementException();
  12. }
  13. return elementData(0);
  14. }
  15.  
  16. public synchronized E lastElement() {//获取最后一个元素
  17. if (elementCount == 0) {
  18. throw new NoSuchElementException();
  19. }
  20. return elementData(elementCount - 1);
  21. }
  22.  
  23. public synchronized void setElementAt(E obj, int index) {//设置位置为index的元素
  24. if (index >= elementCount) {
  25. throw new ArrayIndexOutOfBoundsException(index + " >= " +
  26. elementCount);
  27. }
  28. elementData[index] = obj;//直接设置,时间复杂度是o(1)
  29. }
  30.  
  31. public synchronized void removeElementAt(int index) {//删除位置为index的元素
  32. modCount++;
  33. if (index >= elementCount) {
  34. throw new ArrayIndexOutOfBoundsException(index + " >= " +
  35. elementCount);
  36. }
  37. else if (index < 0) {
  38. throw new ArrayIndexOutOfBoundsException(index);
  39. }
  40. int j = elementCount - index - 1;
  41. if (j > 0) {//如果不是最后一个需要进行元素位移,这个时候的时间复杂度是o(n)
  42. System.arraycopy(elementData, index + 1, elementData, index, j);
  43. }
  44. elementCount--;
  45. elementData[elementCount] = null; /* to let gc do its work *///把最后的值为null
  46. }
  47.  
  48. public synchronized void insertElementAt(E obj, int index) {//在位置中间插入一个元素,时间复杂度是o(n)
  49. modCount++;
  50. if (index > elementCount) {
  51. throw new ArrayIndexOutOfBoundsException(index
  52. + " > " + elementCount);
  53. }
  54. ensureCapacityHelper(elementCount + 1);//先进行扩容
  55. System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);//让出位置,时间复杂度是o(n)
  56. elementData[index] = obj;//直接赋值
  57. elementCount++;//计数加一
  58. }
  59.  
  60. public synchronized void addElement(E obj) {
  61. modCount++;
  62. ensureCapacityHelper(elementCount + 1);
  63. elementData[elementCount++] = obj;//在队尾加入一个,时间复杂度是o(1)
  64. }
  65.  
  66. public synchronized boolean removeElement(Object obj) {//删除Vector中出现的第一个对象obj
  67. modCount++;
  68. int i = indexOf(obj);
  69. if (i >= 0) {
  70. removeElementAt(i);
  71. return true;
  72. }
  73. return false;
  74. }
  75.  
  76. public synchronized void removeAllElements() {//删除所有的元素,遍历,时间复杂度是o(n)
  77. modCount++;
  78. // Let gc do its work
  79. for (int i = 0; i < elementCount; i++)
  80. elementData[i] = null;
  81.  
  82. elementCount = 0;
  83. }
  84.  
  85. public synchronized Object clone() {//拷贝一个对象,时间复杂度是o(n)
  86. try {
  87. @SuppressWarnings("unchecked")
  88. Vector<E> v = (Vector<E>) super.clone();
  89. v.elementData = Arrays.copyOf(elementData, elementCount);
  90. v.modCount = 0;
  91. return v;
  92. } catch (CloneNotSupportedException e) {
  93. // this shouldn't happen, since we are Cloneable
  94. throw new InternalError(e);
  95. }
  96. }
  97.  
  98. public synchronized Object[] toArray() {//转换成数组,时间复杂度是o(n)
  99. return Arrays.copyOf(elementData, elementCount);
  100. }
  101.  
  102. @SuppressWarnings("unchecked")
  103. public synchronized <T> T[] toArray(T[] a) {//转换成数组,时间复杂度是o(n)
  104. if (a.length < elementCount)
  105. return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
  106.  
  107. System.arraycopy(elementData, 0, a, 0, elementCount);
  108.  
  109. if (a.length > elementCount)
  110. a[elementCount] = null;
  111.  
  112. return a;
  113. }
  114.  
  115. @SuppressWarnings("unchecked")
  116. E elementData(int index) {//获取index对应的对象,时间复杂度是o(1)
  117. return (E) elementData[index];
  118. }
  119.  
  120. public synchronized E get(int index) {//获取index对应的对象,时间复杂度是o(1)
  121. if (index >= elementCount)
  122. throw new ArrayIndexOutOfBoundsException(index);
  123.  
  124. return elementData(index);
  125. }
  126.  
  127. public synchronized E set(int index, E element) {//设置index对应的对象,时间复杂度是o(1)
  128. if (index >= elementCount)
  129. throw new ArrayIndexOutOfBoundsException(index);
  130.  
  131. E oldValue = elementData(index);
  132. elementData[index] = element;
  133. return oldValue;
  134. }
  135.  
  136. public synchronized boolean add(E e) //在最后添加一个对象,时间复杂度是o(1),但有可能触发扩容操作
  137. modCount++;
  138. ensureCapacityHelper(elementCount + 1);
  139. elementData[elementCount++] = e;
  140. return true;
  141. }
  142.  
  143. public boolean remove(Object o) {//删除第一次出现的对象o,时间复杂度是o(n)
  144. return removeElement(o);
  145. }
  146.  
  147. public void add(int index, E element) {//在inedx上添加一个元素,时间复杂度是o(n),因为会触发元素位移
  148. insertElementAt(element, index);
  149. }
  150.  
  151. public synchronized E remove(int index) {//删除index上面的元素,时间复杂度是o(n),因为会触发元素位移
  152. modCount++;
  153. if (index >= elementCount)
  154. throw new ArrayIndexOutOfBoundsException(index);
  155. E oldValue = elementData(index);
  156.  
  157. int numMoved = elementCount - index - 1;
  158. if (numMoved > 0)
  159. System.arraycopy(elementData, index+1, elementData, index,
  160. numMoved);
  161. elementData[--elementCount] = null; // Let gc do its work
  162.  
  163. return oldValue;
  164. }

以上是进行获取,增加和删除操作的时间复杂度。

再次强调,Vector在涉及到元素更改的函数之前都加了关键字synchronized,是线程安全的。

Java数据结构漫谈-Vector的更多相关文章

  1. Java数据结构漫谈-Stack

    Stack(栈)是一种比较典型的数据结构,其元素满足后进先出(LIFO)的特点. Java中Stack的实现继承自Vector,所以其天然的具有了一些Vector的特点,所以栈也是线程安全的. cla ...

  2. Java 数据结构之vector

    Vector 实现了一个动态数组.是可实现自动增长的对象数组. vector和arraylist的比较: 1.都是采用数组格式存储数据,索引数据块插入数据慢 2.ArrayList会比Vector快, ...

  3. Java数据结构漫谈-LinkedList

    同样是List的数据结构,LinkedList是使用了前后指针,指明节点的方式来表示链表的,这与之前介绍的ArrayList http://www.cnblogs.com/yakovchang/p/j ...

  4. Java数据结构漫谈-ArrayList

    ArrayList是一个基于数组实现的链表(List),这一点可以从源码中看出: transient Object[] elementData; // non-private to simplify ...

  5. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  6. Java数据结构之线性表

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  7. Java数据结构和算法 - 数组

    Q: 数组的创建? A: Java中有两种数据类型,基本类型和对象类型,在许多编程语言中(甚至面向对象语言C++),数组也是基本类型.但在Java中把数组当做对象来看.因此在创建数组时,必须使用new ...

  8. (6)Java数据结构-- 转:JAVA常用数据结构及原理分析

    JAVA常用数据结构及原理分析  http://www.2cto.com/kf/201506/412305.html 前不久面试官让我说一下怎么理解java数据结构框架,之前也看过部分源码,balab ...

  9. 自己动手实现java数据结构(一) 向量

    1.向量介绍 计算机程序主要运行在内存中,而内存在逻辑上可以被看做是连续的地址.为了充分利用这一特性,在主流的编程语言中都存在一种底层的被称为数组(Array)的数据结构与之对应.在使用数组时需要事先 ...

随机推荐

  1. 读书笔记_Effective_C++_条款二十二:将成员变量声明为private

    1.格式统一 在调用的时候,不会去想有没有(),一律是有get(),或者set()之类的. 2.封装 能直接访问得越少,表明封装性越高, 封装性越高,我们的顾虑就少了, 例如:我们a.data*0.9 ...

  2. 原生拖拽,拖放事件(drag and drop)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Chrome调试nodejs

    1.安装node-inspector 命令: npm install -g node-inspector 2.nodemon --debug xxx.js启动项目(如果使用--debug-brk 就会 ...

  4. CentOS(Linux) - SVN使用笔记(二) - 创建SVN仓库及下载仓库到本地

    1.安装: 参考文章 CentOS(Linux) - SVN使用笔记(一) -  安装SVN过程及开启和关闭svn服务指令 2.创建仓库 #创建项目目录 mkdir /usr/svn#进入目录cd / ...

  5. jquery实现弹出即消失的提示层

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Flask学习记录之Flask-Mail

    Flask-Mail可以连接到配置中的SMTP服务器,进行邮件发送,如果没有进行SMTP服务器的配置,将会默认连接到localhost上的 一.配置及初始化 (1)flask应用配置 #配置选项 MA ...

  7. http head

    Accept HTTP_ACCEPT Accept-Charset HTTP_ACCEPT-CHARSET Accept-Encoding HTTP_ACCEPT-ENCODING Accept-La ...

  8. iOS触摸事件深度解析-备用

    概述 本文主要解析从我们的手指触摸苹果设备到最终响应事件的整个处理机制.本质上讲,整个过程可以分为两个步骤: 步骤1:找目标.在iOS视图层次结构中找到触摸事件的最终接受者: 步骤2:事件响应.基于i ...

  9. jquery serialize的使用

    serialize() 方法通过序列化表单值,创建 URL 编码文本字符串. <!DOCTYPE html> <html lang="en"> <he ...

  10. Linux 杀死挂起的进程

    在用管理员执行一个命令后,用Ctrl+Z把命令转移到了后天.导致无法退出root的. 输入命令:exit终端显示:There are stopped jobs. 解决方法:方法一.输入命令:jobs终 ...