public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
的size,isEmpty,get,set,iterator,和listIterator操作运行在固定的时间。的add运行摊还常量时间,即添加N元素需要O(n)时间。所有其他的操作运行在线性时间(大致说)。相比于LinkedList实现的常数因子是低。
每个ArrayList实例有一个容量。容量是用于存储列表中元素的数组的大小。它总是至少和列表大小一样大。为元素添加到ArrayList,它的容量是自动增加。的增长策略的细节是没有指定在事实上添加一个元素具有恒定的摊销时间成本。
应用程序可以在加入大量使用ensureCapacity操作元素增加一个ArrayList实例的能力。这可以减少增量分配量。
请注意,此实现不同步。如果多个线程访问一个ArrayList实例的同时,并至少有一个线程修改表结构,它必须同步外部。(结构上的修改是任何操作,添加或删除一个或多个元素,或明确的调整支持阵列;只设置一个元素的值是不是一个结构上的修改。)这通常是由一些对象同步自然封装的清单来完成。如果该对象不存在,该清单应“包装”使用Collections.synchronizedList方法。最好的做法是在创建时,防止意外的非同步访问列表:
列表(ArrayList集合。synchronizedlist新(…));
The iterators returned by this class's iterator和listIterator方法快速失败:如果列表迭代器结构改性后创建的任何时间,以任何方式除了通过迭代器的remove或add方法,迭代器将抛出一个ConcurrentModificationException。因此,在并发修改的面前,迭代器失败迅速和干净,而不是冒着任意的,非确定性的行为在未来的一个不确定的时间。
注意迭代器不能快速失败行为得到保证的话,一般来说,不可能在不同步的并发修改的存在作出难以保证。快速失败迭代器扔ConcurrentModificationException尽最大努力的基础上。因此,要写一个程序,依靠这一例外的正确性错误:快速失败迭代器的行为只能用来检测错误。
这个班的一员 Java Collections Framework。
Collection,
List,
LinkedList,
Vector,
Serialized Form
modCount| Constructor and Description |
|---|
ArrayList()
构造一个初始容量为十的空列表。
|
ArrayList(Collection<? extends E> c)
构造一个包含指定集合的元素的列表,它们在集合的迭代器返回的顺序中返回。
|
ArrayList(int initialCapacity)
用指定的初始容量构造一个空列表。
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e)
将指定的元素列表的结束。
|
void |
add(int index, E element)
在列表中指定的位置上插入指定的元素。
|
boolean |
addAll(Collection<? extends E> c)
追加指定集合的所有元素到这个列表的末尾,按他们的指定集合的迭代器返回。
|
boolean |
addAll(int index, Collection<? extends E> c)
将指定集合中的所有元素插入到该列表中,从指定位置开始。
|
void |
clear()
从这个列表中移除所有的元素。
|
Object |
clone()
返回该
ArrayList实例浅拷贝。
|
boolean |
contains(Object o)
返回
true如果这个列表包含指定元素。
|
void |
ensureCapacity(int minCapacity)
增加这
ArrayList实例的能力如果有必要,以确保它至少能容纳的最小容量参数指定元素个数。
|
void |
forEach(Consumer<? super E> action)
执行特定动作的每一个元素的
Iterable直到所有元素都被处理或操作抛出异常。
|
E |
get(int index)
返回此列表中指定位置的元素。
|
int |
indexOf(Object o)
返回此列表中指定元素的第一个出现的索引,或-如果此列表不包含元素,或- 1。
|
boolean |
isEmpty()
返回
true如果此列表不包含元素。
|
Iterator<E> |
iterator()
在这个列表中的元素上返回一个正确的顺序。
|
int |
lastIndexOf(Object o)
返回此列表中指定元素的最后一个发生的索引,或-如果此列表不包含元素,或- 1。
|
ListIterator<E> |
listIterator()
返回列表元素的列表迭代器(在适当的顺序)。
|
ListIterator<E> |
listIterator(int index)
在列表中的元素上返回列表迭代器(在适当的顺序),从列表中的指定位置开始。
|
E |
remove(int index)
移除此列表中指定位置的元素。
|
boolean |
remove(Object o)
从该列表中移除指定元素的第一个发生,如果它是存在的。
|
boolean |
removeAll(Collection<?> c)
从这个列表中移除包含在指定集合中的所有元素。
|
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)
用指定元素替换此列表中指定位置的元素。
|
int |
size()
返回此列表中元素的数目。
|
void |
sort(Comparator<? super E> c)
分类列表使用提供的
Comparator比较元素。
|
Spliterator<E> |
spliterator()
创建一个后期绑定和快速失败
Spliterator超过此列表中的元素。
|
List<E> |
subList(int fromIndex, int toIndex)
返回一个视图之间的指定
fromIndex,包容,和
toIndex这份名单的部分,独家。
|
Object[] |
toArray()
返回一个数组,包含在这个列表中的所有元素在适当的顺序(从第一个到最后一个元素)。
|
<T> T[] |
toArray(T[] a)
返回一个数组,包含在这个列表中的所有元素在适当的顺序(从第一到最后一个元素);返回数组的运行时类型是指定的数组的运行时类型。
|
void |
trimToSize()
装饰这
ArrayList实例是列表的当前容量。
|
equals, hashCodecontainsAll, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitcontainsAll, equals, hashCodeparallelStream, streampublic ArrayList(int initialCapacity)
initialCapacity -列表的初始容量
IllegalArgumentException -如果指定的初始容量为负
public ArrayList()
public ArrayList(Collection<? extends E> c)
c的元素都被放置到这个名单的收集
NullPointerException -如果指定集合为空
public void trimToSize()
public void ensureCapacity(int minCapacity)
minCapacity -所需的最小容量
public int size()
size 接口
Collection<E>
size 接口
List<E>
size 方法重写,继承类
AbstractCollection<E>
public boolean isEmpty()
isEmpty 接口
Collection<E>
isEmpty 接口
List<E>
isEmpty 方法重写,继承类
AbstractCollection<E>
public boolean contains(Object o)
contains 接口
Collection<E>
contains 接口
List<E>
contains 方法重写,继承类
AbstractCollection<E>
o列表中存在的元素进行测试
public int indexOf(Object o)
public int lastIndexOf(Object o)
lastIndexOf 接口
List<E>
lastIndexOf 方法重写,继承类
AbstractList<E>
o元搜索
public Object clone()
public Object[] toArray()
返回的数组将是“安全”的,在这个列表中没有引用它的引用。(换句话说,这种方法必须分配一个新的数组)。因此,调用方可以自由修改返回的数组。
此方法作为基于数组和基于集合的原料药之间的桥梁。
toArray 接口
Collection<E>
toArray 接口
List<E>
toArray 方法重写,继承类
AbstractCollection<E>
Arrays.asList(Object[])
public <T> T[] toArray(T[] a)
如果列表可以指定数组中剩余的空间(即数组比列表元素),在阵列立即收集结束后的元素设置为null。(这是有用的在确定的名单只有长度如果调用者知道列表不包含任何无效的元素。)
toArray 接口
Collection<E>
toArray 接口
List<E>
toArray 方法重写,继承类
AbstractCollection<E>
T -数组的运行时类型包含集合
a -到列表的元素被存储数组,如果它足够大;否则,一个新的运行时类型相同的数组分配给这个目的。
ArrayStoreException -如果指定数组的运行时类型不是超此列表中每个元素运行时类型
NullPointerException -如果指定的数组是空的
public E get(int index)
get 接口
List<E>
get 方法重写,继承类
AbstractList<E>
index索引元素的回归
IndexOutOfBoundsException如果索引超出范围(
index < 0 || index >= size())
public E set(int index, E element)
set 接口
List<E>
set 方法重写,继承类
AbstractList<E>
index索引的元素代替
element元素被存储在指定的位置
IndexOutOfBoundsException如果索引超出范围(
index < 0 || index >= size())
public boolean add(E e)
add 接口
Collection<E>
add 接口
List<E>
add 方法重写,继承类
AbstractList<E>
e元素附加到列表
Collection.add(E)指定)
public void add(int index,
E element)
add 接口
List<E>
add 方法重写,继承类
AbstractList<E>
index指数在指定的元素被插入
element元素被插入
IndexOutOfBoundsException如果索引超出范围(
index < 0 || index > size())
public E remove(int index)
remove 接口
List<E>
remove 方法重写,继承类
AbstractList<E>
index -要被删除的元素的索引
IndexOutOfBoundsException如果索引超出范围(
index < 0 || index >= size())
public boolean remove(Object o)
remove 接口
Collection<E>
remove 接口
List<E>
remove 方法重写,继承类
AbstractCollection<E>
o元要从列表中删除,如果存在
public void clear()
clear 接口
Collection<E>
clear 接口
List<E>
clear 方法重写,继承类
AbstractList<E>
public boolean addAll(Collection<? extends E> c)
addAll 接口
Collection<E>
addAll 接口
List<E>
addAll 方法重写,继承类
AbstractCollection<E>
c收集含有的元素被添加到列表
NullPointerException -如果指定集合为空
AbstractCollection.add(Object)
public boolean addAll(int index,
Collection<? extends E> c)
addAll 接口
List<E>
addAll 方法重写,继承类
AbstractList<E>
index指数,从指定集合的第一个元素的插入
c收集含有的元素被添加到列表
IndexOutOfBoundsException如果索引超出范围(
index < 0 || index > size())
NullPointerException -如果指定集合为空
protected void removeRange(int fromIndex,
int toIndex)
fromIndex之间,包容性,和
toIndex,独家。将任何成功的元素转移到左边(降低它们的索引)。这叫缩短列表
(toIndex - fromIndex)元素。(如果
toIndex==fromIndex,此操作没有影响。)
removeRange 方法重写,继承类
AbstractList<E>
fromIndex -第一个元素被删除索引
toIndex -要删除的最后一个元素后的指标
IndexOutOfBoundsException -如果
fromIndex或
toIndex超出范围(
fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex)
public boolean removeAll(Collection<?> c)
removeAll 接口
Collection<E>
removeAll 接口
List<E>
removeAll 方法重写,继承类
AbstractCollection<E>
c收集含有元素被从列表中删除
true如果调用的结果列表中改变
ClassCastException -如果该列表元素的类与集合不相容(
optional)
NullPointerException -如果此列表包含一个null元素而指定集合不允许null元素(
optional),或者指定集合为空
Collection.contains(Object)
public boolean retainAll(Collection<?> c)
retainAll 接口
Collection<E>
retainAll 接口
List<E>
retainAll 方法重写,继承类
AbstractCollection<E>
c收集含有元素被保留在这个列表
true如果调用的结果列表中改变
ClassCastException -如果该列表元素的类与集合不相容(
optional)
NullPointerException -如果此列表包含一个null元素而指定集合不允许null元素(
optional),或者指定集合为空
Collection.contains(Object)
public ListIterator<E> listIterator(int index)
next。对
previous初始调用将返回与指定的指数减一元。
返回的列表迭代器是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 List<E> subList(int fromIndex, int toIndex)
fromIndex,包容,和
toIndex这份名单的部分,独家。(如果
fromIndex和
toIndex相等,返回的列表是空的。)返回的列表是通过这个列表的支持,所以在返回列表中的非结构的变化反映在这个列表中,反之亦然。返回的列表支持所有可选列表操作。
此方法消除了对显式范围操作(通常用于数组的排序)的需要。任何希望将列表作为一系列操作通过一个子视图而不是整个列表。例如,下面的成语从列表中移除了一系列元素:
列表。列表(从,到clear());相似的习语可以构造
indexOf(Object)和
lastIndexOf(Object),和所有在
Collections类算法可以应用到列表。
列表的语义,该方法返回的是如果支持列表(即,这个列表)是通过返回的列表结构改性以外的其他任何方式。(结构上的修改是指改变这个名单,大小或其他方式,迭代的进展可能会产生不正确的结果。)
subList 接口
List<E>
subList 方法重写,继承类
AbstractList<E>
fromIndex低端点(含)的子列表
toIndex高端点(独家)的子列表
IndexOutOfBoundsException -如果一个端点索引值超出范围
(fromIndex < 0 || toIndex > size)
IllegalArgumentException如果终点指标是为了
(fromIndex > toIndex)
public void forEach(Consumer<? super E> action)
Iterable
Iterable直到所有元素都被处理或操作抛出异常。除非实现类的其他指定,否则操作在迭代的顺序中(如果指定了一个迭代顺序)。由操作引发的异常被传递给调用方。
public Spliterator<E> spliterator()
Spliterator超过此列表中的元素。
报告的Spliterator Spliterator.SIZED,Spliterator.SUBSIZED,和Spliterator.ORDERED。重写实现应记录附加特征值的报告。
spliterator 接口
Iterable<E>
spliterator 接口
Collection<E>
spliterator 接口
List<E>
Spliterator
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。一个
null值表示元素的
natural ordering应使用
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.