1# @ohos.util.LinkedList (线性容器LinkedList)
2
3LinkedList底层通过双向链表实现,双向链表的每个节点都包含对前一个元素和后一个元素的引用。当需要查询元素时,可以从头遍历,也可以从尾部遍历,插入、删除效率高,查询效率低。LinkedList允许元素为null。
4
5LinkedList和[List](js-apis-list.md)相比,LinkedList是双向链表,可以快速地在头尾进行增删,而List是单向链表,无法双向操作。
6
7LinkedList和[ArrayList](js-apis-arraylist.md)相比,插入数据效率LinkedList优于ArrayList,而查询效率ArrayList优于LinkedList。
8
9> **注意:**
10>
11> 在LinkedList中使用\[index\]的方式虽然能够获取对应位置的元素,但这会导致未定义结果。推荐使用get()方法。
12
13**推荐使用场景:** 当需要频繁的插入删除元素,并且需要使用双向链表时,推荐使用LinkedList高效操作。
14
15文档中存在泛型的使用,涉及以下泛型标记符:<br>
16- T: Type,类
17
18> **说明:**
19>
20> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
21
22
23## 导入模块
24
25```ts
26import { LinkedList } from '@kit.ArkTS';
27```
28
29## LinkedList
30
31### 属性
32
33**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
34
35**系统能力:** SystemCapability.Utils.Lang
36
37| 名称 | 类型 | 可读 | 可写 | 说明 |
38| -------- | -------- | -------- | -------- | -------- |
39| length | number | 是 | 否 | LinkedList的元素个数。 |
40
41
42### constructor
43
44constructor()
45
46LinkedList的构造函数。
47
48**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
49
50**系统能力:** SystemCapability.Utils.Lang
51
52**错误码:**
53
54以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
55
56| 错误码ID | 错误信息 |
57| -------- | -------- |
58| 10200012 | The LinkedList's constructor cannot be directly invoked. |
59
60
61**示例:**
62
63```ts
64let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
65```
66
67
68### add
69
70add(element: T): boolean
71
72在LinkedList尾部插入元素。
73
74**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
75
76**系统能力:** SystemCapability.Utils.Lang
77
78**参数:**
79
80| 参数名 | 类型 | 必填 | 说明 |
81| -------- | -------- | -------- | -------- |
82| element | T | 是 | 待插入的元素。 |
83
84**返回值:**
85
86| 类型 | 说明 |
87| -------- | -------- |
88| boolean | 插入成功返回true,否则返回false。 |
89
90**错误码:**
91
92以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
93
94| 错误码ID | 错误信息 |
95| -------- | -------- |
96| 10200011 | The add method cannot be bound. |
97
98**示例:**
99
100```ts
101let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
102let result = linkedList.add("a");
103let result1 = linkedList.add(1);
104let b = [1, 2, 3];
105let result2 = linkedList.add(b);
106class C {
107  name: string = ''
108  age: string = ''
109}
110let c: C = {name : "Dylan", age : "13"};
111let result3 = linkedList.add(c);
112let result4 = linkedList.add(false);
113```
114
115### addFirst
116
117addFirst(element: T): void
118
119在LinkedList头部插入元素。
120
121**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
122
123**系统能力:** SystemCapability.Utils.Lang
124
125**参数:**
126
127| 参数名 | 类型 | 必填 | 说明 |
128| -------- | -------- | -------- | -------- |
129| element | T | 是 | 待插入的元素。 |
130
131**错误码:**
132
133以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
134
135| 错误码ID | 错误信息 |
136| -------- | -------- |
137| 10200011 | The addFirst method cannot be bound. |
138
139**示例:**
140
141```ts
142let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
143linkedList.addFirst("a");
144linkedList.addFirst(1);
145let b = [1, 2, 3];
146linkedList.addFirst(b);
147class C {
148  name: string = ''
149  age: string = ''
150}
151let c: C = {name : "Dylan", age : "13"};
152linkedList.addFirst(c);
153linkedList.addFirst(false);
154```
155
156### insert
157
158insert(index: number, element: T): void
159
160在长度范围内任意插入指定元素。
161
162**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
163
164**系统能力:** SystemCapability.Utils.Lang
165
166**参数:**
167
168| 参数名 | 类型 | 必填 | 说明 |
169| -------- | -------- | -------- | -------- |
170| index | number | 是 | 插入位置索引。需要小于等于int32_max即2147483647。 |
171| element | T | 是 | 插入元素。 |
172
173**错误码:**
174
175以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
176
177| 错误码ID | 错误信息 |
178| -------- | -------- |
179| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
180| 10200001 | The value of index is out of range. |
181| 10200011 | The insert method cannot be bound. |
182
183**示例:**
184
185```ts
186let linkedList: LinkedList<string | number | boolean | object> = new LinkedList();
187linkedList.insert(0, "A");
188linkedList.insert(1, 0);
189linkedList.insert(2, true);
190```
191
192### has
193
194has(element: T): boolean
195
196判断此LinkedList中是否含有该指定元素。
197
198**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
199
200**系统能力:** SystemCapability.Utils.Lang
201
202**参数:**
203
204| 参数名 | 类型 | 必填 | 说明 |
205| -------- | -------- | -------- | -------- |
206| element | T | 是 | 指定元素。 |
207
208**返回值:**
209
210| 类型 | 说明 |
211| -------- | -------- |
212| boolean | 包含指定元素返回true,否则返回false。 |
213
214**错误码:**
215
216以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
217
218| 错误码ID | 错误信息 |
219| -------- | -------- |
220| 10200011 | The has method cannot be bound. |
221
222**示例:**
223
224```ts
225let linkedList: LinkedList<string> = new LinkedList();
226linkedList.add("squirrel");
227let result = linkedList.has("squirrel");
228```
229
230### get
231
232get(index: number): T
233
234根据下标获取LinkedList中的元素。
235
236**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
237
238**系统能力:** SystemCapability.Utils.Lang
239
240**参数:**
241
242| 参数名 | 类型 | 必填 | 说明 |
243| -------- | -------- | -------- | -------- |
244| index | number | 是 | 指定的下标值。需要小于等于int32_max即2147483647。 |
245
246**返回值:**
247
248| 类型 | 说明 |
249| -------- | -------- |
250| T | 根据下标查找到的元素,元素不存在返回undefined。 |
251
252**错误码:**
253
254以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
255
256| 错误码ID | 错误信息 |
257| -------- | -------- |
258| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
259| 10200011 | The get method cannot be bound. |
260
261**示例:**
262
263```ts
264let linkedList: LinkedList<number> = new LinkedList();
265linkedList.add(2);
266linkedList.add(4);
267linkedList.add(5);
268linkedList.add(2);
269linkedList.add(1);
270linkedList.add(2);
271linkedList.add(4);
272let result = linkedList.get(2);
273```
274
275### getLastIndexOf
276
277getLastIndexOf(element: T): number
278
279返回指定元素最后一次出现时的下标值,查找失败返回-1。
280
281**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
282
283**系统能力:** SystemCapability.Utils.Lang
284
285**参数:**
286
287| 参数名 | 类型 | 必填 | 说明 |
288| -------- | -------- | -------- | -------- |
289| element | T | 是 | 指定元素。 |
290
291**返回值:**
292
293| 类型 | 说明 |
294| -------- | -------- |
295| number | 返回指定元素最后一次出现时的下标值,查找失败返回-1。 |
296
297**错误码:**
298
299以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
300
301| 错误码ID | 错误信息 |
302| -------- | -------- |
303| 10200011 | The getLastIndexOf method cannot be bound. |
304
305**示例:**
306
307```ts
308let linkedList: LinkedList<number> = new LinkedList();
309linkedList.add(2);
310linkedList.add(4);
311linkedList.add(5);
312linkedList.add(2);
313linkedList.add(1);
314linkedList.add(2);
315linkedList.add(4);
316let result = linkedList.getLastIndexOf(2);
317```
318
319### getIndexOf
320
321getIndexOf(element: T): number
322
323返回指定元素第一次出现时的下标值,查找失败返回-1。
324
325**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
326
327**系统能力:** SystemCapability.Utils.Lang
328
329**参数:**
330
331| 参数名 | 类型 | 必填 | 说明 |
332| -------- | -------- | -------- | -------- |
333| element | T | 是 | 指定元素。 |
334
335**返回值:**
336
337| 类型 | 说明 |
338| -------- | -------- |
339| number | 返回指定元素第一次出现时的下标值,查找失败返回-1。 |
340
341**错误码:**
342
343以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
344
345| 错误码ID | 错误信息 |
346| -------- | -------- |
347| 10200011 | The getIndexOf method cannot be bound. |
348
349**示例:**
350
351```ts
352let linkedList: LinkedList<number> = new LinkedList();
353linkedList.add(2);
354linkedList.add(4);
355linkedList.add(5);
356linkedList.add(2);
357linkedList.add(1);
358linkedList.add(2);
359linkedList.add(4);
360let result = linkedList.getIndexOf(2);
361```
362
363### removeByIndex
364
365removeByIndex(index: number): T
366
367根据元素的下标值查找元素,并将其删除。
368
369**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
370
371**系统能力:** SystemCapability.Utils.Lang
372
373**参数:**
374
375| 参数名 | 类型 | 必填 | 说明 |
376| -------- | -------- | -------- | -------- |
377| index | number | 是 | 指定元素的下标值。需要小于等于int32_max即2147483647。 |
378
379**返回值:**
380
381| 类型 | 说明 |
382| -------- | -------- |
383| T | 返回删除的元素,如果为空返回undefined。 |
384
385**错误码:**
386
387以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
388
389| 错误码ID | 错误信息 |
390| -------- | -------- |
391| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
392| 10200001 | The value of index is out of range. |
393| 10200011 | The removeByIndex method cannot be bound. |
394
395**示例:**
396
397```ts
398let linkedList: LinkedList<number> = new LinkedList();
399linkedList.add(2);
400linkedList.add(4);
401linkedList.add(5);
402linkedList.add(2);
403linkedList.add(4);
404let result = linkedList.removeByIndex(2);
405```
406
407### removeFirst
408
409removeFirst(): T
410
411删除并返回LinkedList的第一个元素。
412
413**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
414
415**系统能力:** SystemCapability.Utils.Lang
416
417**返回值:**
418
419| 类型 | 说明 |
420| -------- | -------- |
421| T | 返回删除的元素。 |
422
423**错误码:**
424
425以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
426
427| 错误码ID | 错误信息 |
428| -------- | -------- |
429| 10200010 | Container is empty. |
430| 10200011 | The removeFirst method cannot be bound. |
431
432**示例:**
433
434```ts
435let linkedList: LinkedList<number> = new LinkedList();
436linkedList.add(2);
437linkedList.add(4);
438linkedList.add(5);
439linkedList.add(2);
440linkedList.add(4);
441let result = linkedList.removeFirst();
442```
443
444### removeLast
445
446removeLast(): T
447
448删除并返回LinkedList的最后一个元素。
449
450**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
451
452**系统能力:** SystemCapability.Utils.Lang
453
454**返回值:**
455
456| 类型 | 说明 |
457| -------- | -------- |
458| T | 返回删除的元素。 |
459
460**错误码:**
461
462以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
463
464| 错误码ID | 错误信息 |
465| -------- | -------- |
466| 10200010 | Container is empty. |
467| 10200011 | The removeLast method cannot be bound. |
468
469**示例:**
470
471```ts
472let linkedList: LinkedList<number> = new LinkedList();
473linkedList.add(2);
474linkedList.add(4);
475linkedList.add(5);
476linkedList.add(2);
477linkedList.add(4);
478let result = linkedList.removeLast();
479```
480
481### remove
482
483remove(element: T): boolean
484
485删除查找到的第一个指定的元素。
486
487**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
488
489**系统能力:** SystemCapability.Utils.Lang
490
491**参数:**
492
493| 参数名 | 类型 | 必填 | 说明 |
494| -------- | -------- | -------- | -------- |
495| element | T | 是 | 指定元素。 |
496
497**返回值:**
498
499| 类型 | 说明 |
500| -------- | -------- |
501| boolean | 删除成功返回true,否则返回false。 |
502
503**错误码:**
504
505以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
506
507| 错误码ID | 错误信息 |
508| -------- | -------- |
509| 10200011 | The remove method cannot be bound. |
510
511**示例:**
512
513```ts
514let linkedList: LinkedList<number> = new LinkedList();
515linkedList.add(2);
516linkedList.add(4);
517linkedList.add(5);
518linkedList.add(4);
519let result = linkedList.remove(2);
520```
521
522### removeFirstFound
523
524removeFirstFound(element: T): boolean
525
526删除第一次出现的指定元素。
527
528**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
529
530**系统能力:** SystemCapability.Utils.Lang
531
532**参数:**
533
534| 参数名 | 类型 | 必填 | 说明 |
535| -------- | -------- | -------- | -------- |
536| element | T | 是 | 指定元素。 |
537
538**返回值:**
539
540| 类型 | 说明 |
541| -------- | -------- |
542| boolean | 删除成功返回true,删除失败或不存在该元素时返回false。 |
543
544**错误码:**
545
546以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
547
548| 错误码ID | 错误信息 |
549| -------- | -------- |
550| 10200010 | Container is empty. |
551| 10200011 | The removeFirstFound method cannot be bound. |
552| 10200017 | The element does not exist in this container. |
553
554**示例:**
555
556```ts
557let linkedList: LinkedList<number> = new LinkedList();
558linkedList.add(2);
559linkedList.add(4);
560linkedList.add(5);
561linkedList.add(4);
562let result = linkedList.removeFirstFound(4);
563```
564
565### removeLastFound
566
567removeLastFound(element: T): boolean
568
569删除最后一次出现的指定元素。
570
571**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
572
573**系统能力:** SystemCapability.Utils.Lang
574
575**参数:**
576
577| 参数名 | 类型 | 必填 | 说明 |
578| -------- | -------- | -------- | -------- |
579| element | T | 是 | 指定元素。 |
580
581**返回值:**
582
583| 类型 | 说明 |
584| -------- | -------- |
585| boolean | 删除成功返回true,删除失败或不存在该元素时返回false。 |
586
587**错误码:**
588
589以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
590
591| 错误码ID | 错误信息 |
592| -------- | -------- |
593| 10200010 | Container is empty. |
594| 10200011 | The removeLastFound method cannot be bound. |
595| 10200017 | The element does not exist in this container. |
596
597**示例:**
598
599```ts
600let linkedList: LinkedList<number> = new LinkedList();
601linkedList.add(2);
602linkedList.add(4);
603linkedList.add(5);
604linkedList.add(4);
605let result = linkedList.removeLastFound(4);
606```
607
608### clone
609
610clone(): LinkedList&lt;T&gt;
611
612克隆一个与LinkedList相同的实例,并返回克隆后的实例。修改克隆后的实例并不会影响原实例。
613
614**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
615
616**系统能力:** SystemCapability.Utils.Lang
617
618**返回值:**
619
620| 类型 | 说明 |
621| -------- | -------- |
622| LinkedList&lt;T&gt; | 返回LinkedList对象实例。 |
623
624**错误码:**
625
626以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
627
628| 错误码ID | 错误信息 |
629| -------- | -------- |
630| 10200011 | The clone method cannot be bound. |
631
632**示例:**
633
634```ts
635let linkedList: LinkedList<number> = new LinkedList();
636linkedList.add(2);
637linkedList.add(4);
638linkedList.add(5);
639linkedList.add(4);
640let result = linkedList.clone();
641```
642
643### forEach
644
645forEach(callbackFn: (value: T, index?: number, LinkedList?: LinkedList&lt;T&gt;) => void,
646thisArg?: Object): void
647
648通过回调函数来遍历LinkedList实例对象上的元素以及元素对应的下标。
649
650**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
651
652**系统能力:** SystemCapability.Utils.Lang
653
654**参数:**
655
656| 参数名 | 类型 | 必填 | 说明 |
657| -------- | -------- | -------- | -------- |
658| callbackFn | function | 是 | 回调函数。 |
659| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
660
661callbackfn的参数说明:
662
663| 参数名 | 类型 | 必填 | 说明 |
664| -------- | -------- | -------- | -------- |
665| value | T | 是 | 当前遍历到的元素。 |
666| index | number | 否 | 当前遍历到的下标值,默认值为0。 |
667| LinkedList | LinkedList&lt;T&gt; | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
668
669**错误码:**
670
671以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
672
673| 错误码ID | 错误信息 |
674| -------- | -------- |
675| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
676| 10200011 | The forEach method cannot be bound. |
677
678**示例:**
679
680```ts
681let linkedList: LinkedList<number> = new LinkedList();
682linkedList.add(2);
683linkedList.add(4);
684linkedList.add(5);
685linkedList.add(4);
686linkedList.forEach((value: number, index?: number) => {
687  console.log("value:" + value, "index:" + index);
688});
689```
690
691### clear
692
693clear(): void
694
695清除LinkedList中的所有元素,并把length置为0。
696
697**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
698
699**系统能力:** SystemCapability.Utils.Lang
700
701**错误码:**
702
703以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
704
705| 错误码ID | 错误信息 |
706| -------- | -------- |
707| 10200011 | The clear method cannot be bound. |
708
709**示例:**
710
711```ts
712let linkedList: LinkedList<number> = new LinkedList();
713linkedList.add(2);
714linkedList.add(4);
715linkedList.add(5);
716linkedList.add(4);
717linkedList.clear();
718```
719
720### set
721
722set(index: number, element: T): T
723
724将此LinkedList中指定位置的元素替换为指定元素。
725
726**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
727
728**系统能力:** SystemCapability.Utils.Lang
729
730**参数:**
731
732| 参数名 | 类型 | 必填 | 说明 |
733| -------- | -------- | -------- | -------- |
734| index | number | 是 | 查找的下标值。需要小于等于int32_max即2147483647。 |
735| element | T | 是 | 用来替换的元素。 |
736
737**返回值:**
738
739| 类型 | 说明 |
740| -------- | -------- |
741| T | 返回替换后的元素,如果为空返回undefined。 |
742
743**错误码:**
744
745以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
746
747| 错误码ID | 错误信息 |
748| -------- | -------- |
749| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
750| 10200001 | The value of index is out of range. |
751| 10200011 | The set method cannot be bound. |
752
753**示例:**
754
755```ts
756let linkedList: LinkedList<number | string> = new LinkedList();
757linkedList.add(2);
758linkedList.add(4);
759linkedList.add(5);
760linkedList.add(4);
761let result = linkedList.set(2, "b");
762```
763
764### convertToArray
765
766convertToArray(): Array&lt;T&gt;
767
768把当前LinkedList实例转换成数组,并返回转换后的数组。
769
770**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
771
772**系统能力:** SystemCapability.Utils.Lang
773
774**返回值:**
775
776| 类型 | 说明 |
777| -------- | -------- |
778| Array&lt;T&gt; | 返回转换后的数组。 |
779
780**错误码:**
781
782以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
783
784| 错误码ID | 错误信息 |
785| -------- | -------- |
786| 10200011 | The convertToArray method cannot be bound. |
787
788**示例:**
789```ts
790let linkedList: LinkedList<number> = new LinkedList();
791linkedList.add(2);
792linkedList.add(4);
793linkedList.add(5);
794linkedList.add(4);
795let result = linkedList.convertToArray();
796```
797
798### getFirst
799
800getFirst(): T
801
802获取LinkedList实例中的第一个元素。
803
804**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
805
806**系统能力:** SystemCapability.Utils.Lang
807
808**返回值:**
809
810| 类型 | 说明 |
811| -------- | -------- |
812| T | 返回对应元素,如果元素为空返回undefined。 |
813
814**错误码:**
815
816以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
817
818| 错误码ID | 错误信息 |
819| -------- | -------- |
820| 10200011 | The getFirst method cannot be bound. |
821
822**示例:**
823
824```ts
825let linkedList: LinkedList<number> = new LinkedList();
826linkedList.add(2);
827linkedList.add(4);
828linkedList.add(5);
829linkedList.add(4);
830let result = linkedList.getFirst();
831```
832
833### getLast
834
835getLast(): T
836
837获取LinkedList实例中的最后一个元素。
838
839**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
840
841**系统能力:** SystemCapability.Utils.Lang
842
843**返回值:**
844
845| 类型 | 说明 |
846| -------- | -------- |
847| T | 返回对应元素,如果为元素空返回undefined。 |
848
849**错误码:**
850
851以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
852
853| 错误码ID | 错误信息 |
854| -------- | -------- |
855| 10200011 | The getLast method cannot be bound. |
856
857**示例:**
858
859```ts
860let linkedList: LinkedList<number> = new LinkedList();
861linkedList.add(2);
862linkedList.add(4);
863linkedList.add(5);
864linkedList.add(4);
865let result = linkedList.getLast();
866```
867
868### [Symbol.iterator]
869
870[Symbol.iterator]\(): IterableIterator&lt;T&gt;
871
872返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
873
874**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
875
876**系统能力:** SystemCapability.Utils.Lang
877
878**返回值:**
879
880| 类型 | 说明 |
881| -------- | -------- |
882| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
883
884**错误码:**
885
886以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
887
888| 错误码ID | 错误信息 |
889| -------- | -------- |
890| 10200011 | The Symbol.iterator method cannot be bound. |
891
892**示例:**
893
894```ts
895let linkedList: LinkedList<number> = new LinkedList();
896linkedList.add(2);
897linkedList.add(4);
898linkedList.add(5);
899linkedList.add(4);
900
901// 使用方法一:
902let items = Array.from(linkedList)
903for (let item of items) {
904  console.log("value:" + item);
905}
906
907// 使用方法二:
908let iter = linkedList[Symbol.iterator]();
909let temp: IteratorResult<number> = iter.next();
910while(!temp.done) {
911  console.log("value:" + temp.value);
912  temp = iter.next();
913}
914```