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