js-apis-list.md 18.1 KB
Newer Older
1
# @ohos.util.List (Linear Container List)
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4 5 6 7
**List** is implemented based on the singly linked list. Each node has a reference pointing to the next element. When querying an element, the system traverses the list from the beginning. **List** offers efficient insertion and removal operations but supports low query efficiency. **List** allows null elements.

Unlike [LinkedList](js-apis-linkedlist.md), which is a doubly linked list, **List** is a singly linked list that does not support insertion or removal at both ends.

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

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

12 13 14 15
> **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 已提交
16 17
## Modules to Import

W
wusongqing 已提交
18
```ts
19
import List from '@ohos.util.List';
W
wusongqing 已提交
20 21 22 23 24 25 26
```


## List

### Attributes

W
wusongqing 已提交
27 28
**System capability**: SystemCapability.Utils.Lang

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


### constructor

W
wusongqing 已提交
36
constructor()
W
wusongqing 已提交
37 38 39

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

W
wusongqing 已提交
40 41
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
42 43
**Error codes**

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

| ID| Error Message|
| -------- | -------- |
| 10200012 | The List's constructor cannot be directly invoked. |
W
wusongqing 已提交
49 50 51

**Example**

W
wusongqing 已提交
52
```ts
W
wusongqing 已提交
53 54 55 56 57 58 59 60
let list = new List();
```


### add

add(element: T): boolean

W
wusongqing 已提交
61 62 63
Adds an element at the end of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
64 65 66 67 68

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
69
| element | T | Yes| Target element.|
W
wusongqing 已提交
70 71 72 73 74

**Return value**

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

G
Gloria 已提交
77 78
**Error codes**

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

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

W
wusongqing 已提交
85 86
**Example**

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

### insert

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

W
wusongqing 已提交
102 103 104
Inserts an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
105 106 107 108 109

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
110 111
| element | T | Yes| Target element.|
| index | number | Yes| Index of the position where the element is to be inserted.|
W
wusongqing 已提交
112

G
Gloria 已提交
113 114
**Error codes**

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

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

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

W
wusongqing 已提交
124
```ts
W
wusongqing 已提交
125 126 127 128 129 130 131 132 133 134
let list = new List();
list.insert("A", 0);
list.insert(0, 1);
list.insert(true, 2);
```

### has

has(element: T): boolean

W
wusongqing 已提交
135 136 137
Checks whether this container has the specified element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
138 139 140 141 142

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
143
| element | T | Yes| Target element.|
W
wusongqing 已提交
144 145 146 147 148

**Return value**

| Value Type | Description|
| -------- | -------- |
W
wusongqing 已提交
149
| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
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 158

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

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

W
wusongqing 已提交
161
```ts
W
wusongqing 已提交
162
let list = new List();
G
Gloria 已提交
163 164 165
let result = list.has("squirrel");
list.add("squirrel");
let result1 = list.has("squirrel");
W
wusongqing 已提交
166 167 168 169 170 171
```

### get

get(index: number): T

W
wusongqing 已提交
172 173 174
Obtains the element at the specified position in this container.

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

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
180
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
181 182 183 184 185

**Return value**

| Value Type | Description|
| -------- | -------- |
W
wusongqing 已提交
186
| T | Element obtained.|
W
wusongqing 已提交
187

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

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

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

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

W
wusongqing 已提交
198
```ts
W
wusongqing 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.get(2);
```

### getLastIndexOf

getLastIndexOf(element: T): number

W
wusongqing 已提交
214 215 216
Obtains the index of the last occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
217 218 219 220 221

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
222
| element | T | Yes| Target element.|
W
wusongqing 已提交
223 224 225 226 227

**Return value**

| Value Type | Description|
| -------- | -------- |
G
Gloria 已提交
228
| number | Returns the index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
229

G
Gloria 已提交
230 231
**Error codes**

232
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
233 234 235 236 237

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

W
wusongqing 已提交
238 239
**Example**

