Vector笔记整理
2023-02-26 10:21:25 时间
Vector的结构与常量
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { //存放元素的数组 protected Object[] elementData; //元素的个数 protected int elementCount; //矢量容量的自动量当其尺寸大于其容量时递增。如果容量增量小于或等于零的矢量在每次需要增长时翻倍。(源码的解释) protected int 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; } public Vector(int initialCapacity) { this(initialCapacity, 0); } //无参的构造函数,默认的数组容量为10个元素 public Vector() { this(10); } ...}
扩容机制
(福利推荐:阿里云、腾讯云、华为云服务器最新限时优惠活动,云服务器1核2G仅88元/年、2核4G仅698元/3年,点击这里立即抢购>>>)
//ArrayList在需要扩容的时,新数组的容量会变为原来的1.5倍,Vector在不指定增长系数时,通常会增加为原来的两倍。 public synchronized void ensureCapacity(int minCapacity) { if (minCapacity > 0) { modCount++; ensureCapacityHelper(minCapacity); } } private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code //新增元素大于数组的长度,就进行扩容流程 if (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 int oldCapacity = elementData.length; //当增长矢量没有指定(0)时,扩容后的新数组长度为旧数组的两倍 oldCapacity + oldCapacity int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); //指定增长系数,校验是否符合 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); //临界值的处理,同ArrayList大同小异 elementData = Arrays.copyOf(elementData, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
增删改查
//增加元素 public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } //删除元素 public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; } public synchronized void removeAllElements() { modCount++; // Let gc do its work for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } //修改元素 public synchronized E set(int index, E element) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; } //查询 public synchronized E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); return elementData(index); }
总结
1.Vector与ArrayList实现原理是一样的,底层都使用了扩容数组,默认给的容量为10
2.Vector的一些常用方法都加了锁synchronized,这就导致了不必要的性能开销(不要同步方法的时候强制加锁,有点泛滥)
3.可以指定矢量增加系数capacityIncrement,但是在扩容流程中会进行校验判断是否符合实际要求:
int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); //指定增长系数,校验是否符合 if (newCapacity - minCapacity < 0) newCapacity = minCapacity;
如果未指定capacityIncrement则当需要时会扩容为原来的两倍,在性能造成了不必要的浪费(对比与ArrayList的1.5)。
4.大多数情况下,已经不在使用,如果需要同步操作可以有更好的替代类(concurrent下的集合)。
你还在原价购买阿里云、腾讯云、华为云、天翼云产品?那就亏大啦!现在申请成为四大品牌云厂商VIP用户,可以3折优惠价购买云服务器等云产品,并且可享四大云服务商产品终身VIP优惠价,还等什么?赶紧点击下面对应链接免费申请VIP客户吧:
相关文章
- RPG Maker游戏打包安卓APK教程
- WebUI自动化环境搭建
- Windows如何通过WSL2安装Redis7
- Selenium WebDriver API 学习笔记(二):浏览器控制
- Selenium WebDriver API 学习笔记(三):浏览器控制
- Python+Opencv解析一段视频并逐帧保存到本地
- Python+Opencv读取高帧率USB摄像头问题
- 如何与B&S建立EDI连接?
- 处理器基础知识总结
- 制作iOS应用程序,需要注意哪些问题?
- 纵有疾风起,Petterp与他的2022
- SOLIDWORKS Electrical 2023新功能
- 自学开发技术,从入门到入行
- 这一年,熬过许多夜,也有些许收获 | 2022年终总结
- 高质量编码-考勤记录日历可视化
- [oeasy]python0052_ raw格式字符串_单引号_双引号_反引号_ 退格键
- 包年包月与产品配置升级说明
- Echarts的legends 禁用点击事件但不影响鼠标悬浮事件
- 百毒文库,网盘,视频...-IDM多个版本(电脑、手机、浏览器插件都有)
- CleanMyMac X 2023最新详细的测评以及其最佳 Mac Cleaner 替代品