详解Java中的Vector
Vector实现了AbstractList抽象类和List接口,和ArrayList一样是基于Array存储的
Vector 是线程安全的,在大多数方法上存在synchronized关键字
//Vector存放的元素,初始化默认长度为10 protected Object[] elementData; //元素个数 protected int elementCount; //每次扩容大小,默认为0 protected int capacityIncrement; //构造函数,无指定初始化大小和无扩容大小 public Vector() { this(10); } //构造函数,指定初始化大小和无扩容大小 public Vector(int initialCapacity) { this(initialCapacity, 0); } //构造函数,指定初始化大小和扩容大小 public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } //构造函数,Collection集合 public Vector(Collection<? extends E> c) { elementData = c.toArray(); elementCount = elementData.length; if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); } //确保扩容的最小容量 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 void grow(int minCapacity) { int oldCapacity = elementData.length; //当扩容大小为0的时候,扩容为原来的2倍 int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); 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; }
- ensureCapacity(int minCapacity)方法确保Vector的最小长度,当扩容2倍小于minCapacity时,扩容到minCapacity大小,minCapacity不能小于0
- 最大长度为2的31次方-1
设置大小
public synchronized void setSize(int newSize) { modCount++; if (newSize > elementCount) { ensureCapacityHelper(newSize); } else { for (int i = newSize ; i < elementCount ; i++) { elementData[i] = null; } } elementCount = newSize; }
超过大小的被设置为Null
public synchronized void copyInto(Object[] anArray) { System.arraycopy(elementData, 0, anArray, 0, elementCount); } public synchronized void trimToSize() { modCount++; int oldCapacity = elementData.length; if (elementCount < oldCapacity) { elementData = Arrays.copyOf(elementData, elementCount); } } 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; }
是否为空
public synchronized boolean isEmpty() { return elementCount == 0; }
设置索引上的元素
public synchronized void setElementAt(E obj, int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } elementData[index] = obj; }
添加元素
public synchronized void addElement(E obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; }
扩容
插入元素
public synchronized void insertElementAt(E obj, int index) { modCount++; if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++; }
- 扩容
- 数组拷贝向索引后移动
- 删除为向前移动
删除元素
public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; }
只能删除第一个
-我是签名----------------------------
这里只是一个签名
详解
以上所述是小编给大家介绍的Java中的Vector,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
相关文章
Java反射之Call stack introspection详解
这篇文章主要介绍了Java反射之Call stack introspection详解,具有一定参考价值,需要的朋友可以了解下。2017-11-11Jrebel License Server 激活 IDEA-Jrebel-在线-
这篇文章主要介绍了Jrebel License Server 激活 IDEA-Jrebel-在线-离线-均适用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-12-12springboot+angular4前后端分离 跨域问题解决详解
这篇文章主要介绍了springboot+angular4前后端分离 跨域问题解决详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2019-09-09基于spring data jpa @query返回map的踩坑记录
这篇文章主要介绍了基于spring data jpa @query返回map的踩坑记录,具有很好的参考价值,如有错误或未考虑完全的地方,望不吝赐教2021-11-11intellij IDEA配置springboot的图文教程
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。接下来通过本文给大家介绍intellij IDEA配置springboot的图文教程,感兴趣的朋友一起看看吧2018-03-03Java中ThreadLocal使用原理及Synchronized区别
ThreadLocal叫做线程变量,本文详细的介绍了ThreadLocal使用原理及Synchronized区别,有需要的朋友可以参考一下,希望对你有所帮助。2023-05-05
最新评论