W
wusongqing 已提交
240
```ts
W
wusongqing 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
let result = list.getLastIndexOf(2);
```

### getIndexOf

getIndexOf(element: T): number

W
wusongqing 已提交
256 257 258
Obtains the index of the first occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
259 260 261 262 263

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
264
| element | T | Yes| Target element.|
W
wusongqing 已提交
265 266 267 268 269

**Return value**

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

G
Gloria 已提交
272 273
**Error codes**

274
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
275 276 277 278 279

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

W
wusongqing 已提交
280 281
**Example**

W
wusongqing 已提交
282
```ts
W
wusongqing 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(1);
list.add(2);
list.add(4);
list.getIndexOf(2);
let result = list.getIndexOf(2);
```

### equal

equal(obj: Object): boolean

Compares whether a specified object is equal to this container.

W
wusongqing 已提交
301 302
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
303 304 305 306 307 308 309 310 311 312 313 314
**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| obj | Object | Yes| Object used for comparison.|

**Return value**

| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the two are equal; returns **false** otherwise.|

G
Gloria 已提交
315 316
**Error codes**

317
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
318 319 320 321 322

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

W
wusongqing 已提交
323 324
**Example**

W
wusongqing 已提交
325
```ts
W
wusongqing 已提交
326 327 328 329 330 331 332 333 334 335
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
let obj1 = new List();
obj1.add(2);
obj1.add(4);
obj1.add(5);
list.equal(obj1);
G
Gloria 已提交
336
let obj2 = {name : "Dylon", age : "13"};
W
wusongqing 已提交
337 338 339 340 341 342 343
let result = list.equal(obj2);
```

### removeByIndex

removeByIndex(index: number): T

W
wusongqing 已提交
344 345 346
Removes an element at the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
347 348 349 350 351

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
352
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
353 354 355 356 357

**Return value**

| Value Type | Description|
| -------- | -------- |
W
wusongqing 已提交
358
| T | Element removed.|
W
wusongqing 已提交
359

G
Gloria 已提交
360 361
**Error codes**

362
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
363 364 365 366

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

W
wusongqing 已提交
369 370
**Example**

W
wusongqing 已提交
371
```ts
W
wusongqing 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(2);
list.add(4);
let result = list.removeByIndex(2);
```

### remove

remove(element: T): boolean

W
wusongqing 已提交
385 386 387
Removes the first occurrence of the specified element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
388 389 390 391 392

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
393
| element | T | Yes| Target element.|
W
wusongqing 已提交
394 395 396 397 398

**Return value**

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

G
Gloria 已提交
401 402
**Error codes**

403
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
404 405 406 407 408

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

W
wusongqing 已提交
409 410
**Example**

W
wusongqing 已提交
411
```ts
W
wusongqing 已提交
412 413 414 415 416 417 418 419 420
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.remove(2);
```

### replaceAllElements
W
wusongqing 已提交
421

422
replaceAllElements(callbackFn: (value: T, index?: number, list?: List<T>) => T,
W
wusongqing 已提交
423 424
thisArg?: Object): void

W
wusongqing 已提交
425 426 427
Replaces all elements in this container with new elements, and returns the new ones.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
428 429 430 431 432

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
433
| callbackFn | function | Yes| Callback invoked for the replacement.|
W
wusongqing 已提交
434 435 436 437 438 439
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
440 441
| 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 已提交
442 443
| list | List<T> | No| Instance that invokes the **replaceAllElements** method.|

G
Gloria 已提交
444 445
**Error codes**

446
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
447 448 449 450 451

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

W
wusongqing 已提交
452 453
**Example**

W
wusongqing 已提交
454
```ts
W
wusongqing 已提交
455 456 457 458 459
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
460
list.replaceAllElements((value: number, index: number) => {
W
wusongqing 已提交
461 462
  return value = 2 * value;
});
463
list.replaceAllElements((value: number, index: number) => {
W
wusongqing 已提交
464 465 466 467 468
  return value = value - 2;
});
```

### forEach
W
wusongqing 已提交
469

