Vector源码分析学习

  1. 同样,首先是Vector的定义

    //继承AbstractList抽线类,实现了List、RandomAccess、Cloneable和Serializable接口
    public class Vector<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable
  2. Vector源码开头定义的几个成员变量

    //这个就是实际存储数据的数组,从这个可以看出Vector是数组实现的
    protected Object[] elementData;
    //Vector内有效元素的数量
    protected int elementCount;
    //Vector容量增量,当capacityIncrement>0时,需要扩容时,增加的的是capacityIncrement,否则增加的容量是oldCapacity
    protected int capacityIncrement;
  3. 构造函数,Vector一共提供了三个构造函数。

        //构造函数,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;}//构造函数,initialCapacity初始化容量,capacityIncrement设置为0public Vector(int initialCapacity) {this(initialCapacity, 0);}//构造函数,initialCapacity设置为10,capacityIncrement设置为0public Vector() {this(10);}//构造函数,从另外一个集合初始化Vectorpublic Vector(Collection<? extends E> c) {elementData = c.toArray();elementCount = elementData.length;// c.toArray might (incorrectly) not return Object[] (see 6260652)if (elementData.getClass() != Object[].class)elementData = Arrays.copyOf(elementData, elementCount, Object[].class);}
  4. Vector扩容机制

      //保证容量public synchronized void ensureCapacity(int minCapacity) {if (minCapacity > 0) {modCount++;ensureCapacityHelper(minCapacity);}}//保证容量,如果minCapacity大于Vector的容量,那么需要扩容private void ensureCapacityHelper(int minCapacity) {// overflow-conscious codeif (minCapacity - elementData.length > 0)grow(minCapacity);}private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;private void grow(int minCapacity) {// overflow-conscious code//如果capacityIncrement大于0的时候,扩容capacityIncrement,否则扩容oldCapacityint oldCapacity = elementData.length;int newCapacity = oldCapacity + ((capacityIncrement > 0) ?capacityIncrement : oldCapacity);//如果新容量不能满足最小容量要求,那么新容量设置成minCapacityif (newCapacity - minCapacity < 0)newCapacity = minCapacity;//如果新容量大于MAX_ARRAY_SIZE,那么新容量设置成Integer.MAX_VALUEif (newCapacity - MAX_ARRAY_SIZE > 0)newCapacity = hugeCapacity(minCapacity);elementData = Arrays.copyOf(elementData, newCapacity);}private static int hugeCapacity(int minCapacity) {if (minCapacity < 0) // overflowthrow new OutOfMemoryError();return (minCapacity > MAX_ARRAY_SIZE) ?Integer.MAX_VALUE :MAX_ARRAY_SIZE;}
  5. Vector常用操作

     //返回Vector的size()public synchronized int size() {return elementCount;}//判断Vector是否是空public synchronized boolean isEmpty() {return elementCount == 0;}//判断是否包含o元素public boolean contains(Object o) {return indexOf(o, 0) >= 0;}//返回第一个出现o的下标   public synchronized int indexOf(Object o, int index) {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;}
    //获取index下标的元素值public synchronized E get(int index) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);return elementData(index);}
    //设置index,下标的元素值为elementpublic synchronized E set(int index, E element) {if (index >= elementCount)throw new ArrayIndexOutOfBoundsException(index);E oldValue = elementData(index);elementData[index] = element;return oldValue;}
    //增加一个元素epublic synchronized boolean add(E e) {modCount++;//检查是否需要扩容ensureCapacityHelper(elementCount + 1);elementData[elementCount++] = e;return true;}//移除vector中等于o的元素public boolean remove(Object o) {return removeElement(o);}//在下标index处增加了一个elementpublic void add(int index, E element) {insertElementAt(element, index);}//移除下标为index的元素public synchronized E remove(int index) {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);elementData[--elementCount] = null; // Let gc do its workreturn oldValue;}
    

