Java数据结构-List(二)

来源:互联网 发布:小意思托福 mac 编辑:程序博客网 时间:2024/05/23 12:46

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, Lists 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)

Added in API level 1

Adds the specified object at the end of this List.

类似的添加方法,默认都添加到List的尾部。

public abstract void add (int location, E object)

Added in API level 1

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
locationthe index at which to insert.objectthe object to add.
Throws
UnsupportedOperationExceptionif adding to this List is not supported.ClassCastExceptionif the class of the object is inappropriate for this List.IllegalArgumentExceptionif the object cannot be added to this List.IndexOutOfBoundsExceptionif location < 0 || location > size()


public abstract E get (int location)

Added in API level 1

Returns the element at the specified location in this List.

返回指定位置的元素。

Parameters
locationthe index of the element to return.
Returns
  • the element at the specified location.
Throws
IndexOutOfBoundsExceptionif location < 0 || location >= size()

public abstract int indexOf (Object object)

Added in API level 1

Searches this List for the specified object and returns the index of the first occurrence.

返回第一个符合指定对象的元素的角标。如果没有找到符合该对象的元素,将会返回-1。

Parameters
objectthe 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)

Added in API level 1

Searches this List for the specified object and returns the index of the last occurrence.

返回最后一个符合指定对象的元素的角标。如果没有找到符合该对象的元素,将会返回-1。

Parameters
objectthe 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)

Added in API level 1

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中的元素顺序一致。
可以使用listIterator(size())方法来获取一个iterator,然后通过iterator的hasPrevious()方法和previous()方法来实现倒序的遍历。

Parameters
locationthe index at which to start the iteration.
Returns
  • a list iterator on the elements of this List.
Throws
IndexOutOfBoundsExceptionif location < 0 || location > size()
See Also
  • ListIterator

public abstract ListIterator<E> listIterator ()

Added in API level 1

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
  • List iterator on the elements of this List
See Also
  • ListIterator


public abstract E set (int location, E object)

Added in API level 1

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
locationthe index at which to put the specified object.objectthe object to insert.
Returns
  • the previous element at the index.
Throws
UnsupportedOperationExceptionif replacing elements in this List is not supported.ClassCastExceptionif the class of an object is inappropriate for this List.IllegalArgumentExceptionif an object cannot be added to this List.IndexOutOfBoundsExceptionif location < 0 || location >= size()

public abstract List<E> subList (int start, int end)

Added in API level 1

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的一部分,从给定的起始角标到结束角标的前一位(即不包括结束角标的元素)。
注意:返回的List还是由原List来保存的,修改其中一个List将会导致另外一个List同样产生变化。

Parameters
startthe index at which to start the sublist.endthe index one past the end of the sublist.
Returns
  • a list of a portion of this List.
Throws
IndexOutOfBoundsExceptionif start < 0, start > end or end > size()

0 0
原创粉丝点击