A List
is a collection which maintains an ordering for its elements. Every element in the List
has an index. Each element can thus be accessed by its index, with the first index being zero. Normally, List
s allow duplicate elements, as compared to Sets, where elements have to be unique.
List接口实现Collection接口,可以在其中保持元素的顺序。每一个List中的元素都有一个角标(index),角标从0开始,可以通过角标来访问元素。通常List允许其元素重复,而Set中的元素必须唯一。
在通过角标来访问List的元素的过程中,如果角标超过了List的角标范围,将会抛出角标越界异常(IndexOutOfBoundsException)。
List的子类:
除了继承自Collection接口的方法之外,List主要添加了一些通过角标进行元素操作的方法。
public abstract boolean add (E object)
Adds the specified object at the end of this List
.
public abstract void add (int location, E object)
Inserts the specified object into this List
at the specified location. The object is inserted before the current element at the specified location. If the location is equal to the size of this List
, the object is added at the end. If the location is smaller than the size of this List
, then all elements beyond the specified location are moved by one position towards the end of the List
.
在指定的位置添加特定对象。对象将被添加到当前指定位置的元素的前面。如果指定的位置等于List的大小,那么对象将被添加到List的尾部。如果指定的位置小于List的大小,那么所有处于该位置后面的元素都将会向后一次移动一个位置。如果指定的位置大于List的大小,那么将会抛出角标越界异常(IndexOutOfBoundsException)。
Parameters
location | the index at which to insert. |
---|---|
object | the object to add. |
Throws
UnsupportedOperationException | if adding to this List is not supported. |
---|---|
ClassCastException | if the class of the object is inappropriate for this List . |
IllegalArgumentException | if the object cannot be added to this List . |
IndexOutOfBoundsException | if location < 0 || location > size() |
public abstract E get (int location)
Returns the element at the specified location in this List
.
返回指定位置的元素。
Parameters
location | the index of the element to return. |
---|
Returns
- the element at the specified location.
Throws
IndexOutOfBoundsException | if location < 0 || location >= size() |
---|
public abstract int indexOf (Object object)
Searches this List
for the specified object and returns the index of the first occurrence.
返回第一个符合指定对象的元素的角标。如果没有找到符合该对象的元素,将会返回-1。
Parameters
object | the object to search for. |
---|
Returns
- the index of the first occurrence of the object or -1 if the object was not found.
public abstract int lastIndexOf (Object object)
Searches this List
for the specified object and returns the index of the last occurrence.
返回最后一个符合指定对象的元素的角标。如果没有找到符合该对象的元素,将会返回-1。
Parameters
object | the object to search for. |
---|
Returns
- the index of the last occurrence of the object, or -1 if the object was not found.
public abstract ListIterator<E> listIterator (int location)
Returns a list iterator on the elements of this List
. The elements are iterated in the same order as they occur in the List
. The iteration starts at the specified location.
返回一个基于该List元素的ListIterator。该Iterator从指定位置开始,其顺序与List中的元素顺序一致。
Parameters
location | the index at which to start the iteration. |
---|
Returns
- a list iterator on the elements of this
List
.
Throws
IndexOutOfBoundsException | if location < 0 || location > size() |
---|
See Also
public abstract ListIterator<E> listIterator ()
Returns a List
iterator on the elements of this List
. The elements are iterated in the same order that they occur in the List
.
Returns
- a
List
iterator on the elements of thisList
See Also
public abstract E set (int location, E object)
Replaces the element at the specified location in this List
with the specified object. This operation does not change the size of the List
.
替换指定位置上的元素。该操作不会改变List的大小,只会将指定位置上的元素替换为给定的对象。
Parameters
location | the index at which to put the specified object. |
---|---|
object | the object to insert. |
Returns
- the previous element at the index.
Throws
UnsupportedOperationException | if replacing elements in this List is not supported. |
---|---|
ClassCastException | if the class of an object is inappropriate for this List . |
IllegalArgumentException | if an object cannot be added to this List . |
IndexOutOfBoundsException | if location < 0 || location >= size() |
public abstract List<E> subList (int start, int end)
Returns a List
of the specified portion of this List
from the given start index to the end index minus one. The returned List
is backed by this List
so changes to it are reflected by the other.
返回当前List的一部分,从给定的起始角标到结束角标的前一位(即不包括结束角标的元素)。
Parameters
start | the index at which to start the sublist. |
---|---|
end | the index one past the end of the sublist. |
Returns
- a list of a portion of this
List
.
Throws
IndexOutOfBoundsException | if start < 0, start > end or end > size() |
---|