Module java.base
Package java.util

Class ArrayList<E>

Type Parameters:类型参数:
E - the type of elements in this list此列表中元素的类型
All Implemented Interfaces:所有实现的接口:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
Direct Known Subclasses:直接已知子类:
AttributeList, RoleList, RoleUnresolvedList

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable
Resizable-array implementation of the List interface. List接口的可调整大小的数组实现。Implements all optional list operations, and permits all elements, including null. 实现所有可选的列表操作,并允许所有元素,包括nullIn addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. 除了实现List接口外,此类还提供了一些方法来操作内部用于存储列表的数组的大小。(This class is roughly equivalent to Vector, except that it is unsynchronized.)(该类大致相当于Vector,只是不同步。)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. sizeisEmptygetsetiteratorlistIterator操作以固定时间运行。The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. add操作在摊销的固定时间内运行,即添加n个元素需要O(n)倍时间。All of the other operations run in linear time (roughly speaking). 所有其他操作都在线性时间内运行(粗略地说)。The constant factor is low compared to that for the LinkedList implementation. LinkedList实现相比,常数因子较低。

Each ArrayList instance has a capacity. 每个ArrayList实例都有一个容量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. 当元素添加到ArrayList时,其容量会自动增长。The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost. 除了添加要素具有恒定的摊余时间成本这一事实之外,没有详细说明增长政策。

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. 应用程序可以在使用ensureCapacity操作添加大量元素之前增加ArrayList实例的容量。This may reduce the amount of incremental reallocation. 这可能会减少增量重新分配的数量。

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. 如果多个线程同时访问ArrayList实例,并且至少有一个线程在结构上修改该列表,则必须在外部对其进行同步。(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. 如果不存在此类对象,则应使用Collections.synchronizedList方法“包装”列表。This is best done at creation time, to prevent accidental unsynchronized access to the list:最好在创建时执行此操作,以防止意外不同步地访问列表:

List list = Collections.synchronizedList(new ArrayList(...));

The iterators returned by this class's iterator and listIterator methods are fail-fast:此类的iterator方法和listIterator方法返回的迭代器是快速失败 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. 如果在创建迭代器后的任何时候,以迭代器自己的remove方法或add方法以外的任何方式对列表进行结构修改,迭代器将抛出ConcurrentModificationExceptionThus, 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. 因此,在面对并发修改时,迭代器会快速、干净地失败,而不是在将来的不确定时间冒着任意、不确定行为的风险。

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. 快速失败迭代器会尽最大努力抛出ConcurrentModificationExceptionTherefore, 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.迭代器的快速失败行为应该只用于检测bug。

This class is a member of the Java Collections Framework.此类是Java集合框架的成员。

Since:始于:
1.2
See Also:参阅:
Collection, List, LinkedList, Vector, Serialized Form
  • Field Summary字段摘要

    Fields declared in class java.util.AbstractList在java.util.AbstractList中声明的字段

    modCount
  • Constructor Summary构造函数摘要

    Constructors构造函数
    Constructor构造函数
    Description描述
    Constructs an empty list with an initial capacity of ten.构造一个初始容量为10的空列表。
    ArrayList​(int initialCapacity)
    Constructs an empty list with the specified initial capacity.构造具有指定初始容量的空列表。
    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.按照集合迭代器返回的顺序,构造包含指定集合元素的列表。
  • Method Summary方法摘要

    Modifier and Type修饰符和类型
    Method方法
    Description描述
    void
    add​(int index, E element)
    Inserts the specified element at the specified position in this list.在此列表中的指定位置插入指定元素。
    boolean
    add​(E e)
    Appends the specified element to the end of this list.将指定的元素追加到此列表的末尾。
    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.从指定位置开始,将指定集合中的所有元素插入此列表。
    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.按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到此列表的末尾。
    void
    Removes all of the elements from this list.从此列表中删除所有元素。
    Returns a shallow copy of this ArrayList instance.返回此ArrayList实例的浅层副本。
    boolean
    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实例的容量,以确保它至少可以容纳最小容量参数指定的元素数。
    boolean
    equals​(Object o)
    Compares the specified object with this list for equality.比较指定对象与此列表是否相等。
    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.Iterable的每个元素执行给定的操作,直到所有元素都已处理或该操作引发异常为止。
    get​(int index)
    Returns the element at the specified position in this list.返回此列表中指定位置的元素。
    int
    Returns the hash code value for this list.返回此列表的哈希代码值。
    int
    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
    Returns true if this list contains no elements.如果此列表不包含任何元素,则返回true
    Returns an iterator over the elements in this list in proper sequence.按正确的顺序返回此列表中元素的迭代器。
    int
    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。
    Returns a list iterator over the elements in this list (in proper sequence).返回此列表中元素的列表迭代器(按正确顺序)。
    listIterator​(int index)
    Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.返回此列表中元素的列表迭代器(按正确顺序),从列表中的指定位置开始。
    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
    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(不含)之间的所有元素。
    boolean
    Retains only the elements in this list that are contained in the specified collection.仅保留此列表中包含在指定集合中的元素。
    set​(int index, E element)
    Replaces the element at the specified position in this list with the specified element.用指定的元素替换此列表中指定位置的元素。
    int
    Returns the number of elements in this list.返回此列表中的元素数。
    Creates a late-binding and fail-fast Spliterator over the elements in this list.在此列表中的元素上创建后期绑定快速失败拆分器
    subList​(int fromIndex, int toIndex)
    Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.返回此列表中指定的fromIndex(包含)和toIndex(不含)之间部分的视图。
    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
    Trims the capacity of this ArrayList instance to be the list's current size.将此ArrayList实例的容量修剪为列表的当前大小。

    Methods declared in class java.util.AbstractList在java.util.AbstractList类中声明的方法

    equals, hashCode

    Methods declared in class java.util.AbstractCollection在java.util.AbstractCollection类中声明的方法

    containsAll, toString

    Methods declared in class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods declared in interface java.util.Collection在java.util.Collection接口中声明的方法

    parallelStream, stream, toArray

    Methods declared in interface java.util.List在java.util.List接口中声明的方法

    containsAll, replaceAll, sort
  • Constructor Details构造函数细节

    • 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如果指定的集合为null
  • Method Details方法详细信息

    • trimToSize

      public void trimToSize()
      Trims the capacity of this ArrayList instance to be the list's current size. 将此ArrayList实例的容量修剪为列表的当前大小。An application can use this operation to minimize the storage of an ArrayList instance.应用程序可以使用此操作最小化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实例的容量,以确保它至少可以容纳最小容量参数指定的元素数。
      Parameters:参数:
      minCapacity - the desired minimum capacity所需的最小容量
    • size

      public int size()
      Returns the number of elements in this list.返回此列表中的元素数。
      Specified by:指定者:
      size in interface Collection<E>Collection<E>接口中的size
      Specified by:指定者:
      size in interface List<E>List<E>接口中的size
      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 interface Collection<E>Collection<E>接口中的isEmpty
      Specified by:指定者:
      isEmpty in interface List<E>List<E>接口中的isEmpty
      Overrides:覆盖:
      isEmpty in interface AbstractCollection<E>AbstractCollection<E>类中的isEmpty
      Returns:返回:
      true if this list contains no elements如果此列表不包含任何元素,则为true
    • contains

      public boolean contains(Object o)
      Returns true if this list contains the specified element. 如果此列表包含指定的元素,则返回trueMore formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).更正式地说,当且仅当此列表包含至少一个元素e,使得Objects.equals(o, e)true时,才返回true
      Specified by:指定者:
      contains in interface Collection<E>Collection<E>接口中的contains
      Specified by:指定者:
      contains in interface List<E>List<E>接口中的contains
      Overrides:覆盖:
      contains in interface AbstractCollection<E>AbstractCollection<E>类中的contains
      Parameters:参数:
      o - element whose presence in this list is to be tested要测试其在该列表中的存在的元素
      Returns:返回:
      true if this list contains the specified element如果此列表包含指定的元素,则为true
    • 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. 返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回-1。More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.更正式地说,返回满足Objects.equals(o, get(i))的最低的索引i,如果没有这样的索引,则返回-1。
      Specified by:指定者:
      indexOf in interface List<E>List<E>接口中的indexOf
      Overrides:覆盖:
      indexOf in interface AbstractList<E>AbstractList<E>类中的indexOf
      Parameters:参数:
      o - element to search for要搜索的元素
      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
    • lastIndexOf

      public 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。More formally, returns the highest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.更正式地说,返回满足Objects.equals(o, get(i))的最高的索引i,如果没有这样的索引,则返回-1。
      Specified by:指定者:
      lastIndexOf in interface List<E>List<E>接口中的lastIndexOf
      Overrides:覆盖:
      lastIndexOf in interface AbstractList<E>AbstractList<E>类中的lastIndexOf
      Parameters:参数:
      o - element to search for要搜索的元素
      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
    • clone

      public Object clone()
      Returns a shallow copy of this ArrayList instance. 返回此ArrayList实例的浅层副本。(The elements themselves are not copied.)(元素本身不会被复制。)
      Overrides:覆盖:
      clone in interface ObjectObject类中的clone
      Returns:返回:
      a clone of this ArrayList instanceArrayList实例的克隆
      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 interface Collection<E>Collection<E>接口中的toArray
      Specified by:指定者:
      toArray in interface List<E>List<E>接口中的toArray
      Overrides:覆盖:
      toArray in interface AbstractCollection<E>AbstractCollection<E>类中的toArray
      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. 如果列表适合指定的数组,且有空闲空间(即数组中的元素多于列表),则紧跟在集合结束后的数组中的元素将设置为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.)(只有在调用者知道列表不包含任何空元素时,这才有助于确定列表的长度。)

      Specified by:指定者:
      toArray in interface Collection<E>Collection<E>接口中的toArray
      Specified by:指定者:
      toArray in interface List<E>List<E>接口中的toArray
      Overrides:覆盖:
      toArray in interface AbstractCollection<E>AbstractCollection<E>类中的toArray
      Type Parameters:类型参数:
      T - the component 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如果指定的数组为null
    • get

      public E get(int index)
      Returns the element at the specified position in this list.返回此列表中指定位置的元素。
      Specified by:指定者:
      get in interface List<E>List<E>接口中的get
      Specified by:指定者:
      get in interface AbstractList<E>AbstractList<E>类中的get
      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())
    • 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 interface List<E>List<E>接口中的set
      Overrides:覆盖:
      set in interface AbstractList<E>AbstractList<E>类中的set
      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())
    • add

      public boolean add(E e)
      Appends the specified element to the end of this list.将指定的元素追加到此列表的末尾。
      Specified by:指定者:
      add in interface Collection<E>Collection<E>接口中的add
      Specified by:指定者:
      add in interface List<E>List<E>接口中的add
      Overrides:覆盖:
      add in interface AbstractList<E>AbstractList<E>类中的add
      Parameters:参数:
      e - element to be appended to this list要附加到此列表的元素
      Returns:返回:
      true (as specified by Collection.add(E))(由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 interface List<E>List<E>接口中的add
      Overrides:覆盖:
      add in interface AbstractList<E>AbstractList<E>类中的add
      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())
    • 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).将任何后续元素向左移动(从其索引中减去一个)。
      Specified by:指定者:
      remove in interface List<E>List<E>接口中的remove
      Overrides:覆盖:
      remove in interface AbstractList<E>AbstractList<E>类中的remove
      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())
    • equals

      public boolean equals(Object o)
      Compares the specified object with this list for equality. 比较指定对象与此列表是否相等。Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. 当且仅当指定对象也是列表、两个列表的大小相同且两个列表中所有对应的元素对相等时,返回true(Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) (如果(e1==null ? e2==null : e1.equals(e2)),则两个元素e1e2相等。)In other words, two lists are defined to be equal if they contain the same elements in the same order.换句话说,如果两个列表包含相同顺序的相同元素,则它们被定义为相等。
      Specified by:指定者:
      equals in interface Collection<E>Collection<E>接口中的equals
      Specified by:指定者:
      equals in interface List<E>List<E>接口中的equals
      Overrides:覆盖:
      equals in interface AbstractList<E>AbstractList<E>类中的equals
      Parameters:参数:
      o - the object to be compared for equality with this list要比较的对象是否与此列表相等
      Returns:返回:
      true if the specified object is equal to this list如果指定的对象等于此列表,则为true
      See Also:参阅:
      Object.hashCode(), HashMap
    • hashCode

      public int hashCode()
      Returns the hash code value for this list.返回此列表的哈希代码值。
      Specified by:指定者:
      hashCode in interface Collection<E>Collection<E>接口中的hashCode
      Specified by:指定者:
      hashCode in interface List<E>List<E>接口中的hashCode
      Overrides:覆盖:
      hashCode in interface AbstractList<E>AbstractList<E>类中的hashCode
      Returns:返回:
      the hash code value for this list此列表的哈希代码值
      See Also:参阅:
      Object.equals(java.lang.Object), System.identityHashCode(java.lang.Object)
    • 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 Objects.equals(o, get(i)) (if such an element exists). 更正式地说,删除索引i最低的元素,只要满足Objects.equals(o, get(i))(如果存在这样的元素)。Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).如果此列表包含指定的元素,则返回true(如果此列表因调用而更改,则返回等效值)。
      Specified by:指定者:
      remove in interface Collection<E>Collection<E>接口中的remove
      Specified by:指定者:
      remove in interface List<E>List<E>接口中的remove
      Overrides:覆盖:
      remove in interface AbstractCollection<E>AbstractCollection<E>类中的remove
      Parameters:参数:
      o - element to be removed from this list, if present要从此列表中删除的元素(如果存在)
      Returns:返回:
      true if this list contained the specified element如果此列表包含指定的元素,则为true
    • 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 interface Collection<E>Collection<E>接口中的clear
      Specified by:指定者:
      clear in interface List<E>List<E>接口中的clear
      Overrides:覆盖:
      clear in interface AbstractList<E>AbstractList<E>类中的clear
    • 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 interface Collection<E>Collection<E>接口中的addAll
      Specified by:指定者:
      addAll in interface List<E>List<E>接口中的addAll
      Overrides:覆盖:
      addAll in interface AbstractCollection<E>AbstractCollection<E>类中的addAll
      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如果指定的集合为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 interface List<E>List<E>接口中的addAll
      Overrides:覆盖:
      addAll in interface AbstractList<E>AbstractList<E>类中的addAll
      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如果此列表因调用而更改,则为true
      Throws:抛出:
      IndexOutOfBoundsException - if the index is out of range如果索引超出范围 (index < 0 || index > size())
      NullPointerException - if the specified collection is null如果指定的集合为null
    • removeRange

      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(不含)之间的所有元素。Shifts any succeeding elements to the left (reduces their index). 将任何后续元素向左移动(减少其索引)。This call shortens the list by (toIndex - fromIndex) elements. 此调用将列表缩短(toIndex - fromIndex)个元素。(If toIndex==fromIndex, this operation has no effect.)(如果toIndex==fromIndex,则此操作将无效。)
      Overrides:覆盖:
      removeRange in interface AbstractList<E>AbstractList<E>类中的removeRange
      Parameters:参数:
      fromIndex - index of first element to be removed要删除的第一个元素的索引
      toIndex - index after last element to be removed要删除的最后一个元素后的索引
      Throws:抛出:
      IndexOutOfBoundsException - if fromIndex or toIndex is out of range如果fromIndextoIndex超出范围 (fromIndex < 0 || 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 interface Collection<E>Collection<E>接口中的removeAll
      Specified by:指定者:
      removeAll in interface List<E>List<E>接口中的removeAll
      Overrides:覆盖:
      removeAll in interface AbstractCollection<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如果此列表包含null元素且指定的集合不允许null元素(可选),或者指定的集合为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 interface Collection<E>Collection<E>接口中的retainAll
      Specified by:指定者:
      retainAll in interface List<E>List<E>接口中的retainAll
      Overrides:覆盖:
      retainAll in interface AbstractCollection<E>AbstractCollection<E>类中的retainAll
      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如果此列表包含null元素且指定的集合不允许null元素(可选),或者指定的集合为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 to next. 指定的索引指示将由对next的初始调用返回的第一个元素。An initial call to previous would return the element with the specified index minus one. previous的初始调用将返回指定索引为负1的元素。

      The returned list iterator is fail-fast.返回的列表迭代器是快速失败的。

      Specified by:指定者:
      listIterator in interface List<E>List<E>接口中的listIterator
      Overrides:覆盖:
      listIterator in interface AbstractList<E>AbstractList<E>类中的listIterator
      Parameters:参数:
      index - index of the first element to be returned from the list iterator (by a call to next)从列表迭代器返回的第一个元素的索引(通过调用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())
    • 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 interface List<E>List<E>接口中的listIterator
      Overrides:覆盖:
      listIterator in interface AbstractList<E>AbstractList<E>类中的listIterator
      Returns:返回:
      a list iterator over the elements in this list (in proper sequence)此列表中元素的列表迭代器(按正确顺序)
      See Also:参阅:
      listIterator(int)
    • iterator

      public Iterator<E> iterator()
      Returns an iterator over the elements in this list in proper sequence. 按正确的顺序返回此列表中元素的迭代器。

      The returned iterator is fail-fast.返回的迭代器是快速失败的。

      Specified by:指定者:
      iterator in interface Collection<E>Collection<E>接口中的iterator
      Specified by:指定者:
      iterator in interface Iterable<E>Iterable<E>接口中的iterator
      Specified by:指定者:
      iterator in interface List<E>List<E>接口中的iterator
      Overrides:覆盖:
      iterator in interface AbstractList<E>AbstractList<E>类中的iterator
      Returns:返回:
      an iterator over the elements in this list in proper sequence按正确顺序遍历此列表中元素的迭代器
    • subList

      public 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(不含)之间部分的视图。(If fromIndex and toIndex are equal, the returned list is empty.) (如果fromIndextoIndex相等,则返回的列表为空。)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. 可以为indexOf(Object)lastIndexOf(Object)构造类似的习惯用法,集合类中的所有算法都可以应用于子列表。

      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 interface List<E>List<E>接口中的subList
      Overrides:覆盖:
      subList in interface AbstractList<E>AbstractList<E>类中的subList
      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)
      IllegalArgumentException - if the endpoint indices are out of order如果端点索引不符合顺序 (fromIndex > toIndex)
    • forEach

      public void forEach(Consumer<? super E> action)
      Description copied from interface: 描述复制自接口:Iterable
      Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Iterable的每个元素执行给定的操作,直到所有元素都已处理或该操作引发异常为止。Actions are performed in the order of iteration, if that order is specified. 如果指定了迭代顺序,则按迭代顺序执行操作。Exceptions thrown by the action are relayed to the caller. 操作引发的异常将转发给调用方。

      The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy.如果操作执行了修改元素的底层源的副作用,则此方法的行为未指定,除非重写类指定了并发修改策略。

      Specified by:指定者:
      forEach in interface Iterable<E>Iterable<E>接口中的forEach
      Parameters:参数:
      action - The action to be performed for each element为每个元素执行的操作
      Throws:抛出:
      NullPointerException - if the specified action is null如果指定的操作为空
    • spliterator

      public Spliterator<E> spliterator()
      Creates a late-binding and fail-fast Spliterator over the elements in this list. 在此列表中的元素上创建后期绑定快速失败拆分器

      The Spliterator reports Spliterator.SIZED, Spliterator.SUBSIZED, and Spliterator.ORDERED. Spliterator报告Spliterator.SIZEDSpliterator.SUBSIZEDSpliterator.ORDEREDOverriding implementations should document the reporting of additional characteristic values.覆盖实现应记录额外特征值的报告。

      Specified by:指定者:
      spliterator in interface Collection<E>Collection<E>接口中的spliterator
      Specified by:指定者:
      spliterator in interface Iterable<E>Iterable<E>接口中的spliterator
      Specified by:指定者:
      spliterator in interface List<E>List<E>接口中的spliterator
      Returns:返回:
      a Spliterator over the elements in this list此列表中元素上的Spliterator
      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 interface Collection<E>Collection<E>接口中的removeIf
      Parameters:参数:
      filter - a predicate which returns true for elements to be removed为要删除的元素返回true的谓词
      Returns:返回:
      true if any elements were removed如果删除了任何元素,则为true
      Throws:抛出:
      NullPointerException - if the specified filter is null如果指定的筛选器为null