源码学习之Vector
Vector源码分析学习
同样,首先是Vector的定义
//继承AbstractList抽线类,实现了List、RandomAccess、Cloneable和Serializable接口 public class Vector<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, java.io.Serializable
Vector源码开头定义的几个成员变量
//这个就是实际存储数据的数组,从这个可以看出Vector是数组实现的 protected Object[] elementData; //Vector内有效元素的数量 protected int elementCount; //Vector容量增量,当capacityIncrement>0时,需要扩容时,增加的的是capacityIncrement,否则增加的容量是oldCapacity protected int capacityIncrement;
构造函数,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);}
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;}
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相关推荐
- srsLTE源码学习:度量中心:metrics_hub.h
Table of Contents metrics_hub.h PS:<srsLTE源码学习:绑核创建线程threads.h, threads.c> metrics_hub.h lib\i ...
- mongo源码学习(四)服务入口点ServiceEntryPoint
在上一篇博客mongo源码学习(三)请求接收传输层中,稍微分析了一下TransportLayer的作用,这篇来看下ServiceEntryPoint是怎么做的. 首先ServiceEntryPoint ...
- webrtc源码学习 - 点对点(P2P)链接过程(peer connection)
创建PC pc 是 peer connection 的简写,以下文章中pc 都特指 peer connection PeerConnection 是webrtc 中链接过程非常重要的接口,提供了包括, ...
- 博通Broadcom SDK源码学习与开发5——ECOS系统层剖析
声明:原创作品,严禁用于商业目的. 本系列文章将全面剖析以Bcm33xxx芯片开发Cablemodem产品的SDK源码为例,从编译系统到各个功能模块进行分析与探讨. 文章目录 0.写在前篇 1. Op ...
- fasttext源码学习(2)--模型压缩
fasttext源码学习(2)–模型压缩 前言 fasttext模型压缩的很明显,精度却降低不多,其网站上提供的语种识别模型,压缩前后的对比就是例证,压缩前126M,压缩后917K.太震惊了,必须学习 ...
- 【Android 源码学习】 init启动
目录 Android 源码学习 init启动 从main.cpp开始 init.cpp 部分逻辑 init启动zygote 属性服务 总结 Android 源码学习 init启动 Android 11 ...
- Apache log4j-1.2.17源码学习笔记
(1)Apache log4j-1.2.17源码学习笔记 http://blog.csdn.net/zilong_zilong/article/details/78715500 (2)Apache l ...
- 【Android 源码学习】Zygote启动原理
Android 源码学习 Zygote启动原理 望舒课堂 Zygote进程启动原理学习记录整理. Zygote简介 Zygote是进程在init进程启动时创建的,进程本身是app_process,来源 ...
- JAVA JDK 源码学习
JAVA JDK 源码学习 ,以1.8为例,按照下面图片顺序依次学习: applet ,awt,beans,io,lang,math,net,nio,rmi,security,sql,text,tim ...
最新文章
- 为什么数据库连接池不采用 IO 多路复用?
- postgres 物理格式
- 理解Linux系统负荷
- 【NOIP模拟】T1 发电机(递推逆元+期望)
- 大话数据结构07 :链表栈
- 为什么待办事项清单不管用
- jquery(js) 增加 删除 修改属性样式、元素内容
- 【Python CheckiO 题解】Roman Numerals
- C#开发高亮语法编辑器(一)——TextBox ,RichTextBox
- SPOJ - SUBLEX 【后缀自动机】
- 快视频:剽窃了B站的数据库?360的常规操作了。
- Silverlight访问Web Service报System.Security.SecurityException: 安全性错误的处理
- 常用js(javascript)函数
- BeautifulSoup库
- Django__WSGI
- fortan程序设计
- 医院绩效考核系统设计方案
- QWebEngineView播放视频最大化
- WingIDE 7.2.0 远程调试
- PPT里面的背景音乐找不到?