LinkedList底层通过双向链表实现,每个节点都包含对前一个元素和后一个元素的引用。查询元素时,可以从头或从尾部遍历,插入和删除效率高,查询效率低。LinkedList允许元素为null。
LinkedList和List相比,LinkedList是双向链表,可以快速地在头尾进行增删,而List是单向链表,无法双向操作。
LinkedList和ArrayList相比,LinkedList插入数据效率高于ArrayList,而ArrayList查询效率高于LinkedList。
注意:
在LinkedList中使用[index]的方式获取元素可能导致未定义结果,推荐使用get()方法。
推荐使用场景: 当需要频繁的插入删除元素且需要使用双向链表时,推荐使用LinkedList。
文档中使用了泛型,涉及以下泛型标记符:
说明:
本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
import { LinkedList } from '@kit.ArkTS';
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
名称 | 类型 | 可读 | 可写 | 说明 |
---|---|---|---|---|
length | number | 是 | 否 | LinkedList的元素个数。 |
constructor()
LinkedList的构造函数。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200012 | The LinkedList's constructor cannot be directly invoked. |
示例:
let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
add(element: T): boolean
在LinkedList尾部插入元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 待插入的元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 插入成功返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The add method cannot be bound. |
示例:
let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
let result = linkedList.add("a");
let result1 = linkedList.add(1);
let b = [1, 2, 3];
let result2 = linkedList.add(b);
class C {
name: string = ''
age: string = ''
}
let c: C = {name : "Dylan", age : "13"};
let result3 = linkedList.add(c);
let result4 = linkedList.add(false);
addFirst(element: T): void
在LinkedList头部插入元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 待插入的元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The addFirst method cannot be bound. |
示例:
let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
linkedList.addFirst("a");
linkedList.addFirst(1);
let b = [1, 2, 3];
linkedList.addFirst(b);
class C {
name: string = ''
age: string = ''
}
let c: C = {name : "Dylan", age : "13"};
linkedList.addFirst(c);
linkedList.addFirst(false);
insert(index: number, element: T): void
在长度范围内任意位置插入指定元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
index | number | 是 | 插入位置索引。需要小于等于int32_max即2147483647。 |
element | T | 是 | 插入元素。 |
错误码:
错误码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. |
示例:
let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
linkedList.insert(0, "A");
linkedList.insert(1, 0);
linkedList.insert(2, true);
has(element: T): boolean
判断LinkedList中是否包含指定元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 包含指定元素返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The has method cannot be bound. |
示例:
let linkedList: LinkedList<string> = new LinkedList();
linkedList.add("squirrel");
let result = linkedList.has("squirrel");
get(index: number): T
根据下标获取LinkedList中的元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
index | number | 是 | 指定的下标值。需要小于等于int32_max即2147483647。 |
返回值:
类型 | 说明 |
---|---|
T | 根据下标查找到的元素,元素不存在返回undefined。 |
错误码:
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The get method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.get(2);
getLastIndexOf(element: T): number
查找指定元素最后一次出现时的下标值,查找失败返回-1。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
number | 返回指定元素最后一次出现时的下标值,查找失败返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The getLastIndexOf method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.getLastIndexOf(2);
getIndexOf(element: T): number
查找指定元素第一次出现时的下标值,查找失败返回-1。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
number | 返回指定元素第一次出现时的下标值,查找失败返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The getIndexOf method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.getIndexOf(2);
removeByIndex(index: number): T
根据元素的下标值查找元素,并将其删除。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
index | number | 是 | 指定元素的下标值。需要小于等于int32_max即2147483647。 |
返回值:
类型 | 说明 |
---|---|
T | 返回删除的元素,如果元素为空返回undefined。 |
错误码:
错误码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 removeByIndex method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeByIndex(2);
removeFirst(): T
删除并返回LinkedList的第一个元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
T | 返回删除的元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200010 | Container is empty. |
10200011 | The removeFirst method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeFirst();
removeLast(): T
删除并返回LinkedList的最后一个元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
T | 返回删除的元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200010 | Container is empty. |
10200011 | The removeLast method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeLast();
remove(element: T): boolean
删除查找到的第一个指定元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 删除成功返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The remove method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.remove(2);
removeFirstFound(element: T): boolean
删除第一次出现的指定元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 删除成功返回true,删除失败或不存在该元素时返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200010 | Container is empty. |
10200011 | The removeFirstFound method cannot be bound. |
10200017 | The element does not exist in this container. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.removeFirstFound(4);
removeLastFound(element: T): boolean
删除最后一次出现的指定元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
类型 | 说明 |
---|---|
boolean | 删除成功返回true,删除失败或不存在该元素时返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200010 | Container is empty. |
10200011 | The removeLastFound method cannot be bound. |
10200017 | The element does not exist in this container. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.removeLastFound(4);
clone(): LinkedList<T>
克隆一个与LinkedList相同的实例并返回。修改克隆后的实例并不会影响原实例。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
LinkedList<T> | 返回LinkedList对象实例。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The clone method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.clone();
forEach(callbackFn: (value: T, index?: number, LinkedList?: LinkedList<T>) => void, thisArg?: Object): void
通过回调函数来遍历LinkedList实例对象上的元素以及其下标。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
callbackFn | function | 是 | 回调函数。 |
thisArg | Object | 否 | callbackFn被调用时用作this值,默认值为当前实例对象。 |
callbackFn的参数说明:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | T | 是 | 当前遍历到的元素。 |
index | number | 否 | 当前遍历到的下标值,默认值为0。 |
LinkedList | LinkedList<T> | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
错误码:
错误码ID | 错误信息 |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The forEach method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.forEach((value: number, index?: number) => {
console.log("value:" + value, "index:" + index);
});
clear(): void
清除LinkedList中的所有元素,并将length置为0。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The clear method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.clear();
set(index: number, element: T): T
替换LinkedList指定位置的元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
index | number | 是 | 查找的下标值。需要小于等于int32_max即2147483647。 |
element | T | 是 | 用来替换的元素。 |
返回值:
类型 | 说明 |
---|---|
T | 返回替换后的元素,如果元素为空则返回undefined。 |
错误码:
错误码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 set method cannot be bound. |
示例:
let linkedList: LinkedList<number | string> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.set(2, "b");
convertToArray(): Array<T>
将当前LinkedList实例转换成数组并返回。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
Array<T> | 返回转换后的数组。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The convertToArray method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.convertToArray();
getFirst(): T
获取LinkedList实例中的第一个元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
T | 返回对应元素,若元素为空则返回undefined。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The getFirst method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.getFirst();
getLast(): T
获取LinkedList实例中的最后一个元素。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
T | 返回对应元素,若元素为空则返回undefined。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The getLast method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.getLast();
[Symbol.iterator](): IterableIterator<T>
返回一个迭代器,迭代器的每一项都是一个JavaScript对象。
原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
类型 | 说明 |
---|---|
IterableIterator<T> | 返回一个迭代器。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
错误码ID | 错误信息 |
---|---|
10200011 | The Symbol.iterator method cannot be bound. |
示例:
let linkedList: LinkedList<number> = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
// 使用方法一:
let items = Array.from(linkedList);
for (let item of items) {
console.log("value:" + item);
}
// 使用方法二:
let iter = linkedList[Symbol.iterator]();
let temp: IteratorResult<number> = iter.next();
while(!temp.done) {
console.log("value:" + temp.value);
temp = iter.next();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。