js-apis-linkedlist.md 18.6 KB
Newer Older
1
# @ohos.util.LinkedList (Linear Container LinkedList)
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4 5 6
**LinkedList** is implemented based on the doubly linked list. Each node of the doubly linked list has references pointing to the previous element and the next element. When querying an element, the system traverses the list from the beginning or end. **LinkedList** offers efficient insertion and removal operations but supports low query efficiency. **LinkedList** allows null elements.

Unlike **[List](js-apis-list.md)**, which is a singly linked list, **LinkedList** is a doubly linked list that supports insertion and removal at both ends.

7
**LinkedList** is more efficient in data insertion than **[ArrayList](js-apis-arraylist.md)**, but less efficient in data access.
W
wusongqing 已提交
8 9

**Recommended use case**: Use **LinkedList** for frequent insertion and removal operations.
W
wusongqing 已提交
10

G
Gloria 已提交
11 12 13
This topic uses the following to identify the use of generics:
- T: Type

G
Gloria 已提交
14 15 16 17 18
> **NOTE**
>
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.


W
wusongqing 已提交
19 20
## Modules to Import

W
wusongqing 已提交
21
```ts
W
wusongqing 已提交
22
import LinkedList from '@ohos.util.LinkedList';  
W
wusongqing 已提交
23 24 25 26 27 28
```

## LinkedList

### Attributes

W
wusongqing 已提交
29 30
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
31 32
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
33
| length | number | Yes| No| Number of elements in a linked list (called container later).|
W
wusongqing 已提交
34 35 36 37


### constructor

W
wusongqing 已提交
38
constructor()
W
wusongqing 已提交
39 40 41

A constructor used to create a **LinkedList** instance.

W
wusongqing 已提交
42 43
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
44 45
**Error codes**

46
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
47 48 49 50 51

| ID| Error Message|
| -------- | -------- |
| 10200012 | The LinkedList's constructor cannot be directly invoked. |

W
wusongqing 已提交
52 53 54

**Example**

W
wusongqing 已提交
55
```ts
W
wusongqing 已提交
56 57 58 59 60 61 62 63
let linkedList = new LinkedList();
```


### add

add(element: T): boolean

W
wusongqing 已提交
64 65 66
Adds an element at the end of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
67 68 69 70 71

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
72
| element | T | Yes| Target element.|
W
wusongqing 已提交
73 74 75 76 77

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
78
| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
W
wusongqing 已提交
79

G
Gloria 已提交
80 81
**Error codes**

82
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
83 84 85 86 87

| ID| Error Message|
| -------- | -------- |
| 10200011 | The add method cannot be bound. |

W
wusongqing 已提交
88 89
**Example**

W
wusongqing 已提交
90
```ts
W
wusongqing 已提交
91 92 93 94
let linkedList = new LinkedList();
let result = linkedList.add("a");
let result1 = linkedList.add(1);
let b = [1, 2, 3];
G
Gloria 已提交
95
let result2 = linkedList.add(b);
G
Gloria 已提交
96
let c = {name : "Dylon", age : "13"};
G
Gloria 已提交
97 98
let result3 = linkedList.add(c);
let result4 = linkedList.add(false);
W
wusongqing 已提交
99 100 101 102 103 104
```

### addFirst

addFirst(element: T): void

W
wusongqing 已提交
105 106 107
Adds an element at the top of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
108 109 110 111 112

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
113
| element | T | Yes| Target element.|
W
wusongqing 已提交
114

G
Gloria 已提交
115 116
**Error codes**

117
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
118 119 120 121 122

| ID| Error Message|
| -------- | -------- |
| 10200011 | The addFirst method cannot be bound. |

W
wusongqing 已提交
123 124
**Example**

W
wusongqing 已提交
125
```ts
W
wusongqing 已提交
126 127 128 129 130
let linkedList = new LinkedList();
linkedList.addFirst("a");
linkedList.addFirst(1);
let b = [1, 2, 3];
linkedList.addFirst(b);
G
Gloria 已提交
131
let c = {name : "Dylon", age : "13"};
G
Gloria 已提交
132
linkedList.addFirst(c);
W
wusongqing 已提交
133 134 135 136 137 138 139
linkedList.addFirst(false);
```

### insert

insert(index: number, element: T): void

W
wusongqing 已提交
140 141 142
Inserts an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
143 144 145 146 147

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
148 149
| element | T | Yes| Target element.|
| index | number | Yes| Index of the position where the element is to be inserted.|
W
wusongqing 已提交
150