源码学习之Vector相关推荐

  1. srsLTE源码学习:度量中心:metrics_hub.h

    Table of Contents metrics_hub.h PS:<srsLTE源码学习:绑核创建线程threads.h, threads.c> metrics_hub.h lib\i ...

  2. mongo源码学习(四)服务入口点ServiceEntryPoint

    在上一篇博客mongo源码学习(三)请求接收传输层中,稍微分析了一下TransportLayer的作用,这篇来看下ServiceEntryPoint是怎么做的. 首先ServiceEntryPoint ...

  3. webrtc源码学习 - 点对点(P2P)链接过程(peer connection)

    创建PC pc 是 peer connection 的简写,以下文章中pc 都特指 peer connection PeerConnection 是webrtc 中链接过程非常重要的接口,提供了包括, ...

  4. 博通Broadcom SDK源码学习与开发5——ECOS系统层剖析

    声明:原创作品,严禁用于商业目的. 本系列文章将全面剖析以Bcm33xxx芯片开发Cablemodem产品的SDK源码为例,从编译系统到各个功能模块进行分析与探讨. 文章目录 0.写在前篇 1. Op ...

  5. fasttext源码学习(2)--模型压缩

    fasttext源码学习(2)–模型压缩 前言 fasttext模型压缩的很明显,精度却降低不多,其网站上提供的语种识别模型,压缩前后的对比就是例证,压缩前126M,压缩后917K.太震惊了,必须学习 ...

  6. 【Android 源码学习】 init启动

    目录 Android 源码学习 init启动 从main.cpp开始 init.cpp 部分逻辑 init启动zygote 属性服务 总结 Android 源码学习 init启动 Android 11 ...

  7. Apache log4j-1.2.17源码学习笔记

    (1)Apache log4j-1.2.17源码学习笔记 http://blog.csdn.net/zilong_zilong/article/details/78715500 (2)Apache l ...

  8. 【Android 源码学习】Zygote启动原理

    Android 源码学习 Zygote启动原理 望舒课堂 Zygote进程启动原理学习记录整理. Zygote简介 Zygote是进程在init进程启动时创建的,进程本身是app_process,来源 ...

  9. JAVA JDK 源码学习

    JAVA JDK 源码学习 ,以1.8为例,按照下面图片顺序依次学习: applet ,awt,beans,io,lang,math,net,nio,rmi,security,sql,text,tim ...

最新文章

  1. 为什么数据库连接池不采用 IO 多路复用?
  2. postgres 物理格式
  3. 理解Linux系统负荷
  4. 【NOIP模拟】T1 发电机(递推逆元+期望)
  5. 大话数据结构07 :链表栈
  6. 为什么待办事项清单不管用
  7. jquery(js) 增加 删除 修改属性样式、元素内容
  8. 【Python CheckiO 题解】Roman Numerals
  9. C#开发高亮语法编辑器(一)——TextBox ,RichTextBox
  10. SPOJ - SUBLEX 【后缀自动机】
  11. 快视频:剽窃了B站的数据库?360的常规操作了。
  12. Silverlight访问Web Service报System.Security.SecurityException: 安全性错误的处理
  13. 常用js(javascript)函数
  14. BeautifulSoup库
  15. Django__WSGI
  16. fortan程序设计
  17. 医院绩效考核系统设计方案
  18. QWebEngineView播放视频最大化
  19. WingIDE 7.2.0 远程调试
  20. PPT里面的背景音乐找不到?

热门文章

  1. Python 条件控制 — if语句
  2. vue中通过定时器设置倒计时,5秒倒计时
  3. OpenCV之视频截取
  4. java如何截取视频文件_Java获取视频时长及截取帧截图详解
  5. win10电脑用蓝牙实现文件传输,安卓手机通过蓝牙将文件传送到电脑
  6. 电脑计算机界面打开后无法缩小,技术编辑演示win10系统iE网页界面大小无法缩放的办法...
  7. 微软三月十日发行两个安全补丁
  8. token的使用方法
  9. 优秀程序猿写技术文档的正确姿势
  10. 电脑硬件及电脑配置知识大全