470
forEach(callbackFn: (value: T, index?: number, List?: List<T>) => void,
W
wusongqing 已提交
471 472
thisArg?: Object): void

W
wusongqing 已提交
473 474 475
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
476 477 478 479 480

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
481
| callbackFn | function | Yes| Callback invoked for the replacement.|
W
wusongqing 已提交
482 483 484 485 486 487
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
488 489
| 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 已提交
490 491
| List | List<T> | No| Instance that invokes the **forEach** method.|

G
Gloria 已提交
492 493
**Error codes**

494
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
495 496 497 498 499

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

W
wusongqing 已提交
500 501
**Example**

W
wusongqing 已提交
502
```ts
W
wusongqing 已提交
503 504 505 506 507 508
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.forEach((value, index) => {
509
    console.log("value:" + value, "index:" + index);
W
wusongqing 已提交
510 511 512 513
});
```

### sort
W
wusongqing 已提交
514

W
wusongqing 已提交
515 516
sort(comparator: (firstValue: T, secondValue: T) => number): void

W
wusongqing 已提交
517 518 519
Sorts elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
520 521 522

**Parameters**

W
wusongqing 已提交
523
| Name| Value Type | Mandatory| Description|
W
wusongqing 已提交
524
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
525
| comparator | function | Yes| Callback invoked for sorting.|
W
wusongqing 已提交
526 527 528

comparator

W
wusongqing 已提交
529
| Name| Value Type | Mandatory| Description|
W
wusongqing 已提交
530
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
531 532
| firstValue | T | Yes| Previous element.|
| secondValue | T | Yes| Next element.|
W
wusongqing 已提交
533

G
Gloria 已提交
534 535
**Error codes**

536
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
537 538 539 540 541

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

W
wusongqing 已提交
542 543
**Example**

W
wusongqing 已提交
544
```ts
W
wusongqing 已提交
545 546 547 548 549
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
550 551
list.sort((a: number, b: number) => a - b); // The elements are sorted in ascending order.
list.sort((a: number, b: number) => b - a); // The elements are sorted in descending order.
W
wusongqing 已提交
552 553 554
```

### getSubList
W
wusongqing 已提交
555

W
wusongqing 已提交
556 557
getSubList(fromIndex: number, toIndex: number): List<T>

W
wusongqing 已提交
558 559 560
Obtains elements within a range in this container, including the element at the start position but not that at the end position, and returns these elements as a new **List** instance.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|

**Return value**

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

G
Gloria 已提交
575 576
**Error codes**

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

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getSubList method cannot be bound. |
582
| 10200001 | The value of fromIndex or toIndex is out of range. |
G
Gloria 已提交
583

W
wusongqing 已提交
584 585
**Example**

W
wusongqing 已提交
586
```ts
W
wusongqing 已提交
587 588 589 590 591
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
592 593 594
let result = list.getSubList(2, 4);
let result1 = list.getSubList(4, 3);
let result2 = list.getSubList(2, 6);
W
wusongqing 已提交
595 596 597
```

### clear
W
wusongqing 已提交
598

W
wusongqing 已提交
599 600 601 602
clear(): void

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

W
wusongqing 已提交
603 604
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
605 606
**Error codes**

607
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
608 609 610 611 612

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

W
wusongqing 已提交
613 614
**Example**

W
wusongqing 已提交
615
```ts
W
wusongqing 已提交
616 617 618 619 620 621 622 623 624
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.clear();
```

### set
W
wusongqing 已提交
625

W
wusongqing 已提交
626
set(index: number, element: T): T
W
wusongqing 已提交
627

W
wusongqing 已提交
628 629 630
Replaces an element at the specified position in this container with a given element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
631 632 633 634 635

**Parameters**

| Name| Value Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
636 637
| index | number | Yes| Position index of the target element.|
| element | T | Yes| Element to be used for replacement.|
W
wusongqing 已提交
638 639 640 641 642

**Return value**

| Value Type | Description|
| -------- | -------- |
W
wusongqing 已提交
643
| T | New element.|
W
wusongqing 已提交
644

