标签:let addition relay wing increase iter hash possible ber
1 public class ArrayList<E>
2 extends AbstractList<E>
3 implements List<E>, RandomAccess, Cloneable, Serializable
列表接口的可调整大小的数组实现。
实现所有可选的列表操作,并允许所有元素,包括null。
除了实现List接口之外,该类还提供了一些方法来操作数组的大小,该数组在内部用于存储List。
(这个类大致相当于Vector,只是它是不同步的。)
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.
size、isEmpty、get、set、iterator和listIterator操作在固定的常数时间内运行。
添加操作在平摊常数时间内运行,也就是说,添加n个元素需要O(n)个时间。
所有其他操作都在线性时间内运行(粗略地说)。
与LinkedList实现相比,常量因子较低。
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
每个 ArrayList 实例都有一个容量。该容量是指用来存储列表元素的数组的大小。它总是至少等于列表的大小。随着向 ArrayList 中不断添加元素,其容量也自动增长。并未指定增长策略的细节,因为这不只是添加元素会带来分摊固定时间开销那样简单。
An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
在添加大量元素前,应用程序可以使用 ensureCapacity 操作来增加 ArrayList 实例的容量。这可以减少递增式再分配的数量。
Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList
method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
注意,此实现不是同步的。如果多个线程同时访问一个 ArrayList 实例,而其中至少一个线程从结构上修改了列表,那么它必须 保持外部同步。(结构上的修改是指任何添加或删除一个或多个元素的操作,或者显式调整底层数组的大小;仅仅设置元素的值不是结构上的修改。)这一般通过对自然封装该列表的对象进行同步操作来完成。如果不存在这样的对象,则应该使用 Collections.synchronizedList
方法将该列表“包装”起来。这最好在创建时完成,以防止意外对列表进行不同步的访问:
List list = Collections.synchronizedList(new ArrayList(...));
The iterators returned by this class‘s iterator
and listIterator
methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator‘s own remove
or add
methods, the iterator will throw a ConcurrentModificationException
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
此类的 iterator 和 listIterator 方法返回的迭代器是快速失败的:在创建迭代器之后,除非通过迭代器自身的 remove 或 add 方法从结构上对列表进行修改,否则在任何时间以任何方式对列表进行修改,迭代器都会抛出 ConcurrentModificationException
。因此,面对并发的修改,迭代器很快就会完全失败,而不是冒着在将来某个不确定时间发生任意不确定行为的风险。
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException
on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
注意,迭代器的快速失败行为无法得到保证,因为一般来说,不可能对是否出现不同步并发修改做出任何硬性保证。快速失败迭代器会尽最大努力抛出 ConcurrentModificationException。因此,为提高这类迭代器的正确性而编写一个依赖于此异常的程序是错误的做法:迭代器的快速失败行为应该仅用于检测 bug。
This class is a member of the Java Collections Framework.
此类是 Java Collections Framework 的成员。
Collection
, List
, LinkedList
, Vector
, Serialized Form
Constructor and Description |
---|
ArrayList()
Constructs an empty list with an initial capacity of ten.
|
ArrayList(Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection‘s iterator.
|
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
|
Modifier and Type 修饰符和类型 | Method and Description 方法和描述 |
---|---|
boolean |
add(E e)
Appends the specified element to the end of this list.
将指定的元素附加到此列表的末尾。 |
void |
add(int index, E element)
Inserts the specified element at the specified position in this list.
将指定元素插入到列表中的指定位置。 |
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection‘s Iterator.
将指定集合中的所有元素按照指定集合的迭代器返回它们的顺序追加到此列表的末尾。 |
boolean |
addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
从指定位置开始,将指定集合中的所有元素插入此列表。 |
void |
clear()
Removes all of the elements from this list.
从列表中删除所有元素。
|
Object |
clone()
Returns a shallow copy of this ArrayList instance.
返回此ArrayList实例的浅拷贝。 |
boolean |
contains(Object o)
Returns true if this list contains the specified element.
如果此列表包含指定的元素,则返回true。
|
void |
ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
如果需要,增加这个ArrayList实例的容量,以确保它至少可以容纳由minimum capacity参数指定的元素数量。 |
void |
forEach(Consumer<? super E> action)
Performs the given action for each element of the
Iterable until all elements have been processed or the action throws an exception.为可迭代的每个元素执行给定的操作,直到处理完所有元素或操作引发异常。 |
E |
get(int index)
Returns the element at the specified position in this list.
返回此列表中指定位置的元素。 |
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
返回该列表中指定元素第一次出现的索引,如果该列表不包含该元素,则返回-1。 |
boolean |
isEmpty()
Returns true if this list contains no elements.
如果此列表不包含任何元素,则返回true。 |
Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence.
按适当的顺序对列表中的元素返回一个迭代器。 |
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
返回此列表中指定元素的最后一次出现的索引,如果此列表不包含该元素,则返回-1。 |
ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
返回该列表中元素的列表迭代器(按适当的顺序)。 |
ListIterator<E> |
listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
返回此列表中元素的列表迭代器(按适当的顺序),从列表中的指定位置开始。 |
E |
remove(int index)
Removes the element at the specified position in this list.
删除列表中指定位置的元素。 |
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
从该列表中删除指定元素的第一个匹配项(如果存在)。
|
boolean |
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.
从该列表中删除指定集合中包含的所有元素。 |
boolean |
removeIf(Predicate<? super E> filter)
Removes all of the elements of this collection that satisfy the given predicate.
删除此集合中满足给定谓词的所有元素。 |
protected void |
removeRange(int fromIndex, int toIndex)
Removes from this list all of the elements whose index is between
fromIndex , inclusive, and toIndex , exclusive.从该列表中删除其索引在包含的fromIndex和排除的toIndex之间的所有元素。 |
void |
replaceAll(UnaryOperator<E> operator)
Replaces each element of this list with the result of applying the operator to that element.
将此列表中的每个元素替换为将运算符应用于该元素的结果。 |
boolean |
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection.
仅保留此列表中包含在指定集合中的元素。 |
E |
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
将列表中指定位置的元素替换为指定元素。 |
int |
size()
Returns the number of elements in this list.
返回列表中元素的数目。 |
void |
sort(Comparator<? super E> c)
Sorts this list using the supplied
Comparator to compare elements.使用提供的比较器对列表进行排序,以比较元素。 |
Spliterator<E> |
spliterator()
Creates a late-binding and fail-fast
Spliterator over the elements in this list.在此列表中的元素上创建延迟绑定和故障快速Spliterator。 |
List<E> |
subList(int fromIndex, int toIndex)
Returns a view of the portion of this list between the specified
fromIndex , inclusive, and toIndex , exclusive.返回指定的fromIndex(包含)和toIndex(排他)之间的列表部分的视图。 |
Object[] |
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
返回一个数组,该数组按适当的顺序(从第一个元素到最后一个元素)包含列表中的所有元素。 |
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element);
the runtime type of the returned array is that of the specified array.
返回一个数组,该数组按适当的顺序包含列表中的所有元素(从第一个元素到最后一个元素); |
void |
trimToSize()
Trims the capacity of this ArrayList instance to be the list‘s current size.
将此ArrayList实例的容量调整为列表的当前大小。
|
equals, hashCode
public ArrayList(int initialCapacity)
initialCapacity
- the initial capacity of the list列表的初始容量IllegalArgumentException
- if the specified initial capacity is negative 如果指定的初始容量为负public ArrayList()
public ArrayList(Collection<? extends E> c)
c
- the collection whose elements are to be placed into this list要将其元素放入此列表的集合NullPointerException
- if the specified collection is null 如果指定的集合为空public void trimToSize()
将此ArrayList实例的容量调整为列表的当前大小。
应用程序可以使用此操作最小化ArrayList实例的存储。
public void ensureCapacity(int minCapacity)
minCapacity
- the desired minimum capacity 所需的最小容量public int size()
size
in interface Collection<E>
Collection<E>
接口集合的大小<E>
size
in interface List<E
>
List<E>
接口列表中的大小<E>size
in class AbstractCollection<E>
类AbstractCollection<E>的大小
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in interface List<E>
isEmpty
in class AbstractCollection<E>
public boolean contains(Object o)
如果此列表包含指定的元素,则返回true。
更正式地说,当且仅当此列表包含至少一个元素e (o==null ? e = = null: o.equals (e))。
contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
o
- element whose presence in this list is to be tested要测试其在此列表中的存在的元素public int indexOf(Object o)
public int indexOf(Object o)
public Object clone()
返回此ArrayList实例的浅拷贝。(元素本身不会被复制。)
clone
in class Object
类对象中的克隆
这个ArrayList实例的克隆
Cloneable
public Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
返回的数组将是“安全的”,因为这个列表不维护对它的引用。(换句话说,这个方法必须分配一个新数组)。因此,调用者可以自由地修改返回的数组。
This method acts as bridge between array-based and collection-based APIs.
此方法充当基于数组和基于集合的api之间的桥梁。
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
Arrays.asList(Object[])
public <T> T[] toArray(T[] a)
返回一个数组,该数组按适当的顺序包含列表中的所有元素(从第一个元素到最后一个元素);
返回数组的运行时类型是指定数组的运行时类型。
如果列表符合指定的数组,则返回该列表。
否则,将使用指定数组的运行时类型和此列表的大小分配新数组。
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
如果列表符合指定的数组,则有剩余空间(即,数组的元素比列表的元素多),数组中紧跟在集合末尾的元素被设置为null。
(只有在调用方知道列表不包含任何空元素时,这才有助于确定列表的长度。)
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
T
- the runtime type of the array to contain the collectiona
- the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.ArrayStoreException
- if the runtime type of the specified array is not a supertype of the runtime type of every element in this listNullPointerException
- if the specified array is null public E get(int index)
get
in interface List<E>
get
in class AbstractList<E>
index
- index of the element to return要返回的元素的索引
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()) public E set(int index,E element)
set
in interface List<E>
set
in class AbstractList<E>
index
- index of the element to replace
要替换的元素的索引
element
- element to be stored at the specified positionIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()) public boolean add(E e)
add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
e
- element to be appended to this listCollection.add(E)
) public void add(int index,E element)
将指定元素插入到列表中的指定位置。
将当前位于该位置的元素(如果有)和任何后续元素向右移动(将一个元素添加到它们的索引中)。
add
in interface List<E>
add
in class AbstractList<E>
index
- index at which the specified element is to be insertedelement
- element to be insertedIndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()) public E remove(int index)
remove
in interface List<E>
remove
in class AbstractList<E>
index
- the index of the element to be removedIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()) public boolean remove(Object o)
remove
in interface Collection<E>
remove
in interface List<E>
remove
in class AbstractCollection<E>
o
- element to be removed from this list, if presentpublic void clear()
clear
in interface Collection<E>
clear
in interface List<E>
clear
in class AbstractList<E>
public boolean addAll(Collection<? extends E> c)
addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class AbstractCollection<E>
c
- collection containing elements to be added to this listNullPointerException
- if the specified collection is nullAbstractCollection.add(Object)
public boolean addAll(int index,Collection<? extends E> c)
从指定位置开始,将指定集合中的所有元素插入此列表。
将当前位于该位置的元素(如果有)和任何后续元素向右移动(增加它们的索引)。
新元素将按照指定集合的迭代器返回它们的顺序出现在列表中。
addAll
in interface List<E>
addAll
in class AbstractList<E>
index
- index at which to insert the first element from the specified collectionc
- collection containing elements to be added to this listIndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size())NullPointerException
- if the specified collection is null protected void removeRange(int fromIndex,int toIndex)
fromIndex
, inclusive, and toIndex
, exclusive. (toIndex - fromIndex)
elements.toIndex==fromIndex
, this operation has no effect.)从该列表中删除其索引在包含的fromIndex和排除的toIndex之间的所有元素。
将任何后续元素向左移动(减少它们的索引)。
这个调用通过(toIndex - fromIndex)元素缩短了列表。
(如果toIndex==fromIndex,则此操作无效。)
removeRange
in class AbstractList<E>
类AbstractList<E>中的removeRange
fromIndex
- index of first element to be removedtoIndex
- index after last element to be removedIndexOutOfBoundsException
- if fromIndex
or toIndex
is out of range (fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex
) public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<E>
removeAll
in interface List<E>
removeAll
in class AbstractCollection<E>
类AbstractCollection<E>中的removeAll
c
- collection containing elements to be removed from this listtrue
if this list changed as a result of the callClassCastException
- if the class of an element of this list is incompatible with the specified collection (optional)NullPointerException
- if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is nullCollection.contains(Object)
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<E>
retainAll
in interface List<E>
retainAll
in class AbstractCollection<E>
c
- collection containing elements to be retained in this listtrue
if this list changed as a result of the callClassCastException
- if the class of an element of this list is incompatible with the specified collection (optional)NullPointerException
- if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is nullCollection.contains(Object)
public ListIterator<E> listIterator(int index)
next
. previous
would return the element with the specified index minus one.The returned list iterator is fail-fast.
返回的列表迭代器是故障快速的。
listIterator
in interface List<E>
listIterator
in class AbstractList<E>
index
- index of the first element to be returned from the list iterator (by a call to next
)IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()
) public ListIterator<E> listIterator()
The returned list iterator is fail-fast.
返回的列表迭代器是故障快速的。
listIterator
in interface List<E>
listIterator
in class AbstractList<E>
listIterator(int)
public List<E> subList(int fromIndex,int toIndex)
fromIndex
, inclusive, and toIndex
, exclusive. (If fromIndex
and toIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
这种方法不需要显式的范围操作(数组中通常存在的排序)。通过传递子列表视图而不是整个列表,任何期望列表的操作都可以用作范围操作。例如,下面的习惯用法从列表中删除一系列元素:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf(Object)
and lastIndexOf(Object)
, and all of the algorithms in the Collections
class can be applied to a subList.The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
该方法返回的列表的语义将变得未定义,如果支持列表(即除通过返回的列表外,以任何方式对其进行结构修改。
(结构修改是指改变列表的大小,或者以一种正在进行的迭代可能产生错误结果的方式扰乱列表。)
subList
in interface List<E>
subList
in class AbstractList<E>
fromIndex
- low endpoint (inclusive) of the subListtoIndex
- high endpoint (exclusive) of the subListIndexOutOfBoundsException
- if an endpoint index value is out of range (fromIndex < 0 || toIndex > size)
如果端点索引值超出范围(fromIndex < 0 || toIndex > size)
IllegalArgumentException
- if the endpoint indices are out of order (fromIndex > toIndex)
如果端点索引无序(从mindex >到index)
public void forEach(Consumer<? super E> action)
Iterable
从接口复制的描述:Iterable
Iterable
until all elements have been processed or the action throws an exception. 为可迭代的每个元素执行给定的操作,直到处理完所有元素或操作引发异常。
除非实现类另行指定,否则操作将按照迭代的顺序执行(如果指定了迭代的顺序)。
操作引发的异常将被转发给调用者。
public Spliterator<E> spliterator()
Spliterator
over the elements in this list.The Spliterator
reports Spliterator.SIZED
, Spliterator.SUBSIZED
, and Spliterator.ORDERED
. Overriding implementations should document the reporting of additional characteristic values.
Spliterator报告Spliterator。大小,Spliterator。小尺寸,Spliterator.ORDERED。覆盖实现应该记录额外特征值的报告。
spliterator
in interface Iterable<E>
spliterator
in interface Collection<E>
spliterator
in interface List<E>
Spliterator
over the elements in this listpublic boolean removeIf(Predicate<? super E> filter)
Collection
从接口复制的描述:集合
删除此集合中满足给定谓词的所有元素。
迭代期间或由谓词抛出的错误或运行时异常将传递给调用者。
removeIf
in interface Collection<E>
filter
- a predicate which returns true
for elements to be removedtrue
if any elements were removed public void replaceAll(UnaryOperator<E> operator)
List
从接口复制的描述:列表
将此列表中的每个元素替换为将运算符应用于该元素的结果。
操作符抛出的错误或运行时异常将传递给调用者。
replaceAll
in interface List<E>
operator
- the operator to apply to each element public void sort(Comparator<? super E> c)
List
Comparator
to compare elements.从接口复制的描述:列表
使用提供的比较器对列表进行排序,以比较元素。
sort
in interface List<E>
c
- the Comparator
used to compare list elements.null
value indicates that the elements‘ natural ordering should be used.标签:let addition relay wing increase iter hash possible ber
原文地址:https://www.cnblogs.com/LinQingYang/p/12562686.html