G
Gloria 已提交
151 152
**Error codes**

153
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
154 155 156 157

| ID| Error Message|
| -------- | -------- |
| 10200011 | The insert method cannot be bound. |
158
| 10200001 | The value of index is out of range. |
G
Gloria 已提交
159

W
wusongqing 已提交
160 161
**Example**

W
wusongqing 已提交
162
```ts
W
wusongqing 已提交
163 164 165 166 167 168 169 170 171 172
let linkedList = new LinkedList();
linkedList.insert(0, "A");
linkedList.insert(1, 0);
linkedList.insert(2, true);
```

### has

has(element: T): boolean

W
wusongqing 已提交
173 174 175
Checks whether this container has the specified element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
176 177 178 179 180

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
181
| element | T | Yes| Target element.|
W
wusongqing 已提交
182 183 184 185 186

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
187
| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
W
wusongqing 已提交
188

G
Gloria 已提交
189 190
**Error codes**

191
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
192 193 194 195 196

| ID| Error Message|
| -------- | -------- |
| 10200011 | The has method cannot be bound. |

W
wusongqing 已提交
197 198
**Example**

W
wusongqing 已提交
199
```ts
W
wusongqing 已提交
200
let linkedList = new LinkedList();
G
Gloria 已提交
201 202 203
let result1 = linkedList.has("squirrel");
linkedList.add("squirrel");
let result = linkedList.has("squirrel");
W
wusongqing 已提交
204 205 206 207 208 209
```

### get

get(index: number): T

W
wusongqing 已提交
210 211 212
Obtains an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
213 214 215 216 217

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
218
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
219 220 221 222 223

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
224
| T | Element obtained.|
W
wusongqing 已提交
225

G
Gloria 已提交
226 227
**Error codes**

228
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
229 230 231 232 233

| ID| Error Message|
| -------- | -------- |
| 10200011 | The get method cannot be bound. |

W
wusongqing 已提交
234 235
**Example**

W
wusongqing 已提交
236
```ts
W
wusongqing 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249
let linkedList = 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

W
wusongqing 已提交
250
getLastIndexOf(element: T): number
W
wusongqing 已提交
251

W
wusongqing 已提交
252 253 254
Obtains the index of the last occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
255 256 257 258 259

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
260
| element | T | Yes| Target element.|
W
wusongqing 已提交
261 262 263 264 265

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
266
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
267

G
Gloria 已提交
268 269
**Error codes**

270
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
271 272 273 274 275

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getLastIndexOf method cannot be bound. |

W
wusongqing 已提交
276 277
**Example**

W
wusongqing 已提交
278
```ts
W
wusongqing 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
let linkedList = 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

getIndexOf(element: T): number

W
wusongqing 已提交
294 295 296
Obtains the index of the first occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
297 298 299 300 301

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
302
| element | T | Yes| Target element.|
W
wusongqing 已提交
303 304 305 306 307

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
308
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
309

G
Gloria 已提交
310 311
**Error codes**

312
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
313 314 315 316 317

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getIndexOf method cannot be bound. |

W
wusongqing 已提交
318 319
**Example**

W
wusongqing 已提交
320
```ts
W
wusongqing 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
let linkedList = 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

removeByIndex(index: number): T

W
wusongqing 已提交
336 337 338
Removes an element at the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
339 340 341 342 343

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
344
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
345 346 347 348 349

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
350
| T | Element removed.|
W
wusongqing 已提交
351

G
Gloria 已提交
352 353
**Error codes**

354
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
355 356 357 358

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeByIndex method cannot be bound. |
359
| 10200001 | The value of index is out of range. |
G
Gloria 已提交
360

W
wusongqing 已提交
361 362
**Example**

W
wusongqing 已提交
363
```ts
W
wusongqing 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeByIndex(2);
```

### removeFirst

removeFirst(): T

W
wusongqing 已提交
377 378 379
Removes the first element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
380 381 382 383 384

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
385
| T | Element removed.|
W
wusongqing 已提交
386

G
Gloria 已提交
387 388
**Error codes**

389
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
390 391 392 393

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeFirst method cannot be bound. |
394
| 10200010 | Container is empty. |
G
Gloria 已提交
395

W
wusongqing 已提交
396 397
**Example**

W
wusongqing 已提交
398
```ts
W
wusongqing 已提交
399 400 401 402 403 404 405 406 407 408 409
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeFirst();
```

### removeLast

W
wusongqing 已提交
410
removeLast(): T
W
wusongqing 已提交
411

W
wusongqing 已提交
412 413 414
Removes the last element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
415 416 417 418 419

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
420
| T | Element removed.|
W
wusongqing 已提交
421

G
Gloria 已提交
422 423
**Error codes**

424
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
425 426 427 428

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeLast method cannot be bound. |
429
| 10200010 | Container is empty. |
G
Gloria 已提交
430

W
wusongqing 已提交
431 432
**Example**

W
wusongqing 已提交
433
```ts
W
wusongqing 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeLast();
```

### remove

remove(element: T): boolean

W
wusongqing 已提交
447 448 449
Removes the first occurrence of the specified element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
450 451 452 453 454

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
455
| element | T | Yes| Target element.|
W
wusongqing 已提交
456 457 458 459 460

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
461
| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
W
wusongqing 已提交
462

G
Gloria 已提交
463 464
**Error codes**

465
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
466 467 468 469 470

| ID| Error Message|
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

W
wusongqing 已提交
471 472
**Example**

W
wusongqing 已提交
473
```ts
W
wusongqing 已提交
474 475 476 477 478 479 480 481 482 483 484 485
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.remove(2);
```

### removeFirstFound

removeFirstFound(element: T): boolean

W
wusongqing 已提交
486 487 488
Removes the first occurrence of the specified element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
489 490 491 492 493

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
494
| element | T | Yes| Target element.|
W
wusongqing 已提交
495 496 497 498 499

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
500
| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
W
wusongqing 已提交
501

G
Gloria 已提交
502 503
**Error codes**

504
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
505 506 507 508

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeFirstFound method cannot be bound. |
509
| 10200010 | Container is empty. |
G
Gloria 已提交
510
| 10200017 | The element does not exist in this container. |
G
Gloria 已提交
511

W
wusongqing 已提交
512 513
**Example**

W
wusongqing 已提交
514
```ts
W
wusongqing 已提交
515 516 517 518 519 520 521 522 523 524 525 526
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.removeFirstFound(4);
```

### removeLastFound

removeLastFound(element: T): boolean

W
wusongqing 已提交
527 528 529
Removes the last occurrence of the specified element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
530 531 532 533 534

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
535
| element | T | Yes| Target element.|
W
wusongqing 已提交
536 537 538 539 540

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
541
| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
W
wusongqing 已提交
542

G
Gloria 已提交
543 544
**Error codes**

545
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
546 547 548 549

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeLastFound method cannot be bound. |
550
| 10200010 | Container is empty. |
G
Gloria 已提交
551
| 10200017 | The element does not exist in this container. |
G
Gloria 已提交
552

W
wusongqing 已提交
553 554
**Example**

W
wusongqing 已提交
555
```ts
W
wusongqing 已提交
556 557 558 559 560 561 562 563 564 565
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.removeLastFound(4);
```

### clone

W
wusongqing 已提交
566
clone(): LinkedList<T>
W
wusongqing 已提交
567

W
wusongqing 已提交
568
Clones this container and returns a copy. The modification to the copy does not affect the original instance.
W
wusongqing 已提交
569

W
wusongqing 已提交
570 571
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
572 573 574 575 576 577
**Return value**

| Type| Description|
| -------- | -------- |
| LinkedList<T> | New **LinkedList** instance obtained.|

G
Gloria 已提交
578 579
**Error codes**

580
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
581 582 583 584 585

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clone method cannot be bound. |

W
wusongqing 已提交
586 587
**Example**

W
wusongqing 已提交
588
```ts
W
wusongqing 已提交
589 590 591 592 593 594 595 596 597
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.clone();
```

### forEach
W
wusongqing 已提交
598

599
forEach(callbackFn: (value: T, index?: number, LinkedList?: LinkedList<T>) => void,
W
wusongqing 已提交
600 601
thisArg?: Object): void

W
wusongqing 已提交
602 603 604
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
605 606 607 608 609

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
610
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
611 612 613 614 615 616
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
617 618
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
W
wusongqing 已提交
619
| LinkedList | LinkedList<T> | No| Instance that invokes the **forEach** API.|
W
wusongqing 已提交
620

G
Gloria 已提交
621 622
**Error codes**

623
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
624 625 626 627 628

| ID| Error Message|
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

W
wusongqing 已提交
629 630
**Example**

W
wusongqing 已提交
631
```ts
W
wusongqing 已提交
632 633 634 635 636 637
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.forEach((value, index) => {
G
Gloria 已提交
638
    console.log("value:" + value, "index:" + index);
W
wusongqing 已提交
639 640 641 642
});
```

### clear
W
wusongqing 已提交
643

W
wusongqing 已提交
644 645 646 647
clear(): void

Clears this container and sets its length to **0**.

W
wusongqing 已提交
648 649
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
650 651
**Error codes**

652
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
653 654 655 656 657

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

W
wusongqing 已提交
658 659
**Example**

W
wusongqing 已提交
660
```ts
W
wusongqing 已提交
661 662 663 664 665 666 667 668 669
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.clear();
```

### set
W
wusongqing 已提交
670

W
wusongqing 已提交
671 672
set(index: number, element: T): T

W
wusongqing 已提交
673 674 675
Replaces an element at the specified position in this container with a given element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
676 677 678 679 680

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
681 682
| index | number | Yes| Position index of the target element.|
| element | T | Yes| Element to be used for replacement.|
W
wusongqing 已提交
683 684 685 686 687

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
688
| T | New element.|
W
wusongqing 已提交
689

G
Gloria 已提交
690 691
**Error codes**

692
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
693 694 695 696

| ID| Error Message|
| -------- | -------- |
| 10200011 | The set method cannot be bound. |
697
| 10200001 | The value of index is out of range. |
G
Gloria 已提交
698

W
wusongqing 已提交
699 700
**Example**

W
wusongqing 已提交
701
```ts
W
wusongqing 已提交
702 703 704 705 706 707 708 709 710
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.set(2, "b");
```

### convertToArray
W
wusongqing 已提交
711

W
wusongqing 已提交
712 713 714 715
convertToArray(): Array<T>

Converts this container into an array.

W
wusongqing 已提交
716 717
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
718 719 720 721 722 723
**Return value**

| Type| Description|
| -------- | -------- |
| Array<T> | Array obtained.|

G
Gloria 已提交
724 725
**Error codes**

726
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
727 728 729 730 731

| ID| Error Message|
| -------- | -------- |
| 10200011 | The convertToArray method cannot be bound. |

W
wusongqing 已提交
732
**Example**
W
wusongqing 已提交
733
```ts
W
wusongqing 已提交
734 735 736 737 738 739 740 741 742 743 744 745
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.convertToArray();
```

### getFirst

getFirst(): T

W
wusongqing 已提交
746 747 748
Obtains the first element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
749 750 751 752 753

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
754
| T | Returns the element if obtained; returns **undefined** otherwise.|
W
wusongqing 已提交
755

G
Gloria 已提交
756 757
**Error codes**

758
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
759 760 761 762 763

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getFirst method cannot be bound. |

W
wusongqing 已提交
764 765
**Example**

W
wusongqing 已提交
766
```ts
W
wusongqing 已提交
767 768 769 770 771 772 773 774 775 776 777 778
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.getFirst();
```

### getLast

getLast(): T

W
wusongqing 已提交
779 780 781
Obtains the last element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
782 783 784 785 786

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
787
| T | Returns the element if obtained; returns **undefined** otherwise.|
W
wusongqing 已提交
788

G
Gloria 已提交
789 790
**Error codes**

791
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
792 793 794 795 796

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getLast method cannot be bound. |

W
wusongqing 已提交
797 798
**Example**

W
wusongqing 已提交
799
```ts
W
wusongqing 已提交
800 801 802 803 804 805 806 807 808 809
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.getLast();
```

### [Symbol.iterator]

W
wusongqing 已提交
810
[Symbol.iterator]\(): IterableIterator<T>
W
wusongqing 已提交
811 812 813

Obtains an iterator, each item of which is a JavaScript object.

W
wusongqing 已提交
814 815
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
816 817 818 819 820 821
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<T> | Iterator obtained.|

G
Gloria 已提交
822 823
**Error codes**

824
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
825 826 827 828 829

| ID| Error Message|
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

W
wusongqing 已提交
830 831
**Example**

W
wusongqing 已提交
832
```ts
W
wusongqing 已提交
833 834 835 836 837 838 839 840
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);

// Method 1:
for (let item of linkedList) { 
W
wusongqing 已提交
841
  console.log("value:" + item); 
W
wusongqing 已提交
842 843 844 845 846 847
} 

// Method 2:
let iter = linkedList[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
848
  console.log("value:" + temp);
W
wusongqing 已提交
849 850 851
  temp = iter.next().value;
}
```