# @ohos.util.List (线性容器List)
List底层通过单向链表实现,每个节点有一个指向后一个元素的引用。当需要查询元素时,必须从头遍历,插入、删除效率高,查询效率低。List允许元素为null。
List和[LinkedList](js-apis-linkedlist.md)相比,LinkedList是双向链表,可以快速地在头尾进行增删,而List是单向链表,无法双向操作。
> **注意:**
>
> 在List中使用\[index\]的方式虽然能够获取对应位置的元素,但这会导致未定义结果。推荐使用get()方法。
**推荐使用场景:** 当需要频繁的插入删除元素,并且需要使用单向链表时,推荐使用List高效操作。
文档中存在泛型的使用,涉及以下泛型标记符:
- T:Type,类
> **说明:**
>
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```ts
import { List } from '@kit.ArkTS';
```
## List
### 属性
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
| 名称 | 类型 | 可读 | 可写 | 说明 |
| -------- | -------- | -------- | -------- | -------- |
| length | number | 是 | 否 | List的元素个数。 |
### constructor
constructor()
List的构造函数。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200012 | The List's constructor cannot be directly invoked. |
**示例:**
```ts
let list: List = new List();
```
### add
add(element: T): boolean
在List尾部插入元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| element | T | 是 | 添加进去的元素。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 插入成功返回true,否则返回false。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The add method cannot be bound. |
**示例:**
```ts
let list: List = new List();
let result1 = list.add("a");
let result2 = list.add(1);
let b = [1, 2, 3];
let result3 = list.add(b);
class C {
name: string = ''
age: string = ''
}
let c: C = {name : "Dylan", age : "13"};
let result4 = list.add(c);
let result5 = list.add(false);
```
### insert
insert(element: T, index: number): void
在长度范围内任意位置插入指定元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| element | T | 是 | 插入元素。 |
| index | number | 是 | 插入的位置索引。需要小于等于int32_max即2147483647。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 10200001 | The value of index is out of range. |
| 10200011 | The insert method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.insert("A", 0);
list.insert(0, 1);
list.insert(true, 2);
```
### has
has(element: T): boolean
判断此List中是否含有该指定元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| element | T | 是 | 指定元素。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 包含指定元素返回true,否则返回false。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The has method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add("squirrel");
let result = list.has("squirrel");
```
### get
get(index: number): T
根据下标获取List中的元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 要查找的下标。需要小于等于int32_max即2147483647。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| T | 根据下标查找到的元素。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200011 | The get method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.get(2);
```
### getLastIndexOf
getLastIndexOf(element: T): number
查找指定元素最后一次出现的下标值,查找失败返回-1。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| element | T | 是 | 指定元素。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 返回指定元素最后一次出现的下标值,没有找到返回-1。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getLastIndexOf method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.getLastIndexOf(2);
```
### getIndexOf
getIndexOf(element: T): number
查找指定元素第一次出现的下标值,查找失败返回-1。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| element | T | 是 | 指定元素。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| number | 返回第一次找到指定元素的下标,没有找到返回-1。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getIndexOf method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.getIndexOf(2);
```
### equal
equal(obj: Object): boolean
比较指定对象与此List是否相等。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| obj | Object | 是 | 用来比较的对象。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 如果对象与此列表相同返回true,否则返回false。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The equal method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
let obj: List = new List();
obj.add(2);
obj.add(4);
obj.add(5);
let result = list.equal(obj);
```
### removeByIndex
removeByIndex(index: number): T
根据元素的下标值查找元素,并将其删除。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 指定元素的下标值。需要小于等于int32_max即2147483647。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| T | 返回被删除的元素。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200001 | The value of index is out of range. |
| 10200011 | The removeByIndex method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(4);
let result = list.removeByIndex(2);
```
### remove
remove(element: T): boolean
删除查找到的第一个指定的元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| element | T | 是 | 指定元素。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 删除成功返回true,否则返回false。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.remove(2);
```
### replaceAllElements
replaceAllElements(callbackFn: (value: T, index?: number, list?: List<T>) => T,
thisArg?: Object): void
用户操作List中的元素,用操作后的元素替换原元素并返回操作后的元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callbackFn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 当前遍历到的元素。 |
| index | number | 否 | 当前遍历到的下标值,默认值为0。 |
| list | List<T> | 否 | 当前调用replaceAllElements方法的实例对象,默认值为当前实例对象。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200011 | The replaceAllElements method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.replaceAllElements((value: number) => {
// 用户操作逻辑根据实际场景进行添加。
return value;
});
```
### forEach
forEach(callbackFn: (value: T, index?: number, List?: List<T>) => void,
thisArg?: Object): void
通过回调函数来遍历List实例对象上的元素以及元素对应的下标。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| callbackFn | function | 是 | 回调函数。 |
| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
callbackfn的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| value | T | 是 | 当前遍历到的元素。 |
| index | number | 否 | 当前遍历到的下标值,默认值为0。 |
| List | List<T> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200011 | The forEach method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.forEach((value: number, index?: number) => {
console.log("value:" + value, "index:" + index);
});
```
### sort
sort(comparator: (firstValue: T, secondValue: T) => number): void
对List中的元素进行一个排序操作。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| comparator | function | 是 | 回调函数。 |
comparator的参数说明:
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| firstValue | T | 是 | 前一项元素。 |
| secondValue | T | 是 | 后一项元素。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200011 | The sort method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.sort((a: number, b: number) => a - b); // 结果为升序排列
list.sort((a: number, b: number) => b - a); // 结果为降序排列
```
### getSubList
getSubList(fromIndex: number, toIndex: number): List<T>
根据下标截取List中的一段元素,并返回这一段List实例,包括起始值但不包括终止值。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| fromIndex | number | 是 | 起始下标。 |
| toIndex | number | 是 | 终止下标。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| List<T> | 返回List对象实例。 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200001 | The value of fromIndex or toIndex is out of range. |
| 10200011 | The getSubList method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getSubList(1, 3);
```
### clear
clear(): void
清除List中的所有元素,并把length置为0。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.clear();
```
### set
set(index: number, element: T): T
将此 List 中指定位置的元素替换为指定元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 查找的下标值。需要小于等于int32_max即2147483647。 |
| element | T | 是 | 用来替换的元素。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| T | 返回替换后的元素 |
**错误码:**
以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200001 | The value of index is out of range. |
| 10200011 | The set method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.set(2, "b");
```
### convertToArray
convertToArray(): Array<T>
把当前List实例转换成数组,并返回转换后的数组。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Array<T> | 返回转换后的数组。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The convertToArray method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.convertToArray();
```
### isEmpty
isEmpty(): boolean
判断该List是否为空。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| boolean | 为空返回true,不为空返回false。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.isEmpty();
```
### getFirst
getFirst(): T
获取List实例中的第一个元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| T | 返回实例的第一个元素。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getFirst method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getFirst();
```
### getLast
getLast(): T
获取List实例中的最后一个元素。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| T | 返回实例的最后一个元素。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The getLast method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getLast();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator<T>
返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
**系统能力:** SystemCapability.Utils.Lang
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| IterableIterator<T> | 返回一个迭代器。 |
**错误码:**
以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
| 错误码ID | 错误信息 |
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |
**示例:**
```ts
let list: List = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
// 使用方法一:
let items = Array.from(list)
for (let item of items) {
console.log("value: " + item);
}
// 使用方法二:
let iter = list[Symbol.iterator]();
let temp: IteratorResult = iter.next();
while(!temp.done) {
console.log("value: " + temp.value);
temp = iter.next();
}
```