
HarmonyOS API:@ohos.util.List (线性容器List)
版本:v3.1 Beta
@ohos.util.List (线性容器List)
更新时间: 2023-02-24 16:50
List底层通过单向链表实现,每个节点有一个指向后一个元素的引用。当需要查询元素时,必须从头遍历,插入、删除效率高,查询效率低。List允许元素为null。
List和LinkedList相比,LinkedList是双向链表,可以快速地在头尾进行增删,而List是单向链表,无法双向操作。
推荐使用场景: 当需要频繁的插入删除时,推荐使用List高效操作。
文档中存在泛型的使用,涉及以下泛型标记符:
- T: Type,类
说明
本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
导入模块
List
属性
系统能力: SystemCapability.Utils.Lang
名称 | 类型 | 可读 | 可写 | 说明 |
length | number | 是 | 否 | List的元素个数。 |
constructor
constructor()
List的构造函数。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200012 | The List's constructor cannot be directly invoked. |
示例:
add
add(element: T): boolean
在List尾部插入元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
element | T | 是 | 添加进去的元素。 |
返回值:
类型 | 说明 |
boolean | 插入成功返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The add method cannot be bound. |
示例:
insert
insert(element: T, index: number): void
在长度范围内任意位置插入指定元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
element | T | 是 | 插入元素。 |
index | number | 是 | 插入的位置索引。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The insert method cannot be bound. |
10200001 | The value of index is out of range. |
示例:
has
has(element: T): boolean
判断此List中是否含有该指定元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
boolean | 包含指定元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The has method cannot be bound. |
示例:
get
get(index: number): T
根据下标获取List中的元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
index | number | 是 | 要查找的下标。 |
返回值:
类型 | 说明 |
T | 根据下标查找到的元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The get method cannot be bound. |
示例:
getLastIndexOf
getLastIndexOf(element: T): number
查找指定元素最后一次出现的下标值,查找失败返回-1。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
number | 返回指定元素最后一次出现的下标值,没有找到返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The getLastIndexOf method cannot be bound. |
示例:
getIndexOf
getIndexOf(element: T): number
查找指定元素第一次出现的下标值,查找失败返回-1。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
number | 返回第一次找到指定元素的下标,没有找到返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The getIndexOf method cannot be bound. |
示例:
equal
equal(obj: Object): boolean
比较指定对象与此List是否相等。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
obj | Object | 是 | 用来比较的对象。 |
返回值:
类型 | 说明 |
boolean | 如果对象与此列表相同返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The equal method cannot be bound. |
示例:
removeByIndex
removeByIndex(index: number): T
根据元素的下标值查找元素,返回元素后将其删除。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
index | number | 是 | 指定元素的下标值。 |
返回值:
类型 | 说明 |
T | 返回删除的元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The removeByIndex method cannot be bound. |
10200001 | The value of index is out of range. |
示例:
remove
remove(element: T): boolean
删除查找到的第一个指定的元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
boolean | 删除成功返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The remove method cannot be bound. |
示例:
replaceAllElements
replaceAllElements(callbackFn: (value: T, index?: number, list?: List<T>) => T,
thisArg?: Object): void
用户操作List中的元素,用操作后的元素替换原元素并返回操作后的元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
callbackFn | function | 是 | 回调函数。 |
thisArg | Object | 否 | callbackfn被调用时用作this值。 |
callbackfn的参数说明:
参数名 | 类型 | 必填 | 说明 |
value | T | 是 | 当前遍历到的元素。 |
index | number | 否 | 当前遍历到的下标值。 |
list | List<T> | 否 | 当前调用replaceAllElements方法的实例对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The replaceAllElements method cannot be bound. |
示例:
forEach
forEach(callbackFn: (value: T, index?: number, List?: List<T>) => void,
thisArg?: Object): void
通过回调函数来遍历List实例对象上的元素以及元素对应的下标。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
callbackFn | function | 是 | 回调函数。 |
thisArg | Object | 否 | callbackfn被调用时用作this值。 |
callbackfn的参数说明:
参数名 | 类型 | 必填 | 说明 |
value | T | 是 | 当前遍历到的元素。 |
index | number | 否 | 当前遍历到的下标值。 |
List | List<T> | 否 | 当前调用forEach方法的实例对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The forEach method cannot be bound. |
示例:
sort
sort(comparator: (firstValue: T, secondValue: T) => number): void
对List中的元素进行一个排序操作。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
comparator | function | 是 | 回调函数。 |
comparator的参数说明:
参数名 | 类型 | 必填 | 说明 |
firstValue | T | 是 | 前一项元素。 |
secondValue | T | 是 | 后一项元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The sort method cannot be bound. |
示例:
getSubList
getSubList(fromIndex: number, toIndex: number): List<T>
根据下标截取List中的一段元素,并返回这一段List实例,包括起始值但不包括终止值。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
fromIndex | number | 是 | 起始下标。 |
toIndex | number | 是 | 终止下标。 |
返回值:
类型 | 说明 |
List<T> | 返回List对象实例。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The getSubList method cannot be bound. |
10200001 | The value of fromIndex or toIndex is out of range. |
示例:
clear
clear(): void
清除List中的所有元素,并把length置为0。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The clear method cannot be bound. |
示例:
set
set(index: number, element: T): T
将此 List 中指定位置的元素替换为指定元素。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
index | number | 是 | 查找的下标值。 |
element | T | 是 | 用来替换的元素。 |
返回值:
类型 | 说明 |
T | 返回替换后的元素 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The set method cannot be bound. |
10200001 | The value of index is out of range. |
示例:
convertToArray
convertToArray(): Array<T>
把当前List实例转换成数组,并返回转换后的数组。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
Array<T> | 返回转换后的数组。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The convertToArray method cannot be bound. |
示例:
isEmpty
isEmpty(): boolean
判断该List是否为空。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
boolean | 为空返回true,不为空返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The isEmpty method cannot be bound. |
示例:
getFirst
getFirst(): T
获取List实例中的第一个元素。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
T | 返回实例的第一个元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The getFirst method cannot be bound. |
示例:
getLast
getLast(): T
获取List实例中的最后一个元素。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
T | 返回实例的最后一个元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The getLast method cannot be bound. |
示例:
[Symbol.iterator]
[Symbol.iterator](): IterableIterator<T>
返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
IterableIterator<T> | 返回一个迭代器。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
10200011 | The Symbol.iterator method cannot be bound. |
示例:
