public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Vector类实现了可扩展的对象数组。
像数组一样,它包含可以使用整数索引访问的组件。
但是, Vector的大小可以根据需要增长或缩小,以适应在创建Vector之后添加和删除项目。
每个向量尝试通过维护capacity和capacityIncrement优化存储capacityIncrement 。 capacity总是至少与矢量大小一样大; 通常较大,因为当向量中添加组分时,向量的存储空间大小capacityIncrement 。 应用程序可以在插入大量组件之前增加向量的容量; 这减少了增量重新分配的数量。
The iterators returned by this class's个 iterator和listIterator方法是快速失败的 :如果向量在任何时间从结构上修改创建迭代器之后,以任何方式除非通过迭代器自身remove种或add方法,迭代器都将抛出一个ConcurrentModificationException 。 因此,面对并发修改,迭代器将快速而干净地失败,而不是在未来未确定的时间冒着任意的非确定性行为。 由elements返回的Enumerations 不是故障快速的。
请注意,迭代器的故障快速行为无法保证,因为一般来说,在不同步并发修改的情况下,无法做出任何硬性保证。 失败快速迭代器尽力投入ConcurrentModificationException 。 因此,编写依赖于此异常的程序的正确性将是错误的:迭代器的故障快速行为应仅用于检测错误。
从Java 2平台v1.2,这个类被改造为实现List接口,使其成为成员Java Collections Framework 。 与新集合实现不同, Vector是同步的。 如果不需要线程安全的实现,建议使用ArrayList代替Vector 。
Collection , LinkedList , Serialized Form
| Modifier and Type | Field and Description |
|---|---|
protected int |
capacityIncrement
当矢量的大小大于其容量时,矢量的容量自动增加的量。
|
protected int |
elementCount
该
Vector对象中有效组件的数量。
|
protected Object[] |
elementData
存储向量的组件的阵列缓冲区。
|
modCount| Constructor and Description |
|---|
Vector()
构造一个空向量,使其内部数据数组的大小为
10 ,标准容量增量为零。
|
Vector(Collection<? extends E> c)
构造一个包含指定集合元素的向量,按照集合的迭代器返回的顺序。
|
Vector(int initialCapacity)
构造具有指定初始容量并且其容量增量等于零的空向量。
|
Vector(int initialCapacity, int capacityIncrement)
构造具有指定的初始容量和容量增量的空向量。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
将指定的元素追加到此Vector的末尾。
|
void |
add(int index, E element)
在此Vector中的指定位置插入指定的元素。
|
boolean |
addAll(Collection<? extends E> c)
将指定集合中的所有元素追加到该向量的末尾,按照它们由指定集合的迭代器返回的顺序。
|
boolean |
addAll(int index, Collection<? extends E> c)
将指定集合中的所有元素插入到此向量中的指定位置。
|
void |
addElement(E obj)
将指定的组件添加到此向量的末尾,将其大小增加1。
|
int |
capacity()
返回此向量的当前容量。
|
void |
clear()
从此Vector中删除所有元素。
|
Object |
clone()
返回此向量的克隆。
|
boolean |
contains(Object o)
如果此向量包含指定的元素,则返回
true 。
|
boolean |
containsAll(Collection<?> c)
如果此向量包含指定集合中的所有元素,则返回true。
|
void |
copyInto(Object[] anArray)
将此向量的组件复制到指定的数组中。
|
E |
elementAt(int index)
返回指定索引处的组件。
|
Enumeration<E> |
elements()
返回此向量的组件的枚举。
|
void |
ensureCapacity(int minCapacity)
如果需要,增加此向量的容量,以确保它可以至少保存最小容量参数指定的组件数。
|
boolean |
equals(Object o)
将指定的对象与此向量进行比较以获得相等性。
|
E |
firstElement()
返回此向量的第一个组件(索引号为
0的项目)。
|
void |
forEach(Consumer<? super E> action)
对
Iterable的每个元素执行给定的操作,直到所有元素都被处理或动作引发异常。
|
E |
get(int index)
返回此向量中指定位置的元素。
|
int |
hashCode()
返回此Vector的哈希码值。
|
int |
indexOf(Object o)
返回此向量中指定元素的第一次出现的索引,如果此向量不包含元素,则返回-1。
|
int |
indexOf(Object o, int index)
返回此向量中指定元素的第一次出现的索引,从
index向前
index ,如果未找到该元素,则返回-1。
|
void |
insertElementAt(E obj, int index)
在指定的index插入指定对象作为该向量中的一个
index 。
|
boolean |
isEmpty()
测试此矢量是否没有组件。
|
Iterator<E> |
iterator()
以正确的顺序返回该列表中的元素的迭代器。
|
E |
lastElement()
返回向量的最后一个组件。
|
int |
lastIndexOf(Object o)
返回此向量中指定元素的最后一次出现的索引,如果此向量不包含元素,则返回-1。
|
int |
lastIndexOf(Object o, int index)
返回此向量中指定元素的最后一次出现的索引,从
index ,如果未找到元素,则返回-1。
|
ListIterator<E> |
listIterator()
返回列表中的列表迭代器(按适当的顺序)。
|
ListIterator<E> |
listIterator(int index)
从列表中的指定位置开始,返回列表中的元素(按正确顺序)的列表迭代器。
|
E |
remove(int index)
删除此向量中指定位置的元素。
|
boolean |
remove(Object o)
删除此向量中指定元素的第一个出现如果Vector不包含元素,则它不会更改。
|
boolean |
removeAll(Collection<?> c)
从此Vector中删除指定集合中包含的所有元素。
|
void |
removeAllElements()
从该向量中删除所有组件,并将其大小设置为零。
|
boolean |
removeElement(Object obj)
从此向量中删除参数的第一个(最低索引)出现次数。
|
void |
removeElementAt(int index)
删除指定索引处的组件。
|
boolean |
removeIf(Predicate<? super E> filter)
删除满足给定谓词的此集合的所有元素。
|
protected void |
removeRange(int fromIndex, int toIndex)
从此列表中删除所有索引为
fromIndex (含)和
toIndex之间的元素。
|
void |
replaceAll(UnaryOperator<E> operator)
将该列表的每个元素替换为将该运算符应用于该元素的结果。
|
boolean |
retainAll(Collection<?> c)
仅保留此向量中包含在指定集合中的元素。
|
E |
set(int index, E element)
用指定的元素替换此Vector中指定位置的元素。
|
void |
setElementAt(E obj, int index)
设置在指定的组件
index此向量的要指定的对象。
|
void |
setSize(int newSize)
设置此向量的大小。
|
int |
size()
返回此向量中的组件数。
|
void |
sort(Comparator<? super E> c)
使用提供的
Comparator对此列表进行排序以比较元素。
|
Spliterator<E> |
spliterator()
在此列表中的元素上创建late-binding和故障切换 Spliterator 。
|
List<E> |
subList(int fromIndex, int toIndex)
返回此列表之间的fromIndex(包括)和toIndex之间的独占视图。
|
Object[] |
toArray()
以正确的顺序返回一个包含此Vector中所有元素的数组。
|
<T> T[] |
toArray(T[] a)
以正确的顺序返回一个包含此Vector中所有元素的数组;
返回的数组的运行时类型是指定数组的运行时类型。
|
String |
toString()
返回此Vector的字符串表示形式,其中包含每个元素的String表示形式。
|
void |
trimToSize()
修改该向量的容量成为向量的当前大小。
|
finalize, getClass, notify, notifyAll, wait, wait, waitparallelStream, streamprotected Object[] elementData
Vector中最后一个元素后面的任何数组元素都为空。
protected int elementCount
Vector对象中有效组件的数量。
组件elementData[0]至elementData[elementCount-1]是实际项目。
protected int capacityIncrement
public Vector(int initialCapacity,
int capacityIncrement)
initialCapacity - 矢量的初始容量
capacityIncrement - 当矢量溢出时容量增加的量
IllegalArgumentException - 如果指定的初始容量为负
public Vector(int initialCapacity)
initialCapacity - 矢量的初始容量
IllegalArgumentException - 如果指定的初始容量为负
public Vector()
10 ,其标准容量增量为零。
public Vector(Collection<? extends E> c)
c - 要将元素放入此向量的集合
NullPointerException - 如果指定的集合为空
public void copyInto(Object[] anArray)
k处的项目被复制到anArray的k anArray 。
anArray - 组件被复制到的阵列
NullPointerException - 如果给定的数组为空
IndexOutOfBoundsException - 如果指定的数组不够大以容纳该向量的所有组件
ArrayStoreException - 如果此向量的组件不是可以存储在指定数组中的运行时类型
toArray(Object[])
public void trimToSize()
elementData中的较小的内部数据阵列替换其内部数据阵列,将容量更改为等于大小。
应用程序可以使用此操作来最小化向量的存储。
public void ensureCapacity(int minCapacity)
如果该向量的当前容量小于minCapacity ,则通过替换其内部数据阵列来增加其容量,并保留在字段elementData中。 新的数据阵列的大小将是原来的大小加上capacityIncrement ,除非的值capacityIncrement小于或等于零,在这种情况下,新的容量将是原来容量的两倍; 但如果这个新尺寸还是小于minCapacity ,那么新的容量将是minCapacity 。
minCapacity - 所需的最小容量
public void setSize(int newSize)
null项目将添加到向量的末尾。
如果新尺寸小于当前尺寸,则丢弃索引newSize及更高版本的所有组件。
newSize - 这个矢量的新的大小
ArrayIndexOutOfBoundsException - 如果新的大小是负的
public int capacity()
elementData )
public int size()
size在界面
Collection<E>
size在接口
List<E>
size在
AbstractCollection<E>
public boolean isEmpty()
isEmpty在界面
Collection<E>
isEmpty在界面
List<E>
isEmpty在
AbstractCollection<E>
true当且仅当该向量没有组件时,即其大小为零;
false否则。
public Enumeration<E> elements()
Enumeration对象将生成此向量中的所有项。
产生的第一项是在索引的项0 ,则在索引项1 ,依此类推。
Iterator
public boolean contains(Object o)
true 。
更正式地,返回true当且仅当该向量包含至少一个元素e使得(o==null ? e==null : o.equals(e)) 。
contains在界面
Collection<E>
contains在界面
List<E>
contains在
AbstractCollection<E>
o - 要在此矢量中存在的元素将被测试
true如果此向量包含指定的元素
public int indexOf(Object o)
i ,使(o==null ? get(i)==null : o.equals(get(i))) ,或-1如果没有这样的指数。
public int indexOf(Object o, int index)
index向前index ,如果未找到该元素,则返回-1。
更正式地,返回最低指数i ,使(i >= index && (o==null ? get(i)==null : o.equals(get(i)))) ,或-1如果没有这样的索引。
o - 要搜索的元素
index - 开始搜索的索引
index或更高的位置的index ;
-1如果没有找到该元素。
IndexOutOfBoundsException - 如果指定的索引为负数
Object.equals(Object)
public int lastIndexOf(Object o)
i ,使(o==null ? get(i)==null : o.equals(get(i))) ,或-1如果没有这样的索引。
lastIndexOf在界面
List<E>
lastIndexOf在类别
AbstractList<E>
o - 要搜索的元素
public int lastIndexOf(Object o, int index)
index ,如果未找到该元素,则返回-1。
更正式地,返回最高指数i ,使(i <= index && (o==null ? get(i)==null : o.equals(get(i)))) ,或-1如果没有这样的索引。
o - 要搜索的元素
index - 开始向后搜索的索引
index的元素的最后一次出现的索引;
-1如果没有找到元素。
IndexOutOfBoundsException - 如果指定的索引大于或等于此向量的当前大小
public E elementAt(int index)
index - 这个向量的索引
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index >= size() )
public E firstElement()
0的项目)。
NoSuchElementException - 如果此向量没有组件
public E lastElement()
size() - 1 。
NoSuchElementException - 如果此向量为空
public void setElementAt(E obj, int index)
index的组件设置为指定的对象。
该位置的上一个组件被丢弃。
索引必须大于或等于0 ,小于当前的向量大小。
该方法的功能与set(int, E)方法相同(它是List接口的一部分)。 请注意, set方法会反转参数的顺序,以更紧密地匹配阵列使用。 另请注意, set方法返回存储在指定位置的旧值。
obj - 要设置的组件
index - 指定的索引
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index >= size() )
public void removeElementAt(int index)
index向下移动到具有比其先前所具有的值越小的指标之一。
此向量的大小减少1 。
索引必须大于或等于0 ,小于当前的向量大小。
该方法的功能与remove(int)方法相同(它是List接口的一部分)。 请注意, remove方法返回存储在指定位置的旧值。
index - 要删除的对象的索引
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index >= size() )
public void insertElementAt(E obj, int index)
index 。
该向量中具有大于或等于指定值index的索引中的每个index向上移动以使索引一大于先前的值。
索引必须大于或等于0 ,小于或等于向量的当前大小。 (如果索引等于向量的当前大小,则新元素将附加到向量。)
该方法的功能与add(int, E)方法相同(它是List接口的一部分)。 请注意, add方法可以反转参数的顺序,以更紧密地匹配阵列使用。
obj - 要插入的组件
index - 在哪里插入新组件
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index > size() )
public void addElement(E obj)
obj - 要添加的组件
public boolean removeElement(Object obj)
该方法的功能与remove(Object)方法相同(它是List接口的一部分)。
obj - 要删除的组件
true如果参数是此向量的一个组件;
false否则。
public void removeAllElements()
public Object clone()
Vector对象的原始内部数据数组的引用。
public Object[] toArray()
toArray在界面
Collection<E>
toArray在界面
List<E>
toArray在
AbstractCollection<E>
Arrays.asList(Object[])
public <T> T[] toArray(T[] a)
如果向量适合指定的数组,其余空间(即,该数组具有比Vector更多的元素),则紧接在Vector结束之后的数组中的元素将设置为null。 ( 仅当调用者知道Vector不包含任何空元素时,这才有助于确定Vector的长度。)
toArray在接口
Collection<E>
toArray在界面
List<E>
toArray在类别
AbstractCollection<E>
T - 包含集合的数组的运行时类型
a - 要存储Vector的元素的数组,如果它足够大;
否则,为此目的分配相同运行时类型的新数组。
ArrayStoreException - 如果a的运行时类型不是此向量中每个元素的运行时类型的超类型
NullPointerException - 如果给定的数组为空
public E get(int index)
get在界面
List<E>
get在类别
AbstractList<E>
index - 要返回的元素的索引
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index >= size() )
public E set(int index, E element)
set在界面
List<E>
set在
AbstractList<E>
index - 要替换的元素的索引
element - 要存储在指定位置的元素
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index >= size() )
public boolean add(E e)
add在接口
Collection<E>
add在界面
List<E>
add在类别
AbstractList<E>
e - 要附加到此向量的元素
true (由
Collection.add(E)指定 )
public boolean remove(Object o)
(o==null ? get(i)==null : o.equals(get(i))) (如果这样的元素存在)。
remove在接口
Collection<E>
remove在界面
List<E>
remove在
AbstractCollection<E>
o - 要从此Vector中移除的元素(如果存在)
public void add(int index,
E element)
add在界面
List<E>
add在
AbstractList<E>
index - 要在其中插入指定元素的索引
element - 要插入的元素
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index > size() )
public E remove(int index)
remove在界面
List<E>
remove在
AbstractList<E>
index - 要删除的元素的索引
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index >= size() )
public void clear()
clear在界面
Collection<E>
clear在界面
List<E>
clear在类别
AbstractList<E>
public boolean containsAll(Collection<?> c)
containsAll在界面
Collection<E>
containsAll在界面
List<E>
containsAll在类别
AbstractCollection<E>
c - 一个集合,其元素将在此向量中进行遏制测试
NullPointerException - 如果指定的集合为空
AbstractCollection.contains(Object)
public boolean addAll(Collection<? extends E> c)
addAll在界面
Collection<E>
addAll在界面
List<E>
addAll在类别
AbstractCollection<E>
c - 要插入此向量的元素
true如果此向量由于调用而更改
NullPointerException - 如果指定的集合为空
AbstractCollection.add(Object)
public boolean removeAll(Collection<?> c)
removeAll在接口
Collection<E>
removeAll在接口
List<E>
removeAll在
AbstractCollection<E>
c - 要从Vector中删除的元素的集合
ClassCastException - 如果此向量中的一个或多个元素的类型与指定的集合不兼容(
optional )
NullPointerException - 如果此向量包含一个或多个空元素,并且指定的集合不支持空元素(
optional ),或者如果指定的集合为空
AbstractCollection.remove(Object) ,
AbstractCollection.contains(Object)
public boolean retainAll(Collection<?> c)
retainAll在界面
Collection<E>
retainAll在界面
List<E>
retainAll在类别
AbstractCollection<E>
c - 要保留在此向量中的所有元素的集合(所有其他元素都被删除)
ClassCastException - 如果此向量中的一个或多个元素的类型与指定的集合不兼容(
optional )
NullPointerException - 如果此向量包含一个或多个空元素,并且指定的集合不支持空元素(
optional ),或者如果指定的集合为空
AbstractCollection.remove(Object) ,
AbstractCollection.contains(Object)
public boolean addAll(int index,
Collection<? extends E> c)
addAll在界面
List<E>
addAll在
AbstractList<E>类
index - 从中指定集合插入第一个元素的索引
c - 要插入此向量的元素
true如果此向量由于调用而更改
ArrayIndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index > size() )
NullPointerException - 如果指定的集合为空
public boolean equals(Object o)
(e1==null ? e2==null : e1.equals(e2))两个元素e1和e2 相等 )。换句话说,如果两个列表以相同的顺序包含相同的元素,则它们被定义为相等。
equals在界面
Collection<E>
equals在界面
List<E>
equals在
AbstractList<E>
o - 要与此向量相等的对象进行比较
Object.hashCode() , HashMap
public int hashCode()
hashCode在界面
Collection<E>
hashCode在界面
List<E>
hashCode在类别
AbstractList<E>
Object.equals(java.lang.Object) ,
System.identityHashCode(java.lang.Object)
public String toString()
toString在
AbstractCollection<E>
public List<E> subList(int fromIndex, int toIndex)
该方法消除了对显式范围操作(对于数组通常存在的排序)的需要。 任何期望列表的操作都可以通过在子列表视图而不是整个列表中进行操作来用作范围操作。 例如,以下成语从列表中删除了一系列元素:
list.subList(from, to).clear();
可以为indexOf和lastIndexOf构造类似的成语,并且Collections类中的所有算法都可以应用于子列表。
如果支持列表(即,此列表)以除了通过返回的列表之外的任何方式进行结构修改 ,则此方法返回的列表的语义将变得未定义。 (结构修改是那些改变List的大小,或以其他方式扰乱它,使得正在进行的迭代可能产生不正确的结果)。
subList在界面
List<E>
subList在
AbstractList<E>
fromIndex -
fromIndex低端点(含)
toIndex -
toIndex高端点(排他性)
IndexOutOfBoundsException - 如果端点索引值超出范围
(fromIndex < 0 || toIndex > size)
IllegalArgumentException - 如果端点索引
(fromIndex > toIndex)
protected void removeRange(int fromIndex,
int toIndex)
fromIndex (含)和toIndex之间的所有元素。
将任何后续元素移动到左侧(减少其索引)。
此电话会通过(toIndex - fromIndex)元素(toIndex - fromIndex)列表。
(如果toIndex==fromIndex ,此操作无效)
removeRange在
AbstractList<E>
fromIndex - 要删除的第一个元素的索引
toIndex - 要删除的最后一个元素后的索引
public ListIterator<E> listIterator(int index)
next将返回的第一个元素。
对previous的初始调用将返回指定索引减1的元素。
返回的列表迭代器是fail-fast 。
listIterator在接口
List<E>
listIterator在
AbstractList<E>
index - 要从列表迭代器返回的第一个元素的索引(通过调用
next )
IndexOutOfBoundsException - 如果索引超出范围(
index < 0 || index > size() )
public ListIterator<E> listIterator()
返回的列表迭代器是fail-fast 。
listIterator在界面
List<E>
listIterator在
AbstractList<E>
listIterator(int)
public void forEach(Consumer<? super E> action)
Iterable复制
Iterable的每个元素执行给定的操作,直到所有元素都被处理或动作引发异常。
除非实现类另有规定,否则按照迭代的顺序执行操作(如果指定了迭代顺序)。
动作抛出的异常被转发给呼叫者。
public boolean removeIf(Predicate<? super E> filter)
Collection复制
removeIf在界面
Collection<E>
filter - 一个谓词,为要删除的元素返回
true
true如果有任何元素被删除
public void replaceAll(UnaryOperator<E> operator)
List复制
replaceAll在界面
List<E>
operator - 运算符应用于每个元素
public void sort(Comparator<? super E> c)
List复制
Comparator对此列表进行排序,以比较元素。
sort在接口
List<E>
c - 用于比较列表元素的Comparator 。
A null值表示应使用元素' natural ordering '
public Spliterator<E> spliterator()
Spliterator 。
该Spliterator报告Spliterator.SIZED , Spliterator.SUBSIZED和Spliterator.ORDERED 。 覆盖实现应记录其他特征值的报告。
spliterator在界面
Iterable<E>
spliterator在界面
Collection<E>
spliterator在界面
List<E>
Spliterator在这个列表中的元素
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.