G
Gloria 已提交
645 646
**Error codes**

647
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
648 649 650 651

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

W
wusongqing 已提交
654 655
**Example**

W
wusongqing 已提交
656
```ts
W
wusongqing 已提交
657 658 659 660 661 662 663 664 665
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
list.set(2, "b");
```

### convertToArray
W
wusongqing 已提交
666

W
wusongqing 已提交
667 668 669 670
convertToArray(): Array<T>

Converts this container into an array.

W
wusongqing 已提交
671 672
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
673 674
**Return value**

W
wusongqing 已提交
675
| Value Type | Description|
W
wusongqing 已提交
676
| -------- | -------- |
W
wusongqing 已提交
677
| Array<T> | Array obtained.|
W
wusongqing 已提交
678

G
Gloria 已提交
679 680
**Error codes**

681
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
682 683 684 685 686

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

W
wusongqing 已提交
687 688
**Example**

W
wusongqing 已提交
689
```ts
W
wusongqing 已提交
690 691 692 693 694 695 696 697 698
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.convertToArray();
```

### isEmpty
W
wusongqing 已提交
699

W
wusongqing 已提交
700 701
isEmpty(): boolean

W
wusongqing 已提交
702 703 704
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
705 706 707 708 709 710 711

**Return value**

| Value Type | Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

G
Gloria 已提交
712 713
**Error codes**

714
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
715 716 717 718 719

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

W
wusongqing 已提交
720 721
**Example**

W
wusongqing 已提交
722
```ts
W
wusongqing 已提交
723 724 725 726 727 728 729 730 731 732 733 734
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.isEmpty();
```

### getFirst

getFirst(): T

W
wusongqing 已提交
735 736 737
Obtains the first element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
738 739 740 741 742

**Return value**

| Value Type | Description|
| -------- | -------- |
W
wusongqing 已提交
743
| T | The first element obtained.|
W
wusongqing 已提交
744

G
Gloria 已提交
745 746
**Error codes**

747
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
748 749 750 751 752

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

W
wusongqing 已提交
753 754
**Example**

W
wusongqing 已提交
755
```ts
W
wusongqing 已提交
756
let list = new List();
W
wusongqing 已提交
757 758 759 760 761 762 763 764 765 766 767
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getFirst();
```

### getLast

getLast(): T

W
wusongqing 已提交
768 769 770
Obtains the last element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
771 772 773 774 775

**Return value**

| Value Type | Description|
| -------- | -------- |
W
wusongqing 已提交
776
| T | The last element obtained.|
W
wusongqing 已提交
777

G
Gloria 已提交
778 779
**Error codes**

780
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
781 782 783 784 785

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

W
wusongqing 已提交
786 787
**Example**

W
wusongqing 已提交
788
```ts
W
wusongqing 已提交
789
let list = new List();
W
wusongqing 已提交
790 791 792 793 794 795 796 797 798
list.add(2);
list.add(4);
list.add(5);
list.add(4);
let result = list.getLast();
```

### [Symbol.iterator]

W
wusongqing 已提交
799
[Symbol.iterator]\(): IterableIterator<T>
W
wusongqing 已提交
800 801 802

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

W
wusongqing 已提交
803 804
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
805 806 807 808 809 810
**Return value**

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

G
Gloria 已提交
811 812
**Error codes**

813
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
814 815 816 817 818

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

W
wusongqing 已提交
819 820
**Example**

W
wusongqing 已提交
821
```ts
W
wusongqing 已提交
822 823 824 825 826 827 828 829
let list = new List();
list.add(2);
list.add(4);
list.add(5);
list.add(4);

// Method 1:
for (let item of list) { 
W
wusongqing 已提交
830
  console.log("value: " + item); 
W
wusongqing 已提交
831 832 833 834 835 836
}

// Method 2:
let iter = list[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
837
  console.log("value: " + temp);
W
wusongqing 已提交
838 839 840
  temp = iter.next().value;
}
```