【版权声明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
https://blog.csdn.net/m0_69908381/article/details/129917905
出自【进步*于辰的博客】
文章目录
- 1、概述
- 2、构造方法摘要
- 3、方法摘要
- 3.1 boolean add(E o)
- 3.2 void add(int index, E element)
- 3.3 boolean addAll(Collection<? extends E> c)
- 3.4 boolean addAll(int index, Collection<? extends E> c)
- 3.5 void addElement(E obj)
- 3.6 int capacity()
- 3.7 void clear()
- 3.8 Object clone()
- 3.9 boolean contains(Object elem)
- 3.10 boolean containsAll(Collection<?> c)
- 3.11 void copyInto(Object[] anArray)
- 3.12 E elementAt(int index)
- 3.13 Enumeration`<E>` elements()
- 3.14 void ensureCapacity(int minCapacity)
- 3.15 boolean equals(Object o)
- 3.16 E firstElement()
- 3.17 E get(int index)
- 3.18 int hashCode()
- 3.19 int indexOf(Object elem)
- 3.20 int indexOf(Object elem, int index)
- 3.21 void insertElementAt(E obj, int index)
- 3.22 boolean isEmpty()
- 3.23 E lastElement()
- 3.24 int lastIndexOf(Object elem)
- 3.25 int lastIndexOf(Object elem, int index)
- 3.26 E remove(int index)
- 3.27 boolean remove(Object o)
- 3.28 boolean removeAll(Collection<?> c)
- 3.29 void removeAllElements()
- 3.30 boolean removeElement(Object obj)
- 3.31 void removeElementAt(int index)
- 3.32 protected void removeRange(int fromIndex, int toIndex)
- 3.33 boolean retainAll(Collection<?> c)
- 3.34 E set(int index, E element)
- 3.35 void setElementAt(E obj, int index)
- 3.36 void setSize(int newSize)
- 3.37 int size()
- 3.38 List`<E>` subList(int fromIndex, int toIndex)
- 3.39 Object[] toArray()
- 3.40 `<T>` T[] toArray(T[] a)
- 3.41 String toString()
- 3.42 void trimToSize()
- 3.43 synchronized void forEach(Consumer<? super E> action)
- 4.44 synchronized void replaceAll(UnaryOperator<E> operator)
- 3.45 synchronized boolean removeIf(Predicate<? super E> filter)
- 4、方法摘要(不开放)
- 最后
1、概述
继承关系:
- java.lang.Object
- java.util.AbstractCollection
<E>
- java.util.AbstractList
<E>
- java.util.Vector
<E>
- java.util.Vector
- java.util.AbstractList
- java.util.AbstractCollection
所有已实现的接口:
Serializable、Cloneable、Iterable<E>
、Collection<E>
、List<E>
、RandomAccess
直接已知子类:
Stack
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Vector 类可以实现可增长的对象数组。与数组一样,它包含可以使用 整数索引 整数索引 整数索引进行访问的组件。但是,Vector 的大小可以根据需要增大或缩小,以适应创建 Vector 后进行添加或移除项的操作。
每个向量会试图通过维护 capacity
和 capacityIncrement
来优化存储管理。capacity
始终至少应与向量的大小相等;这个值通常比后者大些,因为随着将组件添加到向量中,其存储将按 capacityIncrement
的大小增加存储块。应用程序可以在插入大量组件前增加向量的容量;这样就减少了增加的重分配的量。
从 Java 2 平台 v1.2 开始,已改进此类以实现 List,这样它就成为了 Java 的集合框架的一部分。与新集合的实现不同,Vector 是同步的。
由 Vector 的 iterator()
和 listIterator()
方法所返回的迭代器是 快速失败 的:如果在迭代器创建后的任意时间从结构上修改了向量(通过迭代器自身的 remove()
或 add()
方法之外的任何其他方式),则迭代器将抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就完全失败,而不是冒着在将来不确定的时间任意发生不确定行为的风险。Vector 的 elements()
返回的 Enumeration 不是 快速失败的。
注意,迭代器的快速失败行为不能得到保证,一般来说,存在不同步的并发修改时,不可能作出任何坚决的保证。快速失败迭代器尽最大努力抛出 ConcurrentModificationException。因此,编写依赖于此异常的程序的方式是错误的,正确做法是:迭代器的快速失败行为应该仅用于检测 bug。
此类是 Java Collections Framework 的成员。
从以下版本开始:
JDK1.0
另请参见:
Collection<E>
、List<E>
、ArrayList<E>
、LinkedList<E>
、序列化表格
2、构造方法摘要
属性说明:
protected Object[] elementData;// 数组缓冲区
protected int elementCount;// 有效组件数
// 向量的大小大于其容量时,容量自动增加的量
protected int capacityIncrement;
2.1 null
构造一个空向量,使其内部数据数组的大小为 10
,其标准容量增量为零。
仅调用第3项。
2.2 Collection<? extends E> c
构造一个包含指定集合中的元素的向量,这些元素按其集合的迭代器返回元素的顺序排列。
public Vector(Collection<? extends E> c) {
Object[] a = c.toArray();
elementCount = a.length;
if (c.getClass() == ArrayList.class) {
elementData = a;
} else {
// 从 a 中复制 elementCount 个组件到 elementData
elementData = Arrays.copyOf(a, elementCount, Object[].class);
}
}
toArray()
见第4.39项;copyOf()
见Arrays类的第2.18项。
2.3 int initialCapacity
用指定的初始容量和等于零的容量增量构造一个空向量。
仅调用下1项。指定当向量溢出时容量增加的量为 0
。
2.4 int initialCapacity, 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;
}
3、方法摘要
3.1 boolean add(E o)
将指定元素追加到此向量的末尾。
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);// 见第4.1项
elementData[elementCount++] = e;
return true;
}
3.2 void add(int index, E element)
在此向量的指定位置插入指定的元素。
public void add(int index, E element) {
insertElementAt(element, index);// 见第21项
}
3.3 boolean addAll(Collection<? extends E> c)
将指定 Collection 中的所有元素追加到此向量的末尾,按照指定集合的迭代器所返回的顺序追加这些元素。
public synchronized boolean addAll(Collection<? extends E> c) {
modCount++;
Object[] a = c.toArray();// 见第39项
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);// 见第4.1项
System.arraycopy(a, 0, elementData, elementCount, numNew);
elementCount += numNew;
return numNew != 0;
}
此方法与列表类的第3.3项在逻辑上几乎完全相同。
3.4 boolean addAll(int index, Collection<? extends E> c)
在指定位置将指定 Collection 中的所有元素插入到此向量中。
toArray()
见第39项;ensureCapacityHelper()
见第4.1项。
此方法与列表类的第3.4项在逻辑上几乎完全相同。
3.5 void addElement(E obj)
将指定的组件添加到此向量的末尾,将其大小增加 1。
public synchronized void addElement(E obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);// 见第4.1项
elementData[elementCount++] = obj;
}
3.6 int capacity()
返回此向量的当前容量。
3.7 void clear()
从此向量中移除所有元素。
public void clear() {
removeAllElements();// 调用第29项
}
3.8 Object clone()
返回向量的一个副本。
3.9 boolean contains(Object elem)
测试指定的对象是否为此向量中的组件。
public boolean contains(Object o) {
return indexOf(o, 0) >= 0;// 调用第20项,指定检索开始索引为 0
}
3.10 boolean containsAll(Collection<?> c)
如果此向量包含指定 Collection 中的所有元素,则返回 true。
3.11 void copyInto(Object[] anArray)
将此向量的组件复制到指定的数组中。
3.12 E elementAt(int index)
返回指定索引处的组件。
3.13 Enumeration<E>
elements()
返回此向量的组件的枚举。
3.14 void ensureCapacity(int minCapacity)
增加此向量的容量(如有必要),以确保其至少能够保存最小容量参数指定的组件数。
public synchronized void ensureCapacity(int minCapacity) {
if (minCapacity > 0) {
modCount++;
ensureCapacityHelper(minCapacity);// 见第4.1项
}
}
3.15 boolean equals(Object o)
比较指定对象与此向量的相等性。
3.16 E firstElement()
返回此向量的第一个组件(位于索引 0 处的项)。
3.17 E get(int index)
返回向量中指定位置的元素。
public synchronized E get(int index) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
return elementData(index);
}
3.18 int hashCode()
返回此向量的哈希码值。
3.19 int indexOf(Object elem)
搜索给定参数的第一个匹配项,使用 equals()
测试相等性。
public int indexOf(Object o) {
return indexOf(o, 0);// 见下 1项,指定检索开始索引为 0
}
3.20 int indexOf(Object elem, int index)
搜索给定参数的第一个匹配项,从 index
处开始搜索,并使用 equals()
测试其相等性。
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;
}
3.21 void insertElementAt(E obj, int index)
将指定对象作为此向量中的组件插入到指定的 index
处。
public synchronized void insertElementAt(E obj, int index) {
modCount++;
if (index > elementCount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
ensureCapacityHelper(elementCount + 1);// 见第4.1项
System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
elementData[index] = obj;
elementCount++;
}
arraycopy()
见 System 类的第2.1项。
其他源码与列表类的第3.2项在逻辑上几乎完全相同。
3.22 boolean isEmpty()
测试此向量是否不包含组件。
3.23 E lastElement()
返回此向量的最后一个组件。
3.24 int lastIndexOf(Object elem)
返回指定的对象在此向量中最后一个匹配项的索引。
3.25 int lastIndexOf(Object elem, int index)
向后搜索指定的对象,从指定的索引处开始搜索,并返回一个索引。
3.26 E remove(int 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 work
return oldValue;
}
arraycopy()
见 System 类的第2.1项。
此方法与列表类的第3.9项在逻辑上几乎完全相同。
3.27 boolean remove(Object o)
移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,则元素保持不变。
3.28 boolean removeAll(Collection<?> c)
从此向量中移除包含在指定 Collection 中的所有元素。
3.29 void removeAllElements()
从此向量中移除全部组件,并将其大小设置为零。
public synchronized void removeAllElements() {
modCount++;
// Let gc do its work
for (int i = 0; i < elementCount; i++)
elementData[i] = null;
elementCount = 0;
}
移除元素是将全部组件置空,而不是重新构造elementData
,故列表容量不变、有效组件个数为0
、结构修改次数+1
。
3.30 boolean removeElement(Object obj)
从此向量中移除变量的第一个(索引最小的)匹配项。
3.31 void removeElementAt(int index)
删除指定索引处的组件。
3.32 protected void removeRange(int fromIndex, int toIndex)
从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。
3.33 boolean retainAll(Collection<?> c)
在此向量中仅保留包含在指定 Collection 中的元素。
3.34 E set(int index, E element)
用指定的元素替换此向量中指定位置处的元素。
3.35 void setElementAt(E obj, int index)
将此向量指定 index 处的组件设置为指定的对象。
3.36 void setSize(int newSize)
设置此向量的大小。
3.37 int size()
返回此向量中的组件数。
3.38 List<E>
subList(int fromIndex, int toIndex)
返回此 List 的部分视图,元素范围为从 fromIndex(包括)到 toIndex(不包括)。
3.39 Object[] toArray()
返回一个数组,包含此向量中以正确顺序存放的所有元素。
public synchronized Object[] toArray() {
return Arrays.copyOf(elementData, elementCount);
}
copyOf()
见 Arrays 类的第2.19项。
3.40 <T>
T[] toArray(T[] a)
返回一个数组,包含此向量中以正确顺序存放的所有元素;返回数组的运行时类型为指定数组的类型。
public synchronized <T> T[] toArray(T[] a) {
if (a.length < elementCount)
return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
System.arraycopy(elementData, 0, a, 0, elementCount);
if (a.length > elementCount)
a[elementCount] = null;
return a;
}
copyOf()
见 Arrays 类第2.18项;arraycopy()
见 System 类的第2.1项。
此方法与列表类的第3.14项在逻辑上几乎完全相同。
3.41 String toString()
返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式。
3.42 void trimToSize()
对此向量的容量进行微调,使其等于向量的当前大小。
3.43 synchronized void forEach(Consumer<? super E> action)
遍历所有元素执行指定的逻辑。
4.44 synchronized void replaceAll(UnaryOperator operator)
遍历所有元素执行指定的逻辑,并替换旧元素。
3.45 synchronized boolean removeIf(Predicate<? super E> filter)
(业务不明。)
说明:第43~45三个方法与列表类的第 3.17 ~ 3.19 对应项相同(包括业务和源码)。
4、方法摘要(不开放)
4.1 private void ensureCapacityHelper(int minCapacity)
增加此向量的容量(如有必要)。
private void ensureCapacityHelper(int minCapacity) {
if (minCapacity - elementData.length > 0)
grow(minCapacity);// 见下1项,若所需容量大于当前容量,扩容
}
4.2 private void grow(int minCapacity)
根据指定容量扩容。
copyOf()
见 Arrays 类第2.19项。
红框外与列表类的第5.6项在逻辑上几乎完全相同。
最后
如果大家需要Java-API文档,我上传了《Java-API文档-包含5/8/11三个版本》。
本文暂缓更新。