1# @ohos.util.ArrayList (Linear Container ArrayList) 2 3**ArrayList** is a linear data structure that is implemented based on arrays. **ArrayList** can dynamically adjust the capacity based on project requirements. It increases the capacity by 50% each time. 4 5Similar to **ArrayList**, **[Vector](js-apis-vector.md)** is also implemented based on arrays and can dynamically adjust the capacity. It increases the capability by 100% each time. 6 7When compared with **[LinkedList](js-apis-linkedlist.md)**, **ArrayList** is more efficient in random access but less efficient in the addition or removal operation, because its addition or removal operation affects the position of other elements in the container. 8 9**Recommended use case**: Use **ArrayList** when elements in a container need to be frequently read. 10 11This topic uses the following to identify the use of generics: 12- T: Type 13 14> **NOTE** 15> 16> 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. 17 18 19## Modules to Import 20 21```ts 22import { ArrayList } from '@kit.ArkTS'; 23``` 24 25## ArrayList 26 27### Attributes 28 29**Atomic service API**: This API can be used in atomic services since API version 12. 30 31**System capability**: SystemCapability.Utils.Lang 32 33| Name| Type| Readable| Writable| Description| 34| -------- | -------- | -------- | -------- | -------- | 35| length | number | Yes| No| Number of elements in an array list (called container later).| 36 37 38### constructor 39 40constructor() 41 42A constructor used to create an **ArrayList** instance. 43 44**Atomic service API**: This API can be used in atomic services since API version 12. 45 46**System capability**: SystemCapability.Utils.Lang 47 48**Error codes** 49 50For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 51 52| ID| Error Message| 53| -------- | -------- | 54| 10200012 | The ArrayList's constructor cannot be directly invoked. | 55 56**Example** 57 58```ts 59let arrayList: ArrayList<string | number> = new ArrayList(); 60``` 61 62 63### add 64 65add(element: T): boolean 66 67Adds an element at the end of this container. 68 69**Atomic service API**: This API can be used in atomic services since API version 12. 70 71**System capability**: SystemCapability.Utils.Lang 72 73**Parameters** 74 75| Name| Type| Mandatory| Description| 76| -------- | -------- | -------- | -------- | 77| element | T | Yes| Target element.| 78 79**Return value** 80 81| Type| Description| 82| -------- | -------- | 83| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 84 85**Error codes** 86 87For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 88 89| ID| Error Message| 90| -------- | -------- | 91| 10200011 | The add method cannot be bound. | 92 93**Example** 94 95```ts 96class C1 { 97 name: string = "" 98 age: string = "" 99} 100let arrayList: ArrayList<string | number | boolean | Array<number> | C1> = new ArrayList(); 101let result1 = arrayList.add("a"); 102let arrayList1: ArrayList<number> = new ArrayList(); 103let result2 = arrayList.add(1); 104let b = [1, 2, 3]; 105let result3 = arrayList.add(b); 106let c : C1 = {name: "Dylan", age: "13"} 107let result4 = arrayList.add(c); 108let result5 = arrayList.add(false); 109``` 110 111### insert 112 113insert(element: T, index: number): void 114 115Inserts an element at the specified position in this container. 116 117**Atomic service API**: This API can be used in atomic services since API version 12. 118 119**System capability**: SystemCapability.Utils.Lang 120 121**Parameters** 122 123| Name| Type| Mandatory| Description| 124| -------- | -------- | -------- | -------- | 125| element | T | Yes| Target element.| 126| index | number | Yes| Index of the position where the element is to be inserted. The value must be less than or equal to int32_max, that is, 2147483647.| 127 128**Error codes** 129 130For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 131 132| ID| Error Message| 133| -------- | -------- | 134| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 135| 10200001 | The value of index is out of range. | 136| 10200011 | The insert method cannot be bound. | 137 138**Example** 139 140```ts 141let arrayList: ArrayList<number | string | boolean> = new ArrayList(); 142arrayList.insert("A", 0); 143arrayList.insert(0, 1); 144arrayList.insert(true, 2); 145``` 146 147### has 148 149has(element: T): boolean 150 151Checks whether this container has the specified element. 152 153**Atomic service API**: This API can be used in atomic services since API version 12. 154 155**System capability**: SystemCapability.Utils.Lang 156 157**Parameters** 158 159| Name| Type| Mandatory| Description| 160| -------- | -------- | -------- | -------- | 161| element | T | Yes| Target element.| 162 163**Return value** 164 165| Type| Description| 166| -------- | -------- | 167| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.| 168 169**Error codes** 170 171For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 172 173| ID| Error Message| 174| -------- | -------- | 175| 10200011 | The has method cannot be bound. | 176 177**Example** 178 179```ts 180let arrayList: ArrayList<string> = new ArrayList(); 181arrayList.add("squirrel"); 182let result: boolean = arrayList.has("squirrel"); 183``` 184 185### getIndexOf 186 187getIndexOf(element: T): number 188 189Obtains the index of the first occurrence of the specified element in this container. 190 191**Atomic service API**: This API can be used in atomic services since API version 12. 192 193**System capability**: SystemCapability.Utils.Lang 194 195**Parameters** 196 197| Name| Type| Mandatory| Description| 198| -------- | -------- | -------- | -------- | 199| element | T | Yes| Target element.| 200 201**Return value** 202 203| Type| Description| 204| -------- | -------- | 205| number | Returns the position index if obtained; returns **-1** if the specified element is not found.| 206 207**Error codes** 208 209For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 210 211| ID| Error Message| 212| -------- | -------- | 213| 10200011 | The getIndexOf method cannot be bound. | 214 215**Example** 216 217```ts 218let arrayList: ArrayList<number> = new ArrayList(); 219arrayList.add(2); 220arrayList.add(4); 221arrayList.add(5); 222arrayList.add(2); 223arrayList.add(1); 224arrayList.add(2); 225arrayList.add(4); 226let result: number = arrayList.getIndexOf(2); 227``` 228 229### getLastIndexOf 230 231getLastIndexOf(element: T): number 232 233Obtains the index of the last occurrence of the specified element in this container. 234 235**Atomic service API**: This API can be used in atomic services since API version 12. 236 237**System capability**: SystemCapability.Utils.Lang 238 239**Parameters** 240 241| Name| Type| Mandatory| Description| 242| -------- | -------- | -------- | -------- | 243| element | T | Yes| Target element.| 244 245**Return value** 246 247| Type| Description| 248| -------- | -------- | 249| number | Returns the position index if obtained; returns **-1** if the specified element is not found.| 250 251**Error codes** 252 253For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 254 255| ID| Error Message| 256| -------- | -------- | 257| 10200011 | The getLastIndexOf method cannot be bound. | 258 259**Example** 260 261```ts 262let arrayList: ArrayList<number> = new ArrayList(); 263arrayList.add(2); 264arrayList.add(4); 265arrayList.add(5); 266arrayList.add(2); 267arrayList.add(1); 268arrayList.add(2); 269arrayList.add(4); 270let result: number = arrayList.getLastIndexOf(2); 271``` 272 273### removeByIndex 274 275removeByIndex(index: number): T 276 277Removes an element with the specified position from this container. 278 279**Atomic service API**: This API can be used in atomic services since API version 12. 280 281**System capability**: SystemCapability.Utils.Lang 282 283**Parameters** 284 285| Name| Type| Mandatory| Description| 286| -------- | -------- | -------- | -------- | 287| index | number | Yes| Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647.| 288 289**Return value** 290 291| Type| Description| 292| -------- | -------- | 293| T | Element removed.| 294 295**Error codes** 296 297For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 298 299| ID| Error Message| 300| -------- | -------- | 301| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 302| 10200001 | The value of index is out of range. | 303| 10200011 | The removeByIndex method cannot be bound. | 304 305**Example** 306 307```ts 308let arrayList: ArrayList<number> = new ArrayList(); 309arrayList.add(2); 310arrayList.add(4); 311arrayList.add(5); 312arrayList.add(2); 313arrayList.add(4); 314let result: number = arrayList.removeByIndex(2); 315``` 316 317### remove 318 319remove(element: T): boolean 320 321Removes the first occurrence of the specified element from this container. 322 323**Atomic service API**: This API can be used in atomic services since API version 12. 324 325**System capability**: SystemCapability.Utils.Lang 326 327**Parameters** 328 329| Name| Type| Mandatory| Description| 330| -------- | -------- | -------- | -------- | 331| element | T | Yes| Target element.| 332 333**Return value** 334 335| Type| Description| 336| -------- | -------- | 337| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 338 339**Error codes** 340 341For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 342 343| ID| Error Message| 344| -------- | -------- | 345| 10200011 | The remove method cannot be bound. | 346 347**Example** 348 349```ts 350let arrayList: ArrayList<number> = new ArrayList(); 351arrayList.add(2); 352arrayList.add(4); 353arrayList.add(5); 354arrayList.add(4); 355let result: boolean = arrayList.remove(2); 356``` 357 358### removeByRange 359 360removeByRange(fromIndex: number, toIndex: number): void 361 362Removes from this container all of the elements within a range, including the element at the start position but not that at the end position. 363 364**Atomic service API**: This API can be used in atomic services since API version 12. 365 366**System capability**: SystemCapability.Utils.Lang 367 368**Parameters** 369 370| Name| Type| Mandatory| Description| 371| -------- | -------- | -------- | -------- | 372| fromIndex | number | Yes| Index of the start position.| 373| toIndex | number | Yes| Index of the end position.| 374 375**Error codes** 376 377For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 378 379| ID| Error Message| 380| -------- | -------- | 381| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 382| 10200001 | The value of fromIndex or toIndex is out of range. | 383| 10200011 | The removeByRange method cannot be bound. | 384 385**Example** 386 387```ts 388let arrayList: ArrayList<number> = new ArrayList(); 389arrayList.add(2); 390arrayList.add(4); 391arrayList.add(5); 392arrayList.add(4); 393arrayList.removeByRange(2, 4); 394``` 395 396### replaceAllElements 397 398replaceAllElements(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => T, 399thisArg?: Object): void 400 401Replaces all elements in this container with new elements, and returns the new ones. 402 403**Atomic service API**: This API can be used in atomic services since API version 12. 404 405**System capability**: SystemCapability.Utils.Lang 406 407**Parameters** 408 409| Name| Type| Mandatory| Description| 410| -------- | -------- | -------- | -------- | 411| callbackFn | function | Yes| Callback invoked for the replacement.| 412| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 413 414callbackFn 415 416| Name| Type| Mandatory| Description| 417| -------- | -------- | -------- | -------- | 418| value | T | Yes| Value of the element that is currently traversed.| 419| index | number | No| Position index of the element that is currently traversed. The default value is **0**.| 420| arrlist | ArrayList<T> | No| Instance that calls the **replaceAllElements** API. The default value is this instance.| 421 422**Error codes** 423 424For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 425 426| ID| Error Message| 427| -------- | -------- | 428| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 429| 10200011 | The replaceAllElements method cannot be bound. | 430 431**Example** 432 433```ts 434let arrayList: ArrayList<number> = new ArrayList(); 435arrayList.add(2); 436arrayList.add(4); 437arrayList.add(5); 438arrayList.add(4); 439arrayList.replaceAllElements((value: number): number => { 440 // Add the user operation logic based on the actual scenario. 441 return value; 442}); 443``` 444 445### forEach 446 447forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, 448thisArg?: Object): void 449 450Uses a callback to traverse the elements in this container and obtain their position indexes. 451 452**Atomic service API**: This API can be used in atomic services since API version 12. 453 454**System capability**: SystemCapability.Utils.Lang 455 456**Parameters** 457 458| Name| Type| Mandatory| Description| 459| -------- | -------- | -------- | -------- | 460| callbackFn | function | Yes| Callback invoked for the replacement.| 461| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 462 463callbackFn 464 465| Name| Type| Mandatory| Description| 466| -------- | -------- | -------- | -------- | 467| value | T | Yes| Value of the element that is currently traversed.| 468| index | number | No| Position index of the element that is currently traversed. The default value is **0**.| 469| arrlist | ArrayList<T> | No| Instance that calls the **forEach** API. The default value is this instance.| 470 471**Error codes** 472 473For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 474 475| ID| Error Message| 476| -------- | -------- | 477| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 478| 10200011 | The forEach method cannot be bound. | 479 480**Example** 481 482```ts 483let arrayList: ArrayList<number> = new ArrayList(); 484arrayList.add(2); 485arrayList.add(4); 486arrayList.add(5); 487arrayList.add(4); 488arrayList.forEach((value: number, index?: number) => { 489 console.log("value:" + value, "index:" + index); 490}); 491``` 492 493### sort 494 495sort(comparator?: (firstValue: T, secondValue: T) => number): void 496 497Sorts elements in this container. 498 499**Atomic service API**: This API can be used in atomic services since API version 12. 500 501**System capability**: SystemCapability.Utils.Lang 502 503**Parameters** 504 505| Name| Type| Mandatory| Description| 506| -------- | -------- | -------- | -------- | 507| comparator | function | No| Callback invoked for sorting. The default value is the callback function for sorting elements in ascending order.| 508 509comparator 510 511| Name| Type| Mandatory| Description| 512| -------- | -------- | -------- | -------- | 513| firstValue | T | Yes| Previous element.| 514| secondValue | T | Yes| Next element.| 515 516**Error codes** 517 518For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 519 520| ID| Error Message| 521| -------- | -------- | 522| 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed. | 523| 10200011 | The sort method cannot be bound. | 524 525**Example** 526 527```ts 528let arrayList: ArrayList<number> = new ArrayList(); 529arrayList.add(2); 530arrayList.add(4); 531arrayList.add(5); 532arrayList.add(4); 533arrayList.sort((a: number, b: number) => a - b); 534arrayList.sort((a: number, b: number) => b - a); 535arrayList.sort(); 536``` 537 538### subArrayList 539 540subArrayList(fromIndex: number, toIndex: number): ArrayList<T> 541 542Obtains 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 **ArrayList** instance. 543 544**Atomic service API**: This API can be used in atomic services since API version 12. 545 546**System capability**: SystemCapability.Utils.Lang 547 548**Parameters** 549 550| Name| Type| Mandatory| Description| 551| -------- | -------- | -------- | -------- | 552| fromIndex | number | Yes| Index of the start position.| 553| toIndex | number | Yes| Index of the end position.| 554 555**Return value** 556 557| Type| Description| 558| -------- | -------- | 559| ArrayList<T> | New **ArrayList** instance obtained.| 560 561**Error codes** 562 563For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 564 565| ID| Error Message| 566| -------- | -------- | 567| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 568| 10200001 | The value of fromIndex or toIndex is out of range. | 569| 10200011 | The subArrayList method cannot be bound. | 570 571**Example** 572 573```ts 574let arrayList: ArrayList<number> = new ArrayList(); 575arrayList.add(2); 576arrayList.add(4); 577arrayList.add(5); 578arrayList.add(4); 579let result: ArrayList<number> = arrayList.subArrayList(2, 4); 580``` 581 582### clear 583 584clear(): void 585 586Clears this container and sets its length to **0**. 587 588**Atomic service API**: This API can be used in atomic services since API version 12. 589 590**System capability**: SystemCapability.Utils.Lang 591 592**Error codes** 593 594For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 595 596| ID| Error Message| 597| -------- | -------- | 598| 10200011 | The clear method cannot be bound. | 599 600**Example** 601 602```ts 603let arrayList: ArrayList<number> = new ArrayList(); 604arrayList.add(2); 605arrayList.add(4); 606arrayList.add(5); 607arrayList.add(4); 608arrayList.clear(); 609``` 610 611### clone 612 613clone(): ArrayList<T> 614 615Clones this container and returns a copy. The modification to the copy does not affect the original instance. 616 617**Atomic service API**: This API can be used in atomic services since API version 12. 618 619**System capability**: SystemCapability.Utils.Lang 620 621 622**Return value** 623 624| Type| Description| 625| -------- | -------- | 626| ArrayList<T> | New **ArrayList** instance obtained.| 627 628**Error codes** 629 630For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 631 632| ID| Error Message| 633| -------- | -------- | 634| 10200011 | The clone method cannot be bound. | 635 636**Example** 637 638```ts 639let arrayList: ArrayList<number> = new ArrayList(); 640arrayList.add(2); 641arrayList.add(4); 642arrayList.add(5); 643arrayList.add(4); 644let result: ArrayList<number> = arrayList.clone(); 645``` 646 647### getCapacity 648 649getCapacity(): number 650 651Obtains the capacity of this container. 652 653**Atomic service API**: This API can be used in atomic services since API version 12. 654 655**System capability**: SystemCapability.Utils.Lang 656 657**Return value** 658 659| Type| Description| 660| -------- | -------- | 661| number | Capacity obtained.| 662 663**Error codes** 664 665For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 666 667| ID| Error Message| 668| -------- | -------- | 669| 10200011 | The getCapacity method cannot be bound. | 670 671**Example** 672 673```ts 674let arrayList: ArrayList<number> = new ArrayList(); 675arrayList.add(2); 676arrayList.add(4); 677arrayList.add(5); 678arrayList.add(4); 679let result: number = arrayList.getCapacity(); 680``` 681 682### convertToArray 683 684convertToArray(): Array<T> 685 686Converts this container into an array. 687 688**Atomic service API**: This API can be used in atomic services since API version 12. 689 690**System capability**: SystemCapability.Utils.Lang 691 692**Return value** 693 694| Type| Description| 695| -------- | -------- | 696| Array<T> | Array obtained.| 697 698**Error codes** 699 700For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 701 702| ID| Error Message| 703| -------- | -------- | 704| 10200011 | The convertToArray method cannot be bound. | 705 706**Example** 707 708```ts 709let arrayList: ArrayList<number> = new ArrayList(); 710arrayList.add(2); 711arrayList.add(4); 712arrayList.add(5); 713arrayList.add(4); 714let result: Array<number> = arrayList.convertToArray(); 715``` 716 717### isEmpty 718 719isEmpty(): boolean 720 721Checks whether this container is empty (contains no element). 722 723**Atomic service API**: This API can be used in atomic services since API version 12. 724 725**System capability**: SystemCapability.Utils.Lang 726 727**Return value** 728 729| Type| Description| 730| -------- | -------- | 731| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 732 733**Error codes** 734 735For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 736 737| ID| Error Message| 738| -------- | -------- | 739| 10200011 | The isEmpty method cannot be bound. | 740 741**Example** 742 743```ts 744let arrayList: ArrayList<number> = new ArrayList(); 745arrayList.add(2); 746arrayList.add(4); 747arrayList.add(5); 748arrayList.add(4); 749let result: boolean = arrayList.isEmpty(); 750``` 751 752### \[index: number\]<sup>12+</sup> 753 754\[index: number\]: T 755 756Returns the element at the given index. 757 758**Atomic service API**: This API can be used in atomic services since API version 12. 759 760**System capability**: SystemCapability.Utils.Lang 761 762**Parameters** 763 764| Name| Type| Mandatory| Description| 765| -------- | -------- | -------- | -------- | 766| index | number | Yes| Index. The value must be less than or equal to int32_max, that is, 2147483647.| 767 768**Return value** 769 770| Type| Description| 771| -------- | -------- | 772| T | Element obtained.| 773 774**Error codes** 775 776For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 777 778| ID| Error Message| 779| -------- | -------- | 780| 401 | Parameter error. | 781| 10200001 | The value of index is out of range. | 782 783**Example** 784 785```ts 786let arrayList: ArrayList<number> = new ArrayList(); 787arrayList.add(2); 788arrayList.add(4); 789arrayList.add(5); 790arrayList.add(4); 791let result: number = arrayList[2]; 792``` 793 794### increaseCapacityTo 795 796increaseCapacityTo(newCapacity: number): void 797 798Increases the capacity of this container. 799 800**Atomic service API**: This API can be used in atomic services since API version 12. 801 802**System capability**: SystemCapability.Utils.Lang 803 804**Parameters** 805 806| Name| Type| Mandatory| Description| 807| -------- | -------- | -------- | -------- | 808| newCapacity | number | Yes| New capacity.| 809 810**Error codes** 811 812For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 813 814| ID| Error Message| 815| -------- | -------- | 816| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 817| 10200011 | The increaseCapacityTo method cannot be bound. | 818 819**Example** 820 821```ts 822let arrayList: ArrayList<number> = new ArrayList(); 823arrayList.add(2); 824arrayList.add(4); 825arrayList.add(5); 826arrayList.add(4); 827arrayList.increaseCapacityTo(2); 828arrayList.increaseCapacityTo(8); 829``` 830 831### trimToCurrentLength 832 833trimToCurrentLength(): void 834 835Releases the reserved space in this container by adjusting the container capacity to the actual number of elements in this container. 836 837**Atomic service API**: This API can be used in atomic services since API version 12. 838 839**System capability**: SystemCapability.Utils.Lang 840 841**Error codes** 842 843For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 844 845| ID| Error Message| 846| -------- | -------- | 847| 10200011 | The trimToCurrentLength method cannot be bound. | 848 849**Example** 850 851```ts 852let arrayList: ArrayList<number> = new ArrayList(); 853arrayList.add(2); 854arrayList.add(4); 855arrayList.add(5); 856arrayList.add(4); 857arrayList.trimToCurrentLength(); 858``` 859 860### [Symbol.iterator] 861 862[Symbol.iterator]\(): IterableIterator<T> 863 864Obtains an iterator, each item of which is a JavaScript object. 865 866> **NOTE** 867> 868> This API cannot be used in .ets files. 869 870**Atomic service API**: This API can be used in atomic services since API version 12. 871 872**System capability**: SystemCapability.Utils.Lang 873 874**Return value** 875 876| Type| Description| 877| -------- | -------- | 878| IterableIterator<T> | Iterator obtained.| 879 880**Error codes** 881 882For details about the error codes, see [Utils Error Codes](errorcode-utils.md). 883 884| ID| Error Message| 885| -------- | -------- | 886| 10200011 | The Symbol.iterator method cannot be bound. | 887 888**Example** 889 890```ts 891let arrayList: ArrayList<number> = new ArrayList(); 892arrayList.add(2); 893arrayList.add(4); 894arrayList.add(5); 895arrayList.add(4); 896 897// Method 1: 898let numbers: Array<number> = arrayList.convertToArray() 899for (let item of numbers) { 900 console.log(`value : ${item}`); 901} 902 903// Method 2: 904let iter = arrayList[Symbol.iterator](); 905let temp: IteratorResult<number> = iter.next(); 906while(!temp.done) { 907 console.log(`value:${temp.value}`); 908 temp = iter.next(); 909} 910``` 911