# @ohos.util.ArrayList (Linear Container ArrayList) 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. Similar 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. When 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. **Recommended use case**: Use ArrayList when elements in a container need to be frequently read. This topic uses the following to identify the use of generics: - T: Type > **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. ## Modules to Import ```ts import { ArrayList } from '@kit.ArkTS'; ``` ## ArrayList ### Properties **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang | Name| Type| Read-Only| Optional| Description| | -------- | -------- | -------- | -------- | -------- | | length | number | Yes| No| Number of elements in an ArrayList.| ### constructor constructor() A constructor used to create an **ArrayList** instance. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200012 | The ArrayList's constructor cannot be directly invoked. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); ``` ### add add(element: T): boolean Adds an element at the end of this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Return value** | Type| Description| | -------- | -------- | | boolean | Operation result. The value **true** is returned if the element is added; otherwise, **false** is returned.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The add method cannot be bound. | **Example** ```ts class C1 { name: string = "" age: string = "" } let arrayList: ArrayList | C1> = new ArrayList(); let result1 = arrayList.add("a"); let result2 = arrayList.add(1); let b = [1, 2, 3]; let result3 = arrayList.add(b); let c : C1 = {name: "Dylan", age: "13"} let result4 = arrayList.add(c); let result5 = arrayList.add(false); ``` ### insert insert(element: T, index: number): void Inserts an element at the specified position in this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| | 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.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 10200001 | The value of index is out of range. | | 10200011 | The insert method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.insert("A", 0); arrayList.insert(0, 1); arrayList.insert(true, 2); ``` ### has has(element: T): boolean Checks whether this ArrayList has the specified element. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Return value** | Type| Description| | -------- | -------- | | boolean | Check result. The value **true** is returned if the specified element is contained; otherwise, **false** is returned.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The has method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add("squirrel"); let result: boolean = arrayList.has("squirrel"); ``` ### getIndexOf getIndexOf(element: T): number Obtains the index of the first occurrence of the specified element in this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Return value** | Type| Description| | -------- | -------- | | number | Returns the position index if obtained; returns **-1** if the specified element is not found.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The getIndexOf method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(2); arrayList.add(1); arrayList.add(2); arrayList.add(4); let result: number = arrayList.getIndexOf(2); ``` ### getLastIndexOf getLastIndexOf(element: T): number Obtains the index of the last occurrence of the specified element in this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Return value** | Type| Description| | -------- | -------- | | number | Returns the position index if obtained; returns **-1** if the specified element is not found.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The getLastIndexOf method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(2); arrayList.add(1); arrayList.add(2); arrayList.add(4); let result: number = arrayList.getLastIndexOf(2); ``` ### removeByIndex removeByIndex(index: number): T Removes an element with the specified position from this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | index | number | Yes| Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647.| **Return value** | Type| Description| | -------- | -------- | | T | Element removed.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 10200001 | The value of index is out of range. | | 10200011 | The removeByIndex method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(2); arrayList.add(4); let result: number = arrayList.removeByIndex(2); ``` ### remove remove(element: T): boolean Removes the first occurrence of the specified element from this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | element | T | Yes| Target element.| **Return value** | Type| Description| | -------- | -------- | | boolean | Operation result. The value **true** is returned if the element is removed; otherwise, **false** is returned.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The remove method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: boolean = arrayList.remove(2); ``` ### removeByRange removeByRange(fromIndex: number, toIndex: number): void Removes elements within a range, including the element at the start position but not that at the end position. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | fromIndex | number | Yes| Index of the start position.| | toIndex | number | Yes| Index of the end position.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 10200001 | The value of fromIndex or toIndex is out of range. | | 10200011 | The removeByRange method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.removeByRange(2, 4); ``` ### replaceAllElements replaceAllElements(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => T, thisArg?: Object): void Replaces all elements in this ArrayList with new elements, and returns the new ones. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callbackFn | function | Yes| Callback invoked for the replacement.| | thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| callbackFn parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | T | Yes| Value of the element that is currently traversed.| | index | number | No| Position index of the element that is currently traversed. The default value is **0**.| | arrlist | ArrayList<T> | No| Instance that calls the **replaceAllElements** API. The default value is this instance.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 10200011 | The replaceAllElements method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.replaceAllElements((value: number): number => { // Add the user operation logic based on the actual scenario. return value; }); ``` ### forEach forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, thisArg?: Object): void Uses a callback to traverse the elements in this ArrayList and obtain their indexes. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | callbackFn | function | Yes| Callback invoked for the replacement.| | thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| callbackFn parameters | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | value | T | Yes| Value of the element that is currently traversed.| | index | number | No| Position index of the element that is currently traversed. The default value is **0**.| | arrlist | ArrayList<T> | No| Instance that calls the **forEach** API. The default value is this instance.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 10200011 | The forEach method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.forEach((value: number, index?: number) => { console.log("value:" + value, "index:" + index); }); ``` ### sort sort(comparator?: (firstValue: T, secondValue: T) => number): void Sorts elements in this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | comparator | function | No| Callback invoked for sorting. The default value is the callback function for sorting elements in ascending order.| comparator | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | firstValue | T | Yes| Previous element.| | secondValue | T | Yes| Next element.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed. | | 10200011 | The sort method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.sort((a: number, b: number) => a - b); arrayList.sort((a: number, b: number) => b - a); arrayList.sort(); ``` ### subArrayList subArrayList(fromIndex: number, toIndex: number): ArrayList<T> Obtains elements within a range in this ArrayList, including the element at the start position but not that at the end position, and returns these elements as a new **ArrayList** instance. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | fromIndex | number | Yes| Index of the start position.| | toIndex | number | Yes| Index of the end position.| **Return value** | Type| Description| | -------- | -------- | | ArrayList<T> | New **ArrayList** instance obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 10200001 | The value of fromIndex or toIndex is out of range. | | 10200011 | The subArrayList method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: ArrayList = arrayList.subArrayList(2, 4); ``` ### clear clear(): void Clears this ArrayList and sets its length to **0**. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The clear method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.clear(); ``` ### clone clone(): ArrayList<T> Clones this ArrayList and returns a copy. The modification to the copy does not affect the original instance. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | ArrayList<T> | New **ArrayList** instance obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The clone method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: ArrayList = arrayList.clone(); ``` ### getCapacity getCapacity(): number Obtains the capacity of this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | number | Capacity obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The getCapacity method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: number = arrayList.getCapacity(); ``` ### convertToArray convertToArray(): Array<T> Converts this ArrayList into an array. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | Array<T> | Array obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The convertToArray method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: Array = arrayList.convertToArray(); ``` ### isEmpty isEmpty(): boolean Checks whether this ArrayList is empty (contains no element). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | boolean | Check result. The value **true** is returned if the ArrayList is empty; otherwise, **false** is returned.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The isEmpty method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: boolean = arrayList.isEmpty(); ``` ### \[index: number\]12+ \[index: number\]: T Returns the element at the given index. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | index | number | Yes| Index. The value must be less than or equal to int32_max, that is, 2147483647.| **Return value** | Type| Description| | -------- | -------- | | T | Element obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. | | 10200001 | The value of index is out of range. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); let result: number = arrayList[2]; ``` ### increaseCapacityTo increaseCapacityTo(newCapacity: number): void Increases the capacity of this ArrayList. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Parameters** | Name| Type| Mandatory| Description| | -------- | -------- | -------- | -------- | | newCapacity | number | Yes| New capacity.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 10200011 | The increaseCapacityTo method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.increaseCapacityTo(2); arrayList.increaseCapacityTo(8); ``` ### trimToCurrentLength trimToCurrentLength(): void Releases the reserved space in this ArrayList by adjusting the capacity to the actual number of elements in it. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The trimToCurrentLength method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); arrayList.trimToCurrentLength(); ``` ### [Symbol.iterator] [Symbol.iterator]\(): IterableIterator<T> Returns an iterator, each item of which is a JavaScript object. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.Utils.Lang **Return value** | Type| Description| | -------- | -------- | | IterableIterator<T> | Iterator obtained.| **Error codes** For details about the error codes, see [Utils Error Codes](errorcode-utils.md). | ID| Error Message| | -------- | -------- | | 10200011 | The Symbol.iterator method cannot be bound. | **Example** ```ts let arrayList: ArrayList = new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(5); arrayList.add(4); // Method 1: let numbers: Array = arrayList.convertToArray(); for (let item of numbers) { console.log(`value : ${item}`); } // Method 2: let iter = arrayList[Symbol.iterator](); let temp: IteratorResult = iter.next(); while(!temp.done) { console.log(`value:${temp.value}`); temp = iter.next(); } ```