Java数据结构-List(二)

本文深入探讨了Java中List接口及其实现方式,详细解释了List的基本概念、操作方法和注意事项,帮助开发者掌握如何有效地使用List来组织和管理数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
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)
Added in  API level 1

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)
Added in  API level 1

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)
Added in  API level 1

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)
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
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 ()
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


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
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)
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
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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值