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