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