目录
All Implemented Interfaces所有已实现接口
- All Implemented Interfaces所有已实现接口:
- Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
- Direct Known Subclasses直接已知子类:
- AttributeList, RoleList, RoleUnresolvedList
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 的成员。
- Since从以下版本开始:
- 1.2
- See Also另请参见:
Collection
,List
,LinkedList
,Vector
, Serialized Form
Field Summary(字段汇总)
-
Fields inherited from class java.util.AbstractList(继承自类java.util.AbstractList的字段):modCount
Constructor Summary(构造函数总结)
Constructor and Description |
---|
ArrayList()
Constructs an empty list with an initial capacity of ten.
构造一个初始容量为10的空列表。 |
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.
构造具有指定初始容量的空列表。 |
Method Summary(方法总结)
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实例的容量调整为列表的当前大小。
|
Methods inherited from class java.util.AbstractList(方法继承自类java.util.AbstractList)
equals, hashCode
Methods inherited from class java.util.AbstractCollection(方法继承自java.util.AbstractCollection类)
Methods inherited from class java.lang.Object(方法继承自java.lang.Object类)
Methods inherited from interface java.util.List(方法继承自接口java.util.List)
Methods inherited from interface java.util.Collection(方法继承自接口java.util.Collection)
Constructor Detail(构造方法详细信息)
-
ArrayList
public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
构造具有指定初始容量的空列表。- Parameters:
initialCapacity
- the initial capacity of the list列表的初始容量- Throws:
IllegalArgumentException
- if the specified initial capacity is negative 如果指定的初始容量为负
-
ArrayList
public ArrayList()
Constructs an empty list with an initial capacity of ten.
构造一个初始容量为10的空列表。
-
ArrayList
public 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.
构造一个包含指定集合的元素的列表,按集合的迭代器返回元素的顺序排列。- Parameters:
c
- the collection whose elements are to be placed into this list要将其元素放入此列表的集合- Throws:
NullPointerException
- if the specified collection is null 如果指定的集合为空
Method Detail(方法详细信息)
-
trimToSize
public void trimToSize()
Trims the capacity of this ArrayList instance to be the list's current size.
An application can use this operation to minimize the storage of an ArrayList instance.
将此ArrayList实例的容量调整为列表的当前大小。
应用程序可以使用此操作最小化ArrayList实例的存储。
-
ensureCapacity
public 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参数指定的元素数量。- Parameters:
minCapacity
- the desired minimum capacity 所需的最小容量
-
size
public int size()
Returns the number of elements in this list.
返回列表中元素的数目。- Specified by:
size
in interfaceCollection<E>
Collection<E>
接口集合的大小<E>- Specified by:
size
in interfaceList<E
>
接口列表中的大小<E>
List<E>- Specified by:
size
in classAbstractCollection<E>
类AbstractCollection<E>的大小
- Returns:
- the number of elements in this list
这个列表中的元素数量
-
isEmpty
public boolean isEmpty()
Returns true if this list contains no elements.
如果此列表不包含任何元素,则返回true。- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceList<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
- true if this list contains no elements
如果此列表不包含任何元素,则为真
-
contains
public boolean contains(Object o)
Returns true if this list contains the specified element.More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
如果此列表包含指定的元素,则返回true。
更正式地说,当且仅当此列表包含至少一个元素e (o==null ? e = = null: o.equals (e))。- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceList<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
o
- element whose presence in this list is to be tested要测试其在此列表中的存在的元素- Returns:
- true if this list contains the specified element
如果此列表包含指定的元素,则为真
-
indexOf
public 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. -
More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
返回此列表中指定元素的第一个匹配项的索引,如果此列表不包含该元素,则返回-1。
更正式地说,返回最低索引i,使得(o==null ?get(i)==null: o.equals(get(i)),如果没有这样的索引,则为-1。
-
indexOf
public 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. -
More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
返回此列表中指定元素的第一个匹配项的索引,如果此列表不包含该元素,则返回-1。
更正式地说,返回最低索引i,使得(o==null ?get(i)==null: o.equals(get(i)),如果没有这样的索引,则为-1。
-
clone
public Object clone()
Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
返回此ArrayList实例的浅拷贝。(元素本身不会被复制。)
- Overrides:
clone
in classObject
类对象中的克隆- Returns:
- a clone of this ArrayList instance
这个ArrayList实例的克隆
- See Also:
Cloneable
-
toArray
public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
返回一个数组,该数组按适当的顺序(从第一个元素到最后一个元素)包含列表中的所有元素。
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之间的桥梁。- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Returns:
- an array containing all of the elements in this list in proper sequence
一个数组,包含列表中所有元素的正确顺序 - See Also:
Arrays.asList(Object[])
-
toArray
public <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.
If the list fits in the specified array, it is returned therein.
Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
返回一个数组,该数组按适当的顺序包含列表中的所有元素(从第一个元素到最后一个元素);
返回数组的运行时类型是指定数组的运行时类型。
如果列表符合指定的数组,则返回该列表。
否则,将使用指定数组的运行时类型和此列表的大小分配新数组。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。
(只有在调用方知道列表不包含任何空元素时,这才有助于确定列表的长度。)- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Type Parameters:
T
- the runtime type of the array to contain the collection
包含集合的数组的运行时类型- Parameters:
a
- 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.
如果列表的元素足够大,则将其存储到其中的数组;否则,将为此分配相同运行时类型的新数组。- Returns:
- an array containing the elements of the list
包含列表元素的数组 - Throws:
ArrayStoreException
- if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
如果指定数组的运行时类型不是此列表中每个元素的运行时类型的超类型NullPointerException
- if the specified array is null
如果指定的数组为空
-
get
public E get(int index)
Returns the element at the specified position in this list.
返回此列表中指定位置的元素。- Specified by:
get
in interfaceList<E>
- Specified by:
get
in classAbstractList<E>
- Parameters:
index
- index of the element to return
要返回的元素的索引
- Returns:
- the element at the specified position in this list
位于列表中指定位置的元素 - Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size())
如果索引超出范围(索引< 0 ||索引>= size())
-
set
public E set(int index,E element)
Replaces the element at the specified position in this list with the specified element.
将列表中指定位置的元素替换为指定元素。- Specified by:
set
in interfaceList<E>
- Overrides:
set
in classAbstractList<E>
- Parameters:
index
- index of the element to replace要替换的元素的索引
element
- element to be stored at the specified position
要存储在指定位置的元素- Returns:
- the element previously at the specified position
先前位于指定位置的元素 - Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size())
如果索引超出范围(索引< 0 ||索引>= size())
-
add
public boolean add(E e)
Appends the specified element to the end of this list.
将指定的元素附加到此列表的末尾。- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Parameters:
e
- element to be appended to this list
元素添加到此列表中- Returns:
- true (as specified by
Collection.add(E)
)
true(由Collection.add(E)指定)
-
add
public void add(int index,E element)
Inserts the specified element at the specified position in this list.
Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
将指定元素插入到列表中的指定位置。
将当前位于该位置的元素(如果有)和任何后续元素向右移动(将一个元素添加到它们的索引中)。- Specified by:
add
in interfaceList<E>
- Overrides:
add
in classAbstractList<E>
- Parameters:
index
- index at which the specified element is to be inserted
要插入指定元素的索引element
- element to be inserted
要插入的元素- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size())
如果索引超出范围(索引< 0 ||索引> size())
-
remove
public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
删除列表中指定位置的元素。将任何后续元素向左移动(从它们的索引中减去1)。- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractList<E>
- Parameters:
index
- the index of the element to be removed
要删除的元素的索引- Returns:
- the element that was removed from the list
从列表中删除的元素 - Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size())
如果索引超出范围(索引< 0 ||索引>= size())
-
remove
public boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
If the list does not contain the element, it is unchanged.
More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
从该列表中删除指定元素的第一个匹配项(如果存在)。
如果列表不包含该元素,它将保持不变。
更正式地说,删除索引i最低的元素,使(o==null ?get(i)==null: o.equals(get(i))(如果存在这样一个元素)。
如果该列表包含指定的元素,则返回true(如果该列表由于调用而更改,则返回true)。- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceList<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
o
- element to be removed from this list, if present
元素将从此列表中删除(如果存在)- Returns:
- true if this list contained the specified element
如果此列表包含指定的元素,则为真
-
clear
public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.
从列表中删除所有元素。该调用返回后,列表将为空。- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractList<E>
-
addAll
public 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.
The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
(This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)
将指定集合中的所有元素按照指定集合的迭代器返回它们的顺序追加到此列表的末尾。
如果在操作过程中修改了指定的集合,则此操作的行为未定义。
(这意味着,如果指定的集合是这个列表,而这个列表不是空的,则此调用的行为是未定义的。)- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractCollection<E>
- Parameters:
c
- collection containing elements to be added to this list
集合,其中包含要添加到此列表的元素- Returns:
- true if this list changed as a result of the call
如果此列表因调用而更改,则为true - Throws:
NullPointerException
- if the specified collection is null
如果指定的集合为空- See Also:
AbstractCollection.add(Object)
-
addAll
public 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.
Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).
The new elements will appear in the list in the order that they are returned by the specified collection's iterator.
从指定位置开始,将指定集合中的所有元素插入此列表。
将当前位于该位置的元素(如果有)和任何后续元素向右移动(增加它们的索引)。
新元素将按照指定集合的迭代器返回它们的顺序出现在列表中。- Specified by:
addAll
in interfaceList<E>
- Overrides:
addAll
in classAbstractList<E>
- Parameters:
index
- index at which to insert the first element from the specified collection
从指定集合中插入第一个元素的索引c
- collection containing elements to be added to this list
集合,其中包含要添加到此列表的元素- Returns:
- true if this list changed as a result of the call
- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size())
如果索引超出范围(索引< 0 ||索引> size())NullPointerException
- if the specified collection is null
如果指定的集合为空
-
removeRange
protected void removeRange(int fromIndex,int toIndex)
Removes from this list all of the elements whose index is betweenfromIndex
, inclusive, andtoIndex
, exclusive.
Shifts any succeeding elements to the left (reduces their index).
This call shortens the list by(toIndex - fromIndex)
elements.
(IftoIndex==fromIndex
, this operation has no effect.)
从该列表中删除其索引在包含的fromIndex和排除的toIndex之间的所有元素。
将任何后续元素向左移动(减少它们的索引)。
这个调用通过(toIndex - fromIndex)元素缩短了列表。
(如果toIndex==fromIndex,则此操作无效。)- Overrides:
removeRange
in classAbstractList<E>
类AbstractList<E>中的removeRange- Parameters:
fromIndex
- index of first element to be removed
要删除的第一个元素的索引toIndex
- index after last element to be removed
要删除的最后一个元素之后的索引- Throws:
IndexOutOfBoundsException
- iffromIndex
ortoIndex
is out of range (fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex
)
如果fromIndex或toIndex超出范围(fromIndex < 0 || fromIndex >= size() || toIndex > size() || toIndex < fromIndex)
-
removeAll
public boolean removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.
从该列表中删除指定集合中包含的所有元素。- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Overrides:
removeAll
in classAbstractCollection<E>
类AbstractCollection<E>中的removeAll- Parameters:
c
- collection containing elements to be removed from this list
包含要从此列表中删除的元素的集合- Returns:
true
if this list changed as a result of the call
如果此列表因调用而更改,则为true- Throws:
ClassCastException
- 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 null
如果这个列表包含一个空元素,而指定的集合不允许空元素(可选),或者指定的集合为空- See Also:
Collection.contains(Object)
-
retainAll
public boolean retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection.
In other words, removes from this list all of its elements that are not contained in the specified collection.
仅保留此列表中包含在指定集合中的元素。
换句话说,从这个列表中删除指定集合中不包含的所有元素。- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceList<E>
- Overrides:
retainAll
in classAbstractCollection<E>
- Parameters:
c
- collection containing elements to be retained in this list
包含要保留在此列表中的元素的集合- Returns:
true
if this list changed as a result of the call
如果此列表因调用而更改,则为true- Throws:
ClassCastException
- 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 null
如果此列表包含空元素,而指定的集合不允许空元素(可选),或者指定的集合为空- See Also:
Collection.contains(Object)
-
listIterator
public 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.
The specified index indicates the first element that would be returned by an initial call tonext
.
An initial call toprevious
would return the element with the specified index minus one.
返回此列表中元素的列表迭代器(按适当的顺序),从列表中的指定位置开始。
指定的索引指示将由对next的初始调用返回的第一个元素。
对previous的初始调用将返回具有指定索引- 1的元素。
The returned list iterator is fail-fast.
返回的列表迭代器是故障快速的。- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
- Parameters:
index
- index of the first element to be returned from the list iterator (by a call tonext
)
从列表迭代器返回的第一个元素的索引(通过对next的调用)- Returns:
- a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
一个列表迭代器,遍历此列表中的元素(按适当的顺序),从列表中的指定位置开始 - Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()
)
如果索引超出范围(索引< 0 ||索引> size())
-
listIterator
public ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).
返回该列表中元素的列表迭代器(按适当的顺序)。
The returned list iterator is fail-fast.
返回的列表迭代器是故障快速的。- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
- Returns:
- a list iterator over the elements in this list (in proper sequence)
对列表中的元素使用列表迭代器(按适当的顺序) - See Also:
listIterator(int)
-
subList
public List<E> subList(int fromIndex,int toIndex)
Returns a view of the portion of this list between the specifiedfromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
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.
返回指定的fromIndex(包含)和toIndex(排他)之间的列表部分的视图。(如果fromIndex和toIndex相等,则返回的列表为空。)返回的列表由这个列表支持,因此返回列表中的非结构性更改将反映在这个列表中,反之亦然。返回的列表支持所有可选的列表操作。
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();
indexOf(Object)
andlastIndexOf(Object)
, and all of the algorithms in theCollections
class can be applied to a subList.
可以为indexOf(Object)和lastIndexOf(Object)构造类似的习惯用法,而Collections类中的所有算法都可以应用于一个子列表。
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.)
该方法返回的列表的语义将变得未定义,如果支持列表(即除通过返回的列表外,以任何方式对其进行结构修改。
(结构修改是指改变列表的大小,或者以一种正在进行的迭代可能产生错误结果的方式扰乱列表。)- Specified by:
subList
in interfaceList<E>
- Overrides:
subList
in classAbstractList<E>
- Parameters:
fromIndex
- low endpoint (inclusive) of the subList
子列表的低端点(包括)toIndex
- high endpoint (exclusive) of the subList
子列表的高端(不含)- Returns:
- a view of the specified range within this list
此列表中指定范围的视图 - Throws:
IndexOutOfBoundsException
- 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)
-
forEach
public void forEach(Consumer<? super E> action)
Description copied from interface:Iterable
从接口复制的描述:IterablePerforms the given action for each element of theIterable
until all elements have been processed or the action throws an exception.
Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified).
Exceptions thrown by the action are relayed to the caller.
为可迭代的每个元素执行给定的操作,直到处理完所有元素或操作引发异常。
除非实现类另行指定,否则操作将按照迭代的顺序执行(如果指定了迭代的顺序)。
操作引发的异常将被转发给调用者。
-
spliterator
public Spliterator<E> spliterator()
Creates a late-binding and fail-fastSpliterator
over the elements in this list.
在此列表中的元素上创建延迟绑定和故障快速Spliterator。
The
Spliterator
reportsSpliterator.SIZED
,Spliterator.SUBSIZED
, andSpliterator.ORDERED
. Overriding implementations should document the reporting of additional characteristic values.
Spliterator报告Spliterator。大小,Spliterator。小尺寸,Spliterator.ORDERED。覆盖实现应该记录额外特征值的报告。- Specified by:
spliterator
in interfaceIterable<E>
- Specified by:
spliterator
in interfaceCollection<E>
- Specified by:
spliterator
in interfaceList<E>
- Returns:
- a
Spliterator
over the elements in this list - Since:
- 1.8
-
removeIf
public boolean removeIf(Predicate<? super E> filter)
Description copied from interface:Collection
从接口复制的描述:集合Removes all of the elements of this collection that satisfy the given predicate.
Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.
删除此集合中满足给定谓词的所有元素。
迭代期间或由谓词抛出的错误或运行时异常将传递给调用者。- Specified by:
removeIf
in interfaceCollection<E>
- Parameters:
filter
- a predicate which returnstrue
for elements to be removed
为要删除的元素返回true的谓词- Returns:
true
if any elements were removed
如果删除了任何元素,则为真
-
replaceAll
public void replaceAll(UnaryOperator<E> operator)
Description copied from interface:List
从接口复制的描述:列表Replaces each element of this list with the result of applying the operator to that element.
Errors or runtime exceptions thrown by the operator are relayed to the caller.
将此列表中的每个元素替换为将运算符应用于该元素的结果。
操作符抛出的错误或运行时异常将传递给调用者。- Specified by:
replaceAll
in interfaceList<E>
- Parameters:
operator
- the operator to apply to each element
应用于每个元素的运算符
-
sort
public void sort(Comparator<? super E> c)
Description copied from interface:List
Sorts this list using the suppliedComparator
to compare elements.
从接口复制的描述:列表
使用提供的比较器对列表进行排序,以比较元素。- Specified by:
sort
in interfaceList<E>
- Parameters:
c
- theComparator
used to compare list elements.
Anull
value indicates that the elements' natural ordering should be used.
用于比较列表元素的比较器。空值表示应该使用元素的自然顺序。