1e41f4b71Sopenharmony_ci# @arkts.collections (ArkTS Collections) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe collections module provides ArkTS containers for efficient data transfer in concurrency scenarios. The ArkTS containers provide similar functionalities as their JavaScript counterparts, except that their properties cannot be added or updated through \. or \[\]. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciBy default, ArkTS containers are passed by reference between concurrent instances. This means that multiple concurrent instances can simultaneously operate the same container instance. Pass-by-copy is also supported. In this mode, each concurrent instance holds an ArkTS container instance. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciArkTS containers are not thread-safe. They adopt the fail-fast approach. An exception is thrown if multiple concurrent instances make structural changes to a container instance at the same time. Therefore, in update scenarios, you must use the ArkTS asynchronous lock to ensure secure access to the ArkTS containers. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciCurrently, the ArkTS collection provides the following containers: [Array](#collectionsarray), [Map](#collectionsmap), [Set](#collectionsset), and [TypedArray](#collectionstypedarray). 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci> **NOTE** 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Modules to Import 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```ts 18e41f4b71Sopenharmony_ciimport { collections } from '@kit.ArkTS'; 19e41f4b71Sopenharmony_ci``` 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## ISendable 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_citype ISendable = lang.ISendable 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**ISendable** is the parent type of all sendable types except null and undefined. It does not have any necessary methods or properties. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| Type| Description | 32e41f4b71Sopenharmony_ci| ------ | ------ | 33e41f4b71Sopenharmony_ci| [lang.ISendable](js-apis-arkts-lang.md#langisendable) | Parent type of all sendable types.| 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci## collections.ConcatArray 36e41f4b71Sopenharmony_ciAn array-like object that can be concatenated. This API extends **ISendable**. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciThis section uses the following to identify the use of generics: 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci- T: type, which can be any of the [Sendable Data Types](../../arkts-utils/arkts-sendable.md). 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci### Properties 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 49e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ----------------- | 50e41f4b71Sopenharmony_ci| length | number | Yes | No | Number of elements in a ConcatArray.| 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci### [index: number] 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_cireadonly [index: number]: T 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ciReturns the element at a given index in this ConcatArray. 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 61e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ------------------------------------------------------------------ | 62e41f4b71Sopenharmony_ci| index | number | Yes | Index of the element. The index starts from zero. If the passed-in index is less than 0 or greater than or equal to the value of **length**, an error is thrown.| 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**Return value** 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci| Type | Description | 67e41f4b71Sopenharmony_ci| ----- | ------------------------ | 68e41f4b71Sopenharmony_ci| T | Element in the ConcatArray.| 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci**Error codes** 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci| ID| Error Message | 75e41f4b71Sopenharmony_ci| ------- | ------------------------------------ | 76e41f4b71Sopenharmony_ci| 401 | Parameter error. Illegal index. | 77e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. | 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**Example** 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci```ts 82e41f4b71Sopenharmony_cilet concatArray : collections.ConcatArray<number> = new collections.Array<number>(1, 2, 4); 83e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", concatArray[1]); 84e41f4b71Sopenharmony_ci``` 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci### join 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_cijoin(separator?: string): string 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciConcatenates all elements in this array into a string, with a given separator. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Parameters** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 99e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------------------------- | 100e41f4b71Sopenharmony_ci| separator | string | No | Separator to be used. If no value is passed in, a comma (,) is used as the separator.| 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**Return value** 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci| Type | Description | 105e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | 106e41f4b71Sopenharmony_ci| string | String obtained. If the array is empty, an empty string is returned.| 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci**Error codes** 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci| ID| Error Message| 113e41f4b71Sopenharmony_ci| ------- | -------- | 114e41f4b71Sopenharmony_ci| 401 | Parameter error. Invalid separator. | 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**Example** 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci```ts 119e41f4b71Sopenharmony_cilet concatArray : collections.ConcatArray<string> = new collections.Array<string>('a', 'b', 'c'); 120e41f4b71Sopenharmony_cilet joinedString = concatArray.join('-'); // "a-b-c" is returned. 121e41f4b71Sopenharmony_ci``` 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci### slice 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_cislice(start?: number, end?: number): ConcatArray\<T> 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ciSelects a range of elements in this array to create an array. 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci**Parameters** 134e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 135e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 136e41f4b71Sopenharmony_ci| start | number | No | Start index of the range. If a negative number is passed in, it refers to the index of **start + array.length**. The default value is **0**. | 137e41f4b71Sopenharmony_ci| end | number | No | End index of the range (exclusive). If a negative number is passed in, it refers to the index of **end + array.length**. The default value is the length of the ArkTS array.| 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**Return value** 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci| Type | Description | 142e41f4b71Sopenharmony_ci| --------- | -------------------------- | 143e41f4b71Sopenharmony_ci| ConcatArray\<T> | New array containing the selected elements.| 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci**Error codes** 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci| ID| Error Message| 150e41f4b71Sopenharmony_ci| ------- | -------- | 151e41f4b71Sopenharmony_ci| 401 | Parameter error. Invalid `start` or `end` parameters. | 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci**Example** 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci```ts 156e41f4b71Sopenharmony_cilet concatArray : collections.ConcatArray<number> = new collections.Array<number>(1, 2, 3, 4, 5); 157e41f4b71Sopenharmony_cilet slicedArray = concatArray.slice (1, 3); // [2, 3] is returned. The original array remains unchanged. 158e41f4b71Sopenharmony_ci``` 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci## collections.Array 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ciA linear data structure that is implemented on arrays and can be passed between ArkTS concurrent instances. 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ciPass-by-reference is recommended for better transfer performance. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ciThis section uses the following to identify the use of generics: 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci- T: type, which can be any of the [Sendable Data Types](../../arkts-utils/arkts-sendable.md). 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci### Properties 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 177e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ----------------- | 178e41f4b71Sopenharmony_ci| length | number | Yes | No | Number of elements in an ArkTS array.| 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci### constructor 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ciconstructor() 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ciA constructor used to create an empty ArkTS array. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**Error codes** 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci| ID| Error Message | 198e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 199e41f4b71Sopenharmony_ci| 10200012 | The Array's constructor cannot be directly invoked. | 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**Example** 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci```ts 204e41f4b71Sopenharmony_cilet array = new collections.Array<number>(); 205e41f4b71Sopenharmony_ci``` 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci### constructor 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ciconstructor(first: T, ...left: T[]) 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ciA constructor used to create an ArkTS array with the given elements. 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci**Parameters** 218e41f4b71Sopenharmony_ci 219e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 220e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ------------------------------- | 221e41f4b71Sopenharmony_ci| first | T | Yes | First element to be included in the ArkTS array.| 222e41f4b71Sopenharmony_ci| left | T[] | No | Remaining elements to be included in the ArkTS array. | 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci**Error codes** 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci| ID| Error Message | 229e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 230e41f4b71Sopenharmony_ci| 10200012 | The Array's constructor cannot be directly invoked. | 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**Example** 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci```ts 235e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4); 236e41f4b71Sopenharmony_ci``` 237e41f4b71Sopenharmony_ci### constructor 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ciconstructor(...items: T[]) 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ciA constructor used to create an ArkTS array with the given elements. 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci**Parameters** 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 250e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ------------------------------- | 251e41f4b71Sopenharmony_ci| items | T[] | No | Elements to be included in the ArkTS array. | 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**Error codes** 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci| ID| Error Message | 258e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 259e41f4b71Sopenharmony_ci| 401 | Parameter error. | 260e41f4b71Sopenharmony_ci| 10200012 | The Array's constructor cannot be directly invoked. | 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**Example** 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci```ts 265e41f4b71Sopenharmony_cilet arrayPara = [1,2,3]; 266e41f4b71Sopenharmony_cilet array = new collections.Array<number>(...arrayPara); 267e41f4b71Sopenharmony_ci``` 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci### create 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_cistatic create\<T>(arrayLength: number, initialValue: T): Array\<T> 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ciCreates an ArkTS array of a fixed length, with each element set to a given initial value. 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 278e41f4b71Sopenharmony_ci 279e41f4b71Sopenharmony_ci**Parameters** 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 282e41f4b71Sopenharmony_ci| --------- | ------------- | ---- | ------------------------------- | 283e41f4b71Sopenharmony_ci| arrayLength | number | Yes | Length of the ArkTS array.| 284e41f4b71Sopenharmony_ci| initialValue | T | Yes | Initial value of each element in the ArkTS array.| 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci**Return value** 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci| Type | Description | 289e41f4b71Sopenharmony_ci| --------- | ----------------------- | 290e41f4b71Sopenharmony_ci| Array\<T> | Newly created ArkTS array.| 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**Error codes** 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci| ID| Error Message | 297e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 298e41f4b71Sopenharmony_ci| 10200011 | The create method cannot be bound. | 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci**Example** 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci```ts 303e41f4b71Sopenharmony_cilet array = collections.Array.create<number>(3, 10); // [10, 10, 10] 304e41f4b71Sopenharmony_ci``` 305e41f4b71Sopenharmony_ci 306e41f4b71Sopenharmony_ci### from 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_cistatic from\<T>(arrayLike: ArrayLike\<T>): Array\<T> 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ciCreates an ArkTS array from an array-like object. 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci**Parameters** 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 319e41f4b71Sopenharmony_ci| --------- | ------------- | ---- | ------------------------------- | 320e41f4b71Sopenharmony_ci| arrayLike | ArrayLike\<T> | Yes | Array-like object.| 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci**Return value** 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci| Type | Description | 325e41f4b71Sopenharmony_ci| --------- | ----------------------- | 326e41f4b71Sopenharmony_ci| Array\<T> | Newly created ArkTS array.| 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci**Error codes** 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci| ID| Error Message | 333e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 334e41f4b71Sopenharmony_ci| 10200011 | The from method cannot be bound. | 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci**Example** 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci```ts 339e41f4b71Sopenharmony_ci// Positive example: 340e41f4b71Sopenharmony_cilet array: Array<string> = ['str1', 'str2', 'str3']; // Native Array<T>, where T is the sendable data type. 341e41f4b71Sopenharmony_cilet sendableArray = collections.Array.from<string>(array); // Returns Sendable Array<T>. 342e41f4b71Sopenharmony_ci``` 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci<!--code_no_check--> 345e41f4b71Sopenharmony_ci```ts 346e41f4b71Sopenharmony_ci// Negative example: 347e41f4b71Sopenharmony_cilet array: Array<Array<string>> = [['str1', 'str2', 'str3'], ['str4', 'str5', 'str6'], ['str7', 'str8', 'str9']]; // Native Array<T>, where T is a non-sendable data type. 348e41f4b71Sopenharmony_cilet sendableArray = collections.Array.from<Array<string>>(array); // Prints the following exception information: Parameter error.Only accept sendable value 349e41f4b71Sopenharmony_ci``` 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci### pop 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_cipop(): T | undefined 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ciRemoves the last element from this ArkTS array and returns that element. If the array is empty, **undefined** is returned and the array does not change. 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci**Return value** 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci| Type | Description | 364e41f4b71Sopenharmony_ci| -------------- | --------------------------------------------------- | 365e41f4b71Sopenharmony_ci| T \| undefined | Element removed. If the array is empty, **undefined** is returned.| 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci**Error codes** 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 370e41f4b71Sopenharmony_ci 371e41f4b71Sopenharmony_ci| ID| Error Message | 372e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 373e41f4b71Sopenharmony_ci| 10200011 | The pop method cannot be bound. | 374e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci**Example** 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_ci```ts 379e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3); 380e41f4b71Sopenharmony_cilet lastElement = array.pop(); // 3 is returned. The array changes to [1, 2]. 381e41f4b71Sopenharmony_ci``` 382e41f4b71Sopenharmony_ci 383e41f4b71Sopenharmony_ci### push 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_cipush(...items: T[]): number 386e41f4b71Sopenharmony_ci 387e41f4b71Sopenharmony_ciAdds one or more elements to the end of this ArkTS array and returns the new length of the array. 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci**Parameters** 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 396e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------------------------- | 397e41f4b71Sopenharmony_ci| items | T[] | Yes | Elements to add.| 398e41f4b71Sopenharmony_ci 399e41f4b71Sopenharmony_ci**Return value** 400e41f4b71Sopenharmony_ci 401e41f4b71Sopenharmony_ci| Type | Description | 402e41f4b71Sopenharmony_ci| ------ | ------------------ | 403e41f4b71Sopenharmony_ci| number | New length of the array.| 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci**Error codes** 406e41f4b71Sopenharmony_ci 407e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci| ID| Error Message | 410e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 411e41f4b71Sopenharmony_ci| 10200011 | The push method cannot be bound. | 412e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci**Example** 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci```ts 417e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3); 418e41f4b71Sopenharmony_cilet length = array.push (4, 5); // 5 is returned. The array changes to [1, 2, 3, 4, 5]. 419e41f4b71Sopenharmony_ci``` 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ci### join 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_cijoin(separator?: string): string 424e41f4b71Sopenharmony_ci 425e41f4b71Sopenharmony_ciConcatenates all elements in this ArkTS array into a string, with a given separator. 426e41f4b71Sopenharmony_ci 427e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 428e41f4b71Sopenharmony_ci 429e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci**Parameters** 432e41f4b71Sopenharmony_ci 433e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 434e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------------------------- | 435e41f4b71Sopenharmony_ci| separator | string | No | Separator to be used. If no value is passed in, a comma (,) is used as the separator.| 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_ci**Return value** 438e41f4b71Sopenharmony_ci 439e41f4b71Sopenharmony_ci| Type | Description | 440e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | 441e41f4b71Sopenharmony_ci| string | String obtained. If the array is empty, an empty string is returned.| 442e41f4b71Sopenharmony_ci 443e41f4b71Sopenharmony_ci**Error codes** 444e41f4b71Sopenharmony_ci 445e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci| ID| Error Message | 448e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 449e41f4b71Sopenharmony_ci| 10200011 | The join method cannot be bound. | 450e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci**Example** 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ci```ts 455e41f4b71Sopenharmony_cilet array = new collections.Array<string>('a', 'b', 'c'); 456e41f4b71Sopenharmony_cilet joinedString = array.join('-'); // "a-b-c" is returned. 457e41f4b71Sopenharmony_ci``` 458e41f4b71Sopenharmony_ci 459e41f4b71Sopenharmony_ci### shift 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_cishift(): T | undefined 462e41f4b71Sopenharmony_ci 463e41f4b71Sopenharmony_ciRemoves the first element from this ArkTS array and returns that element. If the array is empty, **undefined** is returned and the array does not change. 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 468e41f4b71Sopenharmony_ci 469e41f4b71Sopenharmony_ci**Return value** 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ci| Type | Description | 472e41f4b71Sopenharmony_ci| -------------- | --------------------------------------------------- | 473e41f4b71Sopenharmony_ci| T \| undefined | Element removed. If the array is empty, **undefined** is returned.| 474e41f4b71Sopenharmony_ci 475e41f4b71Sopenharmony_ci**Error codes** 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 478e41f4b71Sopenharmony_ci 479e41f4b71Sopenharmony_ci| ID| Error Message | 480e41f4b71Sopenharmony_ci| -------- | --------------------------------- | 481e41f4b71Sopenharmony_ci| 10200011 | The shift method cannot be bound. | 482e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 483e41f4b71Sopenharmony_ci 484e41f4b71Sopenharmony_ci**Example** 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci```ts 487e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3); 488e41f4b71Sopenharmony_cilet firstElement = array.shift(); // 1 is returned. The array changes to [2, 3]. 489e41f4b71Sopenharmony_ci``` 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci### unshift 492e41f4b71Sopenharmony_ci 493e41f4b71Sopenharmony_ciunshift(...items: T[]): number 494e41f4b71Sopenharmony_ci 495e41f4b71Sopenharmony_ciAdds one or more elements to the beginning of this ArkTS array and returns the new length of the array. 496e41f4b71Sopenharmony_ci 497e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 498e41f4b71Sopenharmony_ci 499e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 500e41f4b71Sopenharmony_ci 501e41f4b71Sopenharmony_ci**Parameters** 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 504e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ------------------------ | 505e41f4b71Sopenharmony_ci| items | T[] | Yes | Elements to add.| 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci**Return value** 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ci| Type | Description | 510e41f4b71Sopenharmony_ci| ------ | -------------- | 511e41f4b71Sopenharmony_ci| number | New length of the array.| 512e41f4b71Sopenharmony_ci 513e41f4b71Sopenharmony_ci**Error codes** 514e41f4b71Sopenharmony_ci 515e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 516e41f4b71Sopenharmony_ci 517e41f4b71Sopenharmony_ci| ID| Error Message | 518e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 519e41f4b71Sopenharmony_ci| 10200011 | The unshift method cannot be bound. | 520e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 521e41f4b71Sopenharmony_ci 522e41f4b71Sopenharmony_ci**Example** 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ci```ts 525e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3); 526e41f4b71Sopenharmony_cilet newLength = array.unshift(0); // 4 is returned. The array changes to [0, 1, 2, 3]. 527e41f4b71Sopenharmony_ci``` 528e41f4b71Sopenharmony_ci 529e41f4b71Sopenharmony_ci### slice 530e41f4b71Sopenharmony_ci 531e41f4b71Sopenharmony_cislice(start?: number, end?: number): Array\<T> 532e41f4b71Sopenharmony_ci 533e41f4b71Sopenharmony_ciSelects a range of elements in this ArkTS array to create an array. 534e41f4b71Sopenharmony_ci 535e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 536e41f4b71Sopenharmony_ci 537e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 538e41f4b71Sopenharmony_ci 539e41f4b71Sopenharmony_ci**Parameters** 540e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 541e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 542e41f4b71Sopenharmony_ci| start | number | No | Start index of the range. If a negative number is passed in, it refers to the index of **start + array.length**. The default value is **0**. | 543e41f4b71Sopenharmony_ci| end | number | No | End index of the range (exclusive). If a negative number is passed in, it refers to the index of **end + array.length**. The default value is the length of the ArkTS array.| 544e41f4b71Sopenharmony_ci 545e41f4b71Sopenharmony_ci**Return value** 546e41f4b71Sopenharmony_ci 547e41f4b71Sopenharmony_ci| Type | Description | 548e41f4b71Sopenharmony_ci| --------- | -------------------------- | 549e41f4b71Sopenharmony_ci| Array\<T> | New array containing the selected elements.| 550e41f4b71Sopenharmony_ci 551e41f4b71Sopenharmony_ci**Error codes** 552e41f4b71Sopenharmony_ci 553e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 554e41f4b71Sopenharmony_ci 555e41f4b71Sopenharmony_ci| ID| Error Message | 556e41f4b71Sopenharmony_ci| -------- | --------------------------------- | 557e41f4b71Sopenharmony_ci| 10200011 | The slice method cannot be bound. | 558e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 559e41f4b71Sopenharmony_ci 560e41f4b71Sopenharmony_ci**Example** 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci```ts 563e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 564e41f4b71Sopenharmony_cilet slicedArray = array.slice (1, 3); // [2, 3] is returned. The original array remains unchanged. 565e41f4b71Sopenharmony_ci``` 566e41f4b71Sopenharmony_ci 567e41f4b71Sopenharmony_ci### sort 568e41f4b71Sopenharmony_ci 569e41f4b71Sopenharmony_cisort(compareFn?: (a: T, b: T) => number): Array\<T> 570e41f4b71Sopenharmony_ci 571e41f4b71Sopenharmony_ciSorts elements in this ArkTS array and returns a new array. 572e41f4b71Sopenharmony_ci 573e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 574e41f4b71Sopenharmony_ci 575e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 576e41f4b71Sopenharmony_ci 577e41f4b71Sopenharmony_ci**Parameters** 578e41f4b71Sopenharmony_ci 579e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 580e41f4b71Sopenharmony_ci| --------- | ---------------------- | ---- | ------------------------------------------ | 581e41f4b71Sopenharmony_ci| compareFn | (a: T, b: T) => number | No | Function that determines the sort order. By default, elements are sorted in ascending order.| 582e41f4b71Sopenharmony_ci 583e41f4b71Sopenharmony_ci**Return value** 584e41f4b71Sopenharmony_ci 585e41f4b71Sopenharmony_ci| Type | Description | 586e41f4b71Sopenharmony_ci| --------- | -------------- | 587e41f4b71Sopenharmony_ci| Array\<T> | Array with the sorted elements.| 588e41f4b71Sopenharmony_ci 589e41f4b71Sopenharmony_ci**Error codes** 590e41f4b71Sopenharmony_ci 591e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 592e41f4b71Sopenharmony_ci 593e41f4b71Sopenharmony_ci| ID| Error Message | 594e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 595e41f4b71Sopenharmony_ci| 10200011 | The sort method cannot be bound. | 596e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 597e41f4b71Sopenharmony_ci 598e41f4b71Sopenharmony_ci**Example** 599e41f4b71Sopenharmony_ci 600e41f4b71Sopenharmony_ci```ts 601e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 3, 5, 4, 2); 602e41f4b71Sopenharmony_ciarray.sort((a: number, b: number) => a - b); // [1, 2, 3, 4, 5] 603e41f4b71Sopenharmony_ciarray.sort((a: number, b: number) => b - a); // [5, 4, 3, 2, 1] 604e41f4b71Sopenharmony_ci``` 605e41f4b71Sopenharmony_ci 606e41f4b71Sopenharmony_ci### indexOf 607e41f4b71Sopenharmony_ci 608e41f4b71Sopenharmony_ciindexOf(searchElement: T, fromIndex?: number): number 609e41f4b71Sopenharmony_ci 610e41f4b71Sopenharmony_ciReturns the index of the first occurrence of a value in this ArkTS Array. If the value is not found, **-1** is returned. 611e41f4b71Sopenharmony_ci 612e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 613e41f4b71Sopenharmony_ci 614e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 615e41f4b71Sopenharmony_ci 616e41f4b71Sopenharmony_ci**Parameters** 617e41f4b71Sopenharmony_ci 618e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 619e41f4b71Sopenharmony_ci| ------------- | ------ | ---- | --------------------------- | 620e41f4b71Sopenharmony_ci| searchElement | T | Yes | Value to search for. | 621e41f4b71Sopenharmony_ci| fromIndex | number | No | Index from which the search starts. The default value is **0**.| 622e41f4b71Sopenharmony_ci 623e41f4b71Sopenharmony_ci**Return value** 624e41f4b71Sopenharmony_ci 625e41f4b71Sopenharmony_ci| Type | Description | 626e41f4b71Sopenharmony_ci| ------ | ---------------------------------------------- | 627e41f4b71Sopenharmony_ci| number | Index of the first occurrence of the value. If the value is not found, **-1** is returned.| 628e41f4b71Sopenharmony_ci 629e41f4b71Sopenharmony_ci**Error codes** 630e41f4b71Sopenharmony_ci 631e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 632e41f4b71Sopenharmony_ci 633e41f4b71Sopenharmony_ci| ID| Error Message | 634e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 635e41f4b71Sopenharmony_ci| 10200011 | The indexOf method cannot be bound. | 636e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 637e41f4b71Sopenharmony_ci 638e41f4b71Sopenharmony_ci**Example** 639e41f4b71Sopenharmony_ci 640e41f4b71Sopenharmony_ci```ts 641e41f4b71Sopenharmony_cilet array = new collections.Array<string>('a', 'b', 'c'); 642e41f4b71Sopenharmony_cilet index = array.indexOf('b'); // 1 is returned because 'b' is at index 1. 643e41f4b71Sopenharmony_ci``` 644e41f4b71Sopenharmony_ci 645e41f4b71Sopenharmony_ci### forEach 646e41f4b71Sopenharmony_ci 647e41f4b71Sopenharmony_ciforEach(callbackFn: (value: T, index: number, array: Array\<T>) => void): void 648e41f4b71Sopenharmony_ci 649e41f4b71Sopenharmony_ciCalls a callback function for each element in this ArkTS Array. 650e41f4b71Sopenharmony_ci 651e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 654e41f4b71Sopenharmony_ci 655e41f4b71Sopenharmony_ci**Parameters** 656e41f4b71Sopenharmony_ci 657e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 658e41f4b71Sopenharmony_ci| ---------- | --------------------------------------------------- | ---- | ------------------------------ | 659e41f4b71Sopenharmony_ci| callbackFn | (value: T, index: number, array: Array\<T>) => void | Yes | Callback function to run for each element.| 660e41f4b71Sopenharmony_ci 661e41f4b71Sopenharmony_ci**Error codes** 662e41f4b71Sopenharmony_ci 663e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 664e41f4b71Sopenharmony_ci 665e41f4b71Sopenharmony_ci| ID| Error Message | 666e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 667e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound. | 668e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 669e41f4b71Sopenharmony_ci 670e41f4b71Sopenharmony_ci**Example** 671e41f4b71Sopenharmony_ci 672e41f4b71Sopenharmony_ci```ts 673e41f4b71Sopenharmony_cilet array = new collections.Array<string>('a', 'b', 'c'); 674e41f4b71Sopenharmony_ciarray.forEach((value, index, array) => { 675e41f4b71Sopenharmony_ci console.info(`Element ${value} at index ${index}`); 676e41f4b71Sopenharmony_ci}); 677e41f4b71Sopenharmony_ci``` 678e41f4b71Sopenharmony_ci 679e41f4b71Sopenharmony_ci### map 680e41f4b71Sopenharmony_ci 681e41f4b71Sopenharmony_cimap\<U>(callbackFn: (value: T, index: number, array: Array\<T>) => U): Array\<U> 682e41f4b71Sopenharmony_ci 683e41f4b71Sopenharmony_ciCalls a callback function for each element in this ArkTS Array and returns a new array that contains the result of the callback function. 684e41f4b71Sopenharmony_ci 685e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 686e41f4b71Sopenharmony_ci 687e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 688e41f4b71Sopenharmony_ci 689e41f4b71Sopenharmony_ci**Parameters** 690e41f4b71Sopenharmony_ci 691e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 692e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------ | ---- | ------------------------------ | 693e41f4b71Sopenharmony_ci| callbackFn | (value: T, index: number, array: Array\<T>) => U | Yes | Callback function to run for each element.| 694e41f4b71Sopenharmony_ci 695e41f4b71Sopenharmony_ci**Return value** 696e41f4b71Sopenharmony_ci 697e41f4b71Sopenharmony_ci| Type | Description | 698e41f4b71Sopenharmony_ci| --------- | -------------------------- | 699e41f4b71Sopenharmony_ci| Array\<U> | New array containing the result of the callback function.| 700e41f4b71Sopenharmony_ci 701e41f4b71Sopenharmony_ci**Error codes** 702e41f4b71Sopenharmony_ci 703e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 704e41f4b71Sopenharmony_ci 705e41f4b71Sopenharmony_ci| ID| Error Message | 706e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 707e41f4b71Sopenharmony_ci| 10200011 | The map method cannot be bound. | 708e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 709e41f4b71Sopenharmony_ci 710e41f4b71Sopenharmony_ci**Example** 711e41f4b71Sopenharmony_ci 712e41f4b71Sopenharmony_ci```ts 713e41f4b71Sopenharmony_ci// Convert each string element in the original array to uppercase and return a new array containing the new strings. 714e41f4b71Sopenharmony_cilet array = new collections.Array<string>('a', 'b', 'c'); 715e41f4b71Sopenharmony_cilet mappedArray = array.map((value, index, array) => { 716e41f4b71Sopenharmony_ci return value.toUpperCase(); // Convert each string element to uppercase. 717e41f4b71Sopenharmony_ci}); 718e41f4b71Sopenharmony_ciconsole.info("" + mappedArray); // ['A', 'B', 'C'] is returned. 719e41f4b71Sopenharmony_ci``` 720e41f4b71Sopenharmony_ci 721e41f4b71Sopenharmony_ci### filter 722e41f4b71Sopenharmony_ci 723e41f4b71Sopenharmony_cifilter(predicate: (value: T, index: number, array: Array\<T>) => boolean): Array\<T> 724e41f4b71Sopenharmony_ci 725e41f4b71Sopenharmony_ciReturns a new array containing all elements that pass a test provided by a callback function. 726e41f4b71Sopenharmony_ci 727e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 728e41f4b71Sopenharmony_ci 729e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 730e41f4b71Sopenharmony_ci 731e41f4b71Sopenharmony_ci**Parameters** 732e41f4b71Sopenharmony_ci 733e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 734e41f4b71Sopenharmony_ci| --------- | ------------------------------------------------------ | ---- | ------------------------------------------------------------ | 735e41f4b71Sopenharmony_ci| predicate | (value: T, index: number, array: Array\<T>) => boolean | Yes | Function that takes three arguments. It is used to filter elements.| 736e41f4b71Sopenharmony_ci 737e41f4b71Sopenharmony_ci**Return value** 738e41f4b71Sopenharmony_ci 739e41f4b71Sopenharmony_ci| Type | Description | 740e41f4b71Sopenharmony_ci| --------- | ---------------------------- | 741e41f4b71Sopenharmony_ci| Array\<T> | New array containing elements that pass the test.| 742e41f4b71Sopenharmony_ci 743e41f4b71Sopenharmony_ci**Error codes** 744e41f4b71Sopenharmony_ci 745e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 746e41f4b71Sopenharmony_ci 747e41f4b71Sopenharmony_ci| ID| Error Message | 748e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 749e41f4b71Sopenharmony_ci| 10200011 | The filter method cannot be bound. | 750e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 751e41f4b71Sopenharmony_ci 752e41f4b71Sopenharmony_ci**Example** 753e41f4b71Sopenharmony_ci 754e41f4b71Sopenharmony_ci```ts 755e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 756e41f4b71Sopenharmony_cilet filteredArray = array.filter((value: number) => value % 2 === 0); // [2, 4] is returned. This new array contains only even numbers. 757e41f4b71Sopenharmony_ci``` 758e41f4b71Sopenharmony_ci 759e41f4b71Sopenharmony_ci### reduce 760e41f4b71Sopenharmony_ci 761e41f4b71Sopenharmony_cireduce(callbackFn: (previousValue: T, currentValue: T, currentIndex: number, array: Array\<T>) => T): T 762e41f4b71Sopenharmony_ci 763e41f4b71Sopenharmony_ciCalls a callback function for each element in this ArkTS array, uses the previous return value of the function as an accumulated value, and returns the final result. 764e41f4b71Sopenharmony_ci 765e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 766e41f4b71Sopenharmony_ci 767e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 768e41f4b71Sopenharmony_ci 769e41f4b71Sopenharmony_ci**Parameters** 770e41f4b71Sopenharmony_ci 771e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 772e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 773e41f4b71Sopenharmony_ci| callbackFn | (previousValue: T, currentValue: T, currentIndex: number, array: Array\<T>) => T | Yes | Function that takes four arguments. It performs an operation on each element and passes the result as an accumulated value to the next element.| 774e41f4b71Sopenharmony_ci 775e41f4b71Sopenharmony_ci**Return value** 776e41f4b71Sopenharmony_ci 777e41f4b71Sopenharmony_ci| Type| Description | 778e41f4b71Sopenharmony_ci| ---- | -------------------------- | 779e41f4b71Sopenharmony_ci| T | Final result obtained from the last call of the callback function.| 780e41f4b71Sopenharmony_ci 781e41f4b71Sopenharmony_ci**Error codes** 782e41f4b71Sopenharmony_ci 783e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 784e41f4b71Sopenharmony_ci 785e41f4b71Sopenharmony_ci| ID| Error Message | 786e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 787e41f4b71Sopenharmony_ci| 10200011 | The reduce method cannot be bound. | 788e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 789e41f4b71Sopenharmony_ci 790e41f4b71Sopenharmony_ci**Example** 791e41f4b71Sopenharmony_ci 792e41f4b71Sopenharmony_ci```ts 793e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 794e41f4b71Sopenharmony_cilet reducedValue = array.reduce((accumulator, value) => accumulator + value); // 15, which is the final accumulated result of all elements, is returned. 795e41f4b71Sopenharmony_ci``` 796e41f4b71Sopenharmony_ci 797e41f4b71Sopenharmony_ci### reduce 798e41f4b71Sopenharmony_ci 799e41f4b71Sopenharmony_cireduce\<U>(callbackFn: (previousValue: U, currentValue: T, currentIndex: number, array: Array\<T>) => U, initialValue: U): U 800e41f4b71Sopenharmony_ci 801e41f4b71Sopenharmony_ciSimilar to the previous API, this API takes an initial value as the second parameter to initialize the accumulator before the array traversal starts. 802e41f4b71Sopenharmony_ci 803e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 804e41f4b71Sopenharmony_ci 805e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 806e41f4b71Sopenharmony_ci 807e41f4b71Sopenharmony_ci**Parameters** 808e41f4b71Sopenharmony_ci 809e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 810e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 811e41f4b71Sopenharmony_ci| callbackFn | callbackFn: (previousValue: U, currentValue: T, currentIndex: number, array: Array\<T>) => U | Yes | Function that takes four arguments. It performs an operation on each element and passes the result as an accumulated value to the next element.| 812e41f4b71Sopenharmony_ci| initialValue | U | Yes | Initial value of the accumulator. | 813e41f4b71Sopenharmony_ci 814e41f4b71Sopenharmony_ci**Return value** 815e41f4b71Sopenharmony_ci 816e41f4b71Sopenharmony_ci| Type| Description | 817e41f4b71Sopenharmony_ci| ---- | -------------------------- | 818e41f4b71Sopenharmony_ci| U | Final result obtained from the last call of the callback function.| 819e41f4b71Sopenharmony_ci 820e41f4b71Sopenharmony_ci**Error codes** 821e41f4b71Sopenharmony_ci 822e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 823e41f4b71Sopenharmony_ci 824e41f4b71Sopenharmony_ci| ID| Error Message | 825e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 826e41f4b71Sopenharmony_ci| 10200011 | The reduce method cannot be bound. | 827e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 828e41f4b71Sopenharmony_ci 829e41f4b71Sopenharmony_ci**Example** 830e41f4b71Sopenharmony_ci 831e41f4b71Sopenharmony_ci```ts 832e41f4b71Sopenharmony_ci// An accumulator with the initial value 0 is used. The accumulator is used to calculate the sum of all elements in the array and return the sum. 833e41f4b71Sopenharmony_cilet array = new collections.Array(1, 2, 3, 4, 5); 834e41f4b71Sopenharmony_cilet reducedValue = array.reduce<number>((accumulator: number, value: number) => accumulator + value, 0); // 15, which is the final accumulated result of all elements, is returned. The initial value is 0. 835e41f4b71Sopenharmony_ci``` 836e41f4b71Sopenharmony_ci 837e41f4b71Sopenharmony_ci### at 838e41f4b71Sopenharmony_ci 839e41f4b71Sopenharmony_ciat(index: number): T | undefined 840e41f4b71Sopenharmony_ci 841e41f4b71Sopenharmony_ciReturns the element at a given index in this ArkTS array. 842e41f4b71Sopenharmony_ci 843e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 844e41f4b71Sopenharmony_ci 845e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 846e41f4b71Sopenharmony_ci 847e41f4b71Sopenharmony_ci**Parameters** 848e41f4b71Sopenharmony_ci 849e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 850e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 851e41f4b71Sopenharmony_ci| index | number | Yes | Index of the element. The index in an array always starts from 0 and is an integer. If a negative number is passed in, it refers to the index of **index + array.length**.| 852e41f4b71Sopenharmony_ci 853e41f4b71Sopenharmony_ci 854e41f4b71Sopenharmony_ci**Return value** 855e41f4b71Sopenharmony_ci 856e41f4b71Sopenharmony_ci| Type | Description | 857e41f4b71Sopenharmony_ci| -------------- | ------------------------------------------------------------ | 858e41f4b71Sopenharmony_ci| T \| undefined | Element at the given index. If the index is out of range or invalid, **undefined** is returned.| 859e41f4b71Sopenharmony_ci 860e41f4b71Sopenharmony_ci**Error codes** 861e41f4b71Sopenharmony_ci 862e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 863e41f4b71Sopenharmony_ci 864e41f4b71Sopenharmony_ci| ID| Error Message | 865e41f4b71Sopenharmony_ci| -------- | ------------------------------ | 866e41f4b71Sopenharmony_ci| 10200011 | The at method cannot be bound. | 867e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 868e41f4b71Sopenharmony_ci 869e41f4b71Sopenharmony_ci**Example** 870e41f4b71Sopenharmony_ci 871e41f4b71Sopenharmony_ci```ts 872e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 873e41f4b71Sopenharmony_cilet elementAtIndex = array.at(2); // 3 is returned. This is because the index starts from 0. 874e41f4b71Sopenharmony_ci``` 875e41f4b71Sopenharmony_ci 876e41f4b71Sopenharmony_ci### entries 877e41f4b71Sopenharmony_ci 878e41f4b71Sopenharmony_cientries(): IterableIterator<[number, T]> 879e41f4b71Sopenharmony_ci 880e41f4b71Sopenharmony_ciReturns an iterator object that contains the key-value pair of each element in this ArkTS array. 881e41f4b71Sopenharmony_ci 882e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 883e41f4b71Sopenharmony_ci 884e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 885e41f4b71Sopenharmony_ci 886e41f4b71Sopenharmony_ci**Return value** 887e41f4b71Sopenharmony_ci 888e41f4b71Sopenharmony_ci| Type | Description | 889e41f4b71Sopenharmony_ci| ----------------------------- | ------------------------------------------ | 890e41f4b71Sopenharmony_ci| IterableIterator<[number, T]> | Iterator object that contains the key-value pair of each element in the array.| 891e41f4b71Sopenharmony_ci 892e41f4b71Sopenharmony_ci**Error codes** 893e41f4b71Sopenharmony_ci 894e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 895e41f4b71Sopenharmony_ci 896e41f4b71Sopenharmony_ci| ID| Error Message | 897e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 898e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound. | 899e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 900e41f4b71Sopenharmony_ci 901e41f4b71Sopenharmony_ci**Example** 902e41f4b71Sopenharmony_ci 903e41f4b71Sopenharmony_ci```ts 904e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 905e41f4b71Sopenharmony_cilet iterator = array.entries(); 906e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); // [0, 1], key-value pair of the first element is returned. 907e41f4b71Sopenharmony_ci``` 908e41f4b71Sopenharmony_ci 909e41f4b71Sopenharmony_ci### keys 910e41f4b71Sopenharmony_ci 911e41f4b71Sopenharmony_cikeys(): IterableIterator\<number> 912e41f4b71Sopenharmony_ci 913e41f4b71Sopenharmony_ciReturns an iterator object that contains the key of each element in this ArkTS array. 914e41f4b71Sopenharmony_ci 915e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 916e41f4b71Sopenharmony_ci 917e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 918e41f4b71Sopenharmony_ci 919e41f4b71Sopenharmony_ci**Return value** 920e41f4b71Sopenharmony_ci 921e41f4b71Sopenharmony_ci| Type | Description | 922e41f4b71Sopenharmony_ci| ------------------------- | -------------------------------------- | 923e41f4b71Sopenharmony_ci| IterableIterator\<number> | Iterator object that contains the key of each element in the array.| 924e41f4b71Sopenharmony_ci 925e41f4b71Sopenharmony_ci**Error codes** 926e41f4b71Sopenharmony_ci 927e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 928e41f4b71Sopenharmony_ci 929e41f4b71Sopenharmony_ci| ID| Error Message | 930e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 931e41f4b71Sopenharmony_ci| 10200011 | The keys method cannot be bound. | 932e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 933e41f4b71Sopenharmony_ci 934e41f4b71Sopenharmony_ci**Example** 935e41f4b71Sopenharmony_ci 936e41f4b71Sopenharmony_ci```ts 937e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 938e41f4b71Sopenharmony_cilet iterator = array.keys(); 939e41f4b71Sopenharmony_cifor (const key of iterator) { 940e41f4b71Sopenharmony_ci console.info("" + key); // 0, 1, 2, 3, and 4 are returned in sequence. 941e41f4b71Sopenharmony_ci} 942e41f4b71Sopenharmony_ci``` 943e41f4b71Sopenharmony_ci 944e41f4b71Sopenharmony_ci### values 945e41f4b71Sopenharmony_ci 946e41f4b71Sopenharmony_civalues(): IterableIterator\<T> 947e41f4b71Sopenharmony_ci 948e41f4b71Sopenharmony_ciReturns an iterator object that contains the value of each element in this ArkTS array. 949e41f4b71Sopenharmony_ci 950e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 951e41f4b71Sopenharmony_ci 952e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 953e41f4b71Sopenharmony_ci 954e41f4b71Sopenharmony_ci**Return value** 955e41f4b71Sopenharmony_ci 956e41f4b71Sopenharmony_ci| Type | Description | 957e41f4b71Sopenharmony_ci| -------------------- | -------------------------------------- | 958e41f4b71Sopenharmony_ci| IterableIterator\<T> | Iterator object that contains the value of each element in the array.| 959e41f4b71Sopenharmony_ci 960e41f4b71Sopenharmony_ci**Error codes** 961e41f4b71Sopenharmony_ci 962e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 963e41f4b71Sopenharmony_ci 964e41f4b71Sopenharmony_ci| ID| Error Message | 965e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 966e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound. | 967e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 968e41f4b71Sopenharmony_ci 969e41f4b71Sopenharmony_ci**Example** 970e41f4b71Sopenharmony_ci 971e41f4b71Sopenharmony_ci```ts 972e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 973e41f4b71Sopenharmony_cilet iterator = array.values(); 974e41f4b71Sopenharmony_cifor(const value of iterator) { 975e41f4b71Sopenharmony_ci console.info("" + value); // 1, 2, 3, 4, and 5 are returned in sequence. 976e41f4b71Sopenharmony_ci} 977e41f4b71Sopenharmony_ci``` 978e41f4b71Sopenharmony_ci 979e41f4b71Sopenharmony_ci### find 980e41f4b71Sopenharmony_ci 981e41f4b71Sopenharmony_cifind(predicate: (value: T, index: number, obj: Array\<T>) => boolean): T | undefined 982e41f4b71Sopenharmony_ci 983e41f4b71Sopenharmony_ciReturns the value of the first element that passes a test provided by a callback function. If none of the elements pass the test, **undefined** is returned. 984e41f4b71Sopenharmony_ci 985e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 986e41f4b71Sopenharmony_ci 987e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 988e41f4b71Sopenharmony_ci 989e41f4b71Sopenharmony_ci**Parameters** 990e41f4b71Sopenharmony_ci 991e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 992e41f4b71Sopenharmony_ci| --------- | ---------------------------------------------------- | ---- | ------------------------------------------------------ | 993e41f4b71Sopenharmony_ci| predicate | (value: T, index: number, obj: Array\<T>) => boolean | Yes | Function that takes three arguments. It is used to filter elements.| 994e41f4b71Sopenharmony_ci 995e41f4b71Sopenharmony_ci**Return value** 996e41f4b71Sopenharmony_ci 997e41f4b71Sopenharmony_ci| Type | Description | 998e41f4b71Sopenharmony_ci| -------------- | ------------------------------------------------------------ | 999e41f4b71Sopenharmony_ci| T \| undefined | Value of the first element that passes the test. If none of the elements pass the test, **undefined** is returned.| 1000e41f4b71Sopenharmony_ci 1001e41f4b71Sopenharmony_ci**Error codes** 1002e41f4b71Sopenharmony_ci 1003e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1004e41f4b71Sopenharmony_ci 1005e41f4b71Sopenharmony_ci| ID| Error Message | 1006e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 1007e41f4b71Sopenharmony_ci| 10200011 | The find method cannot be bound. | 1008e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1009e41f4b71Sopenharmony_ci 1010e41f4b71Sopenharmony_ci**Example** 1011e41f4b71Sopenharmony_ci 1012e41f4b71Sopenharmony_ci```ts 1013e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 1014e41f4b71Sopenharmony_cilet foundValue = array.find((value: number) => value % 2 === 0); // 2, the first even element, is returned. 1015e41f4b71Sopenharmony_ci``` 1016e41f4b71Sopenharmony_ci 1017e41f4b71Sopenharmony_ci### includes 1018e41f4b71Sopenharmony_ci 1019e41f4b71Sopenharmony_ciincludes(searchElement: T, fromIndex?: number): boolean 1020e41f4b71Sopenharmony_ci 1021e41f4b71Sopenharmony_ciChecks whether this ArkTS array contains an element and returns a Boolean value. 1022e41f4b71Sopenharmony_ci 1023e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1024e41f4b71Sopenharmony_ci 1025e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1026e41f4b71Sopenharmony_ci 1027e41f4b71Sopenharmony_ci**Parameters** 1028e41f4b71Sopenharmony_ci 1029e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1030e41f4b71Sopenharmony_ci| ------------- | ------ | ---- | --------------------------- | 1031e41f4b71Sopenharmony_ci| searchElement | T | Yes | Element to search for. | 1032e41f4b71Sopenharmony_ci| fromIndex | number | No | Index from which the search starts. The default value is **0**.| 1033e41f4b71Sopenharmony_ci 1034e41f4b71Sopenharmony_ci**Return value** 1035e41f4b71Sopenharmony_ci 1036e41f4b71Sopenharmony_ci| Type | Description | 1037e41f4b71Sopenharmony_ci| ------- | --------------------------------------------------- | 1038e41f4b71Sopenharmony_ci| boolean | **true**: The element exists.<br>**false**: The element does not exist.| 1039e41f4b71Sopenharmony_ci 1040e41f4b71Sopenharmony_ci**Error codes** 1041e41f4b71Sopenharmony_ci 1042e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1043e41f4b71Sopenharmony_ci 1044e41f4b71Sopenharmony_ci| ID| Error Message | 1045e41f4b71Sopenharmony_ci| -------- | ------------------------------------ | 1046e41f4b71Sopenharmony_ci| 10200011 | The includes method cannot be bound. | 1047e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1048e41f4b71Sopenharmony_ci 1049e41f4b71Sopenharmony_ci**Example** 1050e41f4b71Sopenharmony_ci 1051e41f4b71Sopenharmony_ci```ts 1052e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 1053e41f4b71Sopenharmony_cilet includesResult = array.includes(3); // true is returned, because the array contains 3. 1054e41f4b71Sopenharmony_ci``` 1055e41f4b71Sopenharmony_ci 1056e41f4b71Sopenharmony_ci### findIndex 1057e41f4b71Sopenharmony_ci 1058e41f4b71Sopenharmony_cifindIndex(predicate: (value: T, index: number, obj: Array\<T>) => boolean): number 1059e41f4b71Sopenharmony_ci 1060e41f4b71Sopenharmony_ciReturns the index of the first element that passes a test provided by a callback function. If none of the elements pass the test, **-1** is returned. 1061e41f4b71Sopenharmony_ci 1062e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1063e41f4b71Sopenharmony_ci 1064e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1065e41f4b71Sopenharmony_ci 1066e41f4b71Sopenharmony_ci**Parameters** 1067e41f4b71Sopenharmony_ci 1068e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1069e41f4b71Sopenharmony_ci| --------- | ---------------------------------------------------- | ---- | ------------------------------------------------------ | 1070e41f4b71Sopenharmony_ci| predicate | (value: T, index: number, obj: Array\<T>) => boolean | Yes | Function that takes three arguments. It is used to filter elements.| 1071e41f4b71Sopenharmony_ci 1072e41f4b71Sopenharmony_ci**Return value** 1073e41f4b71Sopenharmony_ci 1074e41f4b71Sopenharmony_ci| Type | Description | 1075e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | 1076e41f4b71Sopenharmony_ci| number | Index of the first element that passes the test. If none of the elements pass the test, **-1** is returned.| 1077e41f4b71Sopenharmony_ci 1078e41f4b71Sopenharmony_ci**Error codes** 1079e41f4b71Sopenharmony_ci 1080e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1081e41f4b71Sopenharmony_ci 1082e41f4b71Sopenharmony_ci| ID| Error Message | 1083e41f4b71Sopenharmony_ci| -------- | ------------------------------------- | 1084e41f4b71Sopenharmony_ci| 10200011 | The findIndex method cannot be bound. | 1085e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1086e41f4b71Sopenharmony_ci 1087e41f4b71Sopenharmony_ci**Example** 1088e41f4b71Sopenharmony_ci 1089e41f4b71Sopenharmony_ci```ts 1090e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 1091e41f4b71Sopenharmony_cilet foundIndex = array.findIndex((value: number) => value % 2 === 0); // 1 is returned, because 2 is the first even element. 1092e41f4b71Sopenharmony_ci``` 1093e41f4b71Sopenharmony_ci 1094e41f4b71Sopenharmony_ci### fill 1095e41f4b71Sopenharmony_ci 1096e41f4b71Sopenharmony_cifill(value: T, start?: number, end?: number): Array\<T> 1097e41f4b71Sopenharmony_ci 1098e41f4b71Sopenharmony_ciFills elements in the specified range of this ArkTS array with a given value. 1099e41f4b71Sopenharmony_ci 1100e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1101e41f4b71Sopenharmony_ci 1102e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1103e41f4b71Sopenharmony_ci 1104e41f4b71Sopenharmony_ci**Parameters** 1105e41f4b71Sopenharmony_ci 1106e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1107e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------ | 1108e41f4b71Sopenharmony_ci| value | T | Yes | Value to fill in. | 1109e41f4b71Sopenharmony_ci| start | number | No | Start index of the range. The default value is **0**. | 1110e41f4b71Sopenharmony_ci| end | number | No | End index of the range (exclusive). If no value is passed in, it refers to the last element of the array.| 1111e41f4b71Sopenharmony_ci 1112e41f4b71Sopenharmony_ci**Return value** 1113e41f4b71Sopenharmony_ci 1114e41f4b71Sopenharmony_ci| Type | Description | 1115e41f4b71Sopenharmony_ci| --------- | -------------- | 1116e41f4b71Sopenharmony_ci| Array\<T> | Filled array.| 1117e41f4b71Sopenharmony_ci 1118e41f4b71Sopenharmony_ci**Error codes** 1119e41f4b71Sopenharmony_ci 1120e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1121e41f4b71Sopenharmony_ci 1122e41f4b71Sopenharmony_ci| ID| Error Message | 1123e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 1124e41f4b71Sopenharmony_ci| 10200011 | The fill method cannot be bound. | 1125e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1126e41f4b71Sopenharmony_ci 1127e41f4b71Sopenharmony_ci**Example** 1128e41f4b71Sopenharmony_ci 1129e41f4b71Sopenharmony_ci```ts 1130e41f4b71Sopenharmony_cilet array = new collections.Array(1, 2, 3, 4, 5); 1131e41f4b71Sopenharmony_ciarray.fill(0, 1, 3); // [1, 0, 0, 4, 5] is returned, because elements in the index range from 1 to 3 are filled with 0. 1132e41f4b71Sopenharmony_ci``` 1133e41f4b71Sopenharmony_ci 1134e41f4b71Sopenharmony_ci### shrinkTo 1135e41f4b71Sopenharmony_ci 1136e41f4b71Sopenharmony_cishrinkTo(arrayLength: number): void 1137e41f4b71Sopenharmony_ci 1138e41f4b71Sopenharmony_ciShrinks this ArkTS array to a given length. 1139e41f4b71Sopenharmony_ci 1140e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1141e41f4b71Sopenharmony_ci 1142e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1143e41f4b71Sopenharmony_ci 1144e41f4b71Sopenharmony_ci**Parameters** 1145e41f4b71Sopenharmony_ci 1146e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1147e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------ | 1148e41f4b71Sopenharmony_ci| arrayLength | number | Yes | New length of the array. If a value greater than or equal to the current array length is passed in, the array does not change.| 1149e41f4b71Sopenharmony_ci 1150e41f4b71Sopenharmony_ci**Error codes** 1151e41f4b71Sopenharmony_ci 1152e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1153e41f4b71Sopenharmony_ci 1154e41f4b71Sopenharmony_ci| ID| Error Message | 1155e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 1156e41f4b71Sopenharmony_ci| 10200011 | The shrinkTo method cannot be bound. | 1157e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1158e41f4b71Sopenharmony_ci 1159e41f4b71Sopenharmony_ci**Example** 1160e41f4b71Sopenharmony_ci 1161e41f4b71Sopenharmony_ci```ts 1162e41f4b71Sopenharmony_cilet array1 = new collections.Array(1, 2, 3, 4, 5); 1163e41f4b71Sopenharmony_ciarray1.shrinkTo(1); // The array is changed to [1]. 1164e41f4b71Sopenharmony_ci 1165e41f4b71Sopenharmony_cilet array2 = new collections.Array(1, 2, 3, 4, 5); 1166e41f4b71Sopenharmony_ciarray2.shrinkTo(10); // The array remains unchanged. 1167e41f4b71Sopenharmony_ci``` 1168e41f4b71Sopenharmony_ci 1169e41f4b71Sopenharmony_ci### extendTo 1170e41f4b71Sopenharmony_ci 1171e41f4b71Sopenharmony_ciextendTo(arrayLength: number, initialValue: T): void 1172e41f4b71Sopenharmony_ci 1173e41f4b71Sopenharmony_ciExtends this array to a given length by adding elements with the specified initial value. 1174e41f4b71Sopenharmony_ci 1175e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1176e41f4b71Sopenharmony_ci 1177e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1178e41f4b71Sopenharmony_ci 1179e41f4b71Sopenharmony_ci**Parameters** 1180e41f4b71Sopenharmony_ci 1181e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1182e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------ | 1183e41f4b71Sopenharmony_ci| arrayLength | number | Yes | New length of the array. If a value less than or equal to the current array length is passed in, the array does not change.| 1184e41f4b71Sopenharmony_ci| initialValue | T | Yes | Initial value of the elements to be added.| 1185e41f4b71Sopenharmony_ci 1186e41f4b71Sopenharmony_ci**Error codes** 1187e41f4b71Sopenharmony_ci 1188e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1189e41f4b71Sopenharmony_ci 1190e41f4b71Sopenharmony_ci| ID| Error Message | 1191e41f4b71Sopenharmony_ci| -------- | -------------------------------- | 1192e41f4b71Sopenharmony_ci| 10200011 | The extendTo method cannot be bound. | 1193e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1194e41f4b71Sopenharmony_ci 1195e41f4b71Sopenharmony_ci**Example** 1196e41f4b71Sopenharmony_ci 1197e41f4b71Sopenharmony_ci```ts 1198e41f4b71Sopenharmony_cilet array1 = new collections.Array(1, 2, 3); 1199e41f4b71Sopenharmony_ciarray1.extendTo (5, 10); // The array is changed to [1, 2, 3, 10, 10]. 1200e41f4b71Sopenharmony_ci 1201e41f4b71Sopenharmony_cilet array2 = new collections.Array(1, 2, 3); 1202e41f4b71Sopenharmony_ciarray2.extendTo (1, 10); // The array remains unchanged. 1203e41f4b71Sopenharmony_ci``` 1204e41f4b71Sopenharmony_ci 1205e41f4b71Sopenharmony_ci### concat 1206e41f4b71Sopenharmony_ci 1207e41f4b71Sopenharmony_ciconcat(...items: ConcatArray\<T>[]): Array\<T> 1208e41f4b71Sopenharmony_ci 1209e41f4b71Sopenharmony_ciConcatenates this ArkTS array with one or more arrays. 1210e41f4b71Sopenharmony_ci 1211e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1212e41f4b71Sopenharmony_ci 1213e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1214e41f4b71Sopenharmony_ci 1215e41f4b71Sopenharmony_ci**Parameters** 1216e41f4b71Sopenharmony_ci 1217e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1218e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------------------------- | 1219e41f4b71Sopenharmony_ci| items | ConcatArray\<T>[] | Yes | Arrays to concatenate.| 1220e41f4b71Sopenharmony_ci 1221e41f4b71Sopenharmony_ci**Return value** 1222e41f4b71Sopenharmony_ci 1223e41f4b71Sopenharmony_ci| Type| Description | 1224e41f4b71Sopenharmony_ci| ---- | ---------------------------------- | 1225e41f4b71Sopenharmony_ci| Array\<T> | New array generated.| 1226e41f4b71Sopenharmony_ci 1227e41f4b71Sopenharmony_ci**Error codes** 1228e41f4b71Sopenharmony_ci 1229e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1230e41f4b71Sopenharmony_ci 1231e41f4b71Sopenharmony_ci| ID| Error Message | 1232e41f4b71Sopenharmony_ci| ------- | -------- | 1233e41f4b71Sopenharmony_ci| 401 | Parameter error. Not a valid array. | 1234e41f4b71Sopenharmony_ci| 10200011 | The concat method cannot be bound. | 1235e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1236e41f4b71Sopenharmony_ci 1237e41f4b71Sopenharmony_ci**Example** 1238e41f4b71Sopenharmony_ci 1239e41f4b71Sopenharmony_ci```ts 1240e41f4b71Sopenharmony_cilet array = new collections.Array(1, 2, 3); 1241e41f4b71Sopenharmony_cilet array1 = new collections.Array(4, 5, 6); 1242e41f4b71Sopenharmony_cilet array2 = new collections.Array(7, 8, 9); 1243e41f4b71Sopenharmony_ci 1244e41f4b71Sopenharmony_cilet concatArray = array.concat(array1, array2); // The concatenated array is [1, 2, 3, 4, 5, 6, 7, 8, 9]. 1245e41f4b71Sopenharmony_ci``` 1246e41f4b71Sopenharmony_ci 1247e41f4b71Sopenharmony_ci### splice 1248e41f4b71Sopenharmony_ci 1249e41f4b71Sopenharmony_cisplice(start: number): Array\<T> 1250e41f4b71Sopenharmony_ci 1251e41f4b71Sopenharmony_ciRemoves elements from a specified position in an array. 1252e41f4b71Sopenharmony_ci 1253e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1254e41f4b71Sopenharmony_ci 1255e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1256e41f4b71Sopenharmony_ci 1257e41f4b71Sopenharmony_ci**Parameters** 1258e41f4b71Sopenharmony_ci 1259e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1260e41f4b71Sopenharmony_ci| ----- | ------ | -- | ------------------------------------------------------------------- | 1261e41f4b71Sopenharmony_ci| start | number | Yes| Index from which the removal starts. If -array.length =< start < 0, the removal starts from **start + array.length**. If start < -array.length, the removal starts from 0.| 1262e41f4b71Sopenharmony_ci 1263e41f4b71Sopenharmony_ci**Return value** 1264e41f4b71Sopenharmony_ci 1265e41f4b71Sopenharmony_ci| Type | Description | 1266e41f4b71Sopenharmony_ci| --------- | --------------------- | 1267e41f4b71Sopenharmony_ci| Array\<T> | **Array** object that contains the removed elements. If no element is removed, an empty **Array** object is returned.| 1268e41f4b71Sopenharmony_ci 1269e41f4b71Sopenharmony_ci**Error codes** 1270e41f4b71Sopenharmony_ci 1271e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1272e41f4b71Sopenharmony_ci 1273e41f4b71Sopenharmony_ci| ID| Error Message | 1274e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 1275e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 1276e41f4b71Sopenharmony_ci| 10200011 | The splice method cannot be bound. | 1277e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1278e41f4b71Sopenharmony_ci 1279e41f4b71Sopenharmony_ci**Example** 1280e41f4b71Sopenharmony_ci 1281e41f4b71Sopenharmony_ci```ts 1282e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 1283e41f4b71Sopenharmony_cilet removeArray = array.splice(2); // The array is changed to [1, 2], and [3, 4, 5] is returned. 1284e41f4b71Sopenharmony_ci``` 1285e41f4b71Sopenharmony_ci 1286e41f4b71Sopenharmony_ci### splice 1287e41f4b71Sopenharmony_ci 1288e41f4b71Sopenharmony_cisplice(start: number, deleteCount: number, ...items: T[]): Array\<T> 1289e41f4b71Sopenharmony_ci 1290e41f4b71Sopenharmony_ciRemoves elements from a specified position in an array, and inserts new elements from the same position. 1291e41f4b71Sopenharmony_ci 1292e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1293e41f4b71Sopenharmony_ci 1294e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1295e41f4b71Sopenharmony_ci 1296e41f4b71Sopenharmony_ci**Parameters** 1297e41f4b71Sopenharmony_ci 1298e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1299e41f4b71Sopenharmony_ci| ----------- | ------ | -- | ------------------------------------------------------------------- | 1300e41f4b71Sopenharmony_ci| start | number | Yes | Index from which the removal starts. If -array.length =< start < 0, the removal starts from **start + array.length**. If start < -array.length, the removal starts from 0.| 1301e41f4b71Sopenharmony_ci| deleteCount | number | Yes | Number of elements to remove. | 1302e41f4b71Sopenharmony_ci| items | T[] | No | New elements to insert from the **start** position. If no value is passed in, only the elements in the array are removed. | 1303e41f4b71Sopenharmony_ci 1304e41f4b71Sopenharmony_ci**Return value** 1305e41f4b71Sopenharmony_ci 1306e41f4b71Sopenharmony_ci| Type | Description | 1307e41f4b71Sopenharmony_ci| --------- | ------------------------------------ | 1308e41f4b71Sopenharmony_ci| Array\<T> | **Array** object that contains the removed elements. If no element is removed, an empty **Array** object is returned.| 1309e41f4b71Sopenharmony_ci 1310e41f4b71Sopenharmony_ci**Error codes** 1311e41f4b71Sopenharmony_ci 1312e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1313e41f4b71Sopenharmony_ci 1314e41f4b71Sopenharmony_ci| ID| Error Message | 1315e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 1316e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 1317e41f4b71Sopenharmony_ci| 10200011 | The splice method cannot be bound. | 1318e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 1319e41f4b71Sopenharmony_ci 1320e41f4b71Sopenharmony_ci**Example** 1321e41f4b71Sopenharmony_ci 1322e41f4b71Sopenharmony_ci```ts 1323e41f4b71Sopenharmony_ci// Example 1: 1324e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 1325e41f4b71Sopenharmony_cilet removeArray = array.splice(2, 2); // The array is changed to [1, 2, 5], and [3, 4] is returned. 1326e41f4b71Sopenharmony_ci``` 1327e41f4b71Sopenharmony_ci 1328e41f4b71Sopenharmony_ci```ts 1329e41f4b71Sopenharmony_ci// Example 2: 1330e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 3, 4, 5); 1331e41f4b71Sopenharmony_cilet removeArray = array.splice(2, 2, 6, 7, 8); // The array is changed to [1, 2, 6, 7, 8, 5], and [3, 4] is returned. 1332e41f4b71Sopenharmony_ci``` 1333e41f4b71Sopenharmony_ci 1334e41f4b71Sopenharmony_ci### [Symbol.iterator] 1335e41f4b71Sopenharmony_ci 1336e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<T> 1337e41f4b71Sopenharmony_ci 1338e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object. 1339e41f4b71Sopenharmony_ci 1340e41f4b71Sopenharmony_ci> **NOTE** 1341e41f4b71Sopenharmony_ci> 1342e41f4b71Sopenharmony_ci> This API cannot be used in .ets files. 1343e41f4b71Sopenharmony_ci 1344e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1345e41f4b71Sopenharmony_ci 1346e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1347e41f4b71Sopenharmony_ci 1348e41f4b71Sopenharmony_ci**Return value** 1349e41f4b71Sopenharmony_ci 1350e41f4b71Sopenharmony_ci| Type | Description | 1351e41f4b71Sopenharmony_ci| ------------------------- | ---------------- | 1352e41f4b71Sopenharmony_ci| IterableIterator<T> | Iterator obtained.| 1353e41f4b71Sopenharmony_ci 1354e41f4b71Sopenharmony_ci**Error codes** 1355e41f4b71Sopenharmony_ci 1356e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1357e41f4b71Sopenharmony_ci 1358e41f4b71Sopenharmony_ci| ID| Error Message | 1359e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | 1360e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. | 1361e41f4b71Sopenharmony_ci 1362e41f4b71Sopenharmony_ci**Example** 1363e41f4b71Sopenharmony_ci 1364e41f4b71Sopenharmony_ci```ts 1365e41f4b71Sopenharmony_cilet array= new collections.Array<number>(1, 2, 3, 4); 1366e41f4b71Sopenharmony_ci 1367e41f4b71Sopenharmony_cifor (let item of array) { 1368e41f4b71Sopenharmony_ci console.info(`value : ${item}`); 1369e41f4b71Sopenharmony_ci} 1370e41f4b71Sopenharmony_ci``` 1371e41f4b71Sopenharmony_ci 1372e41f4b71Sopenharmony_ci### [index: number] 1373e41f4b71Sopenharmony_ci 1374e41f4b71Sopenharmony_ci[index: number]: T 1375e41f4b71Sopenharmony_ci 1376e41f4b71Sopenharmony_ciReturns the element at a given index in this array. 1377e41f4b71Sopenharmony_ci 1378e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1379e41f4b71Sopenharmony_ci 1380e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1381e41f4b71Sopenharmony_ci 1382e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1383e41f4b71Sopenharmony_ci| ----- | ------ | ---- | ------------------------------------------------------------------ | 1384e41f4b71Sopenharmony_ci| index | number | Yes | Index of the element. The index starts from zero. If the passed-in index is less than 0 or greater than or equal to the value of **length**, an error is thrown.| 1385e41f4b71Sopenharmony_ci 1386e41f4b71Sopenharmony_ci**Return value** 1387e41f4b71Sopenharmony_ci 1388e41f4b71Sopenharmony_ci| Type | Description | 1389e41f4b71Sopenharmony_ci| ----- | ------------------------ | 1390e41f4b71Sopenharmony_ci| T | Element in the array. | 1391e41f4b71Sopenharmony_ci 1392e41f4b71Sopenharmony_ci**Error codes** 1393e41f4b71Sopenharmony_ci 1394e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 1395e41f4b71Sopenharmony_ci 1396e41f4b71Sopenharmony_ci| ID| Error Message | 1397e41f4b71Sopenharmony_ci| ------- | ------------------------------------ | 1398e41f4b71Sopenharmony_ci| 401 | Parameter error. | 1399e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. | 1400e41f4b71Sopenharmony_ci 1401e41f4b71Sopenharmony_ci**Example** 1402e41f4b71Sopenharmony_ci 1403e41f4b71Sopenharmony_ci```ts 1404e41f4b71Sopenharmony_cilet array = new collections.Array<number>(1, 2, 4); 1405e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", array[1]); 1406e41f4b71Sopenharmony_ci``` 1407e41f4b71Sopenharmony_ci 1408e41f4b71Sopenharmony_ci## collections.Map 1409e41f4b71Sopenharmony_ci 1410e41f4b71Sopenharmony_ciA non-linear data structure. 1411e41f4b71Sopenharmony_ci 1412e41f4b71Sopenharmony_ciThis section uses the following to identify the use of generics: 1413e41f4b71Sopenharmony_ci 1414e41f4b71Sopenharmony_ci- K: key. 1415e41f4b71Sopenharmony_ci- V: value. 1416e41f4b71Sopenharmony_ci 1417e41f4b71Sopenharmony_ciThe K and V types must be any of the [Sendable Data Types](../../arkts-utils/arkts-sendable.md). 1418e41f4b71Sopenharmony_ci 1419e41f4b71Sopenharmony_ci### Properties 1420e41f4b71Sopenharmony_ci 1421e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1422e41f4b71Sopenharmony_ci 1423e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1424e41f4b71Sopenharmony_ci 1425e41f4b71Sopenharmony_ci| Name| Type | Read Only| Optional| Description | 1426e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | --------------- | 1427e41f4b71Sopenharmony_ci| size | number | Yes | No | Number of elements in a map.| 1428e41f4b71Sopenharmony_ci 1429e41f4b71Sopenharmony_ci 1430e41f4b71Sopenharmony_ci### constructor 1431e41f4b71Sopenharmony_ciconstructor(entries?: readonly (readonly [K, V])[] | null) 1432e41f4b71Sopenharmony_ci 1433e41f4b71Sopenharmony_ciA constructor used to create an ArkTS map. 1434e41f4b71Sopenharmony_ci 1435e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1436e41f4b71Sopenharmony_ci 1437e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1438e41f4b71Sopenharmony_ci 1439e41f4b71Sopenharmony_ci**Parameters** 1440e41f4b71Sopenharmony_ci 1441e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1442e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 1443e41f4b71Sopenharmony_ci| entries | [K, V][] \| null | No | Array containing key-value pairs, or iterator object. The default value is **null**, indicating that an empty map is created.| 1444e41f4b71Sopenharmony_ci 1445e41f4b71Sopenharmony_ci**Error codes** 1446e41f4b71Sopenharmony_ci 1447e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1448e41f4b71Sopenharmony_ci 1449e41f4b71Sopenharmony_ci| ID| Error Message | 1450e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 1451e41f4b71Sopenharmony_ci| 10200012 | The ArkTS Map's constructor cannot be directly invoked. | 1452e41f4b71Sopenharmony_ci 1453e41f4b71Sopenharmony_ci**Example** 1454e41f4b71Sopenharmony_ci 1455e41f4b71Sopenharmony_ci```ts 1456e41f4b71Sopenharmony_ci// Positive example 1: 1457e41f4b71Sopenharmony_ciconst myMap = new collections.Map<number, number>(); 1458e41f4b71Sopenharmony_ci``` 1459e41f4b71Sopenharmony_ci 1460e41f4b71Sopenharmony_ci```ts 1461e41f4b71Sopenharmony_ci// Positive example 2: 1462e41f4b71Sopenharmony_ciconst myMap = new collections.Map<number, string>([ 1463e41f4b71Sopenharmony_ci [1, "one"], 1464e41f4b71Sopenharmony_ci [2, "two"], 1465e41f4b71Sopenharmony_ci [3, "three"], 1466e41f4b71Sopenharmony_ci]); 1467e41f4b71Sopenharmony_ci``` 1468e41f4b71Sopenharmony_ci 1469e41f4b71Sopenharmony_ci<!--code_no_check--> 1470e41f4b71Sopenharmony_ci```ts 1471e41f4b71Sopenharmony_ci// Negative example: 1472e41f4b71Sopenharmony_ci@Sendable 1473e41f4b71Sopenharmony_ciclass SharedClass { 1474e41f4b71Sopenharmony_ci constructor() { 1475e41f4b71Sopenharmony_ci } 1476e41f4b71Sopenharmony_ci} 1477e41f4b71Sopenharmony_cilet sObj = new SharedClass(); 1478e41f4b71Sopenharmony_ciconst myMap1: collections.Map<number, SharedClass> = new collections.Map<number, SharedClass>([[1, sObj]]); 1479e41f4b71Sopenharmony_ci// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types) 1480e41f4b71Sopenharmony_cilet obj = new Object(); 1481e41f4b71Sopenharmony_ciconst myMap2: collections.Map<number, Object> = new collections.Map<number, Object>([[1, obj]]); 1482e41f4b71Sopenharmony_ci``` 1483e41f4b71Sopenharmony_ci 1484e41f4b71Sopenharmony_ci### entries 1485e41f4b71Sopenharmony_cientries(): IterableIterator<[K, V]> 1486e41f4b71Sopenharmony_ci 1487e41f4b71Sopenharmony_ciReturns a map iterator object that contains the key-value pair of each element in this ArkTS map. 1488e41f4b71Sopenharmony_ci 1489e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1490e41f4b71Sopenharmony_ci 1491e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1492e41f4b71Sopenharmony_ci 1493e41f4b71Sopenharmony_ci**Return value** 1494e41f4b71Sopenharmony_ci 1495e41f4b71Sopenharmony_ci| Type | Description | 1496e41f4b71Sopenharmony_ci| ------------------------------ | ----------------------- | 1497e41f4b71Sopenharmony_ci| IterableIterator<[K, V]> | Map iterator object.| 1498e41f4b71Sopenharmony_ci 1499e41f4b71Sopenharmony_ci**Error codes** 1500e41f4b71Sopenharmony_ci 1501e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1502e41f4b71Sopenharmony_ci 1503e41f4b71Sopenharmony_ci| ID| Error Message | 1504e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | 1505e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound with non-sendable. | 1506e41f4b71Sopenharmony_ci 1507e41f4b71Sopenharmony_ci**Example** 1508e41f4b71Sopenharmony_ci 1509e41f4b71Sopenharmony_ci```ts 1510e41f4b71Sopenharmony_ci// Example 1: 1511e41f4b71Sopenharmony_ciconst myMap = new collections.Map<number, string>([ 1512e41f4b71Sopenharmony_ci [0, "foo"], 1513e41f4b71Sopenharmony_ci [1, "bar"] 1514e41f4b71Sopenharmony_ci]); 1515e41f4b71Sopenharmony_ci 1516e41f4b71Sopenharmony_ciconst iterator = myMap.entries(); 1517e41f4b71Sopenharmony_ci// Expected output: [0, "foo"] 1518e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 1519e41f4b71Sopenharmony_ci// Expected output: [1, "bar"] 1520e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 1521e41f4b71Sopenharmony_ci``` 1522e41f4b71Sopenharmony_ci 1523e41f4b71Sopenharmony_ci```ts 1524e41f4b71Sopenharmony_ci// Example 2: 1525e41f4b71Sopenharmony_ciconst myMap: collections.Map<number, string> = new collections.Map<number, string>([ 1526e41f4b71Sopenharmony_ci [0, "one"], 1527e41f4b71Sopenharmony_ci [1, "two"], 1528e41f4b71Sopenharmony_ci [2, "three"], 1529e41f4b71Sopenharmony_ci [3, "four"] 1530e41f4b71Sopenharmony_ci]); 1531e41f4b71Sopenharmony_ciconst entriesIter: IterableIterator<[number, string]> = myMap.entries(); 1532e41f4b71Sopenharmony_cifor (const entry of entriesIter) { 1533e41f4b71Sopenharmony_ci if (entry[1].startsWith('t')) { 1534e41f4b71Sopenharmony_ci myMap.delete(entry[0]); 1535e41f4b71Sopenharmony_ci } 1536e41f4b71Sopenharmony_ci} 1537e41f4b71Sopenharmony_ci// Expected output: 2 1538e41f4b71Sopenharmony_ciconsole.info("size:" + myMap.size); 1539e41f4b71Sopenharmony_ci``` 1540e41f4b71Sopenharmony_ci 1541e41f4b71Sopenharmony_ci### keys 1542e41f4b71Sopenharmony_cikeys(): IterableIterator\<K> 1543e41f4b71Sopenharmony_ci 1544e41f4b71Sopenharmony_ciReturns a map iterator object that contains the key of each element in this ArkTS map. 1545e41f4b71Sopenharmony_ci 1546e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1547e41f4b71Sopenharmony_ci 1548e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1549e41f4b71Sopenharmony_ci 1550e41f4b71Sopenharmony_ci**Return value** 1551e41f4b71Sopenharmony_ci 1552e41f4b71Sopenharmony_ci| Type | Description | 1553e41f4b71Sopenharmony_ci| ------------------------- | ----------------------- | 1554e41f4b71Sopenharmony_ci| IterableIterator<K> | Map iterator object.| 1555e41f4b71Sopenharmony_ci 1556e41f4b71Sopenharmony_ci**Error codes** 1557e41f4b71Sopenharmony_ci 1558e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1559e41f4b71Sopenharmony_ci 1560e41f4b71Sopenharmony_ci| ID| Error Message | 1561e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------- | 1562e41f4b71Sopenharmony_ci| 10200011 | The keys method cannot be bound with non-sendable. | 1563e41f4b71Sopenharmony_ci 1564e41f4b71Sopenharmony_ci**Example** 1565e41f4b71Sopenharmony_ci 1566e41f4b71Sopenharmony_ci```ts 1567e41f4b71Sopenharmony_ciconst myMap = new collections.Map<number, string>([ 1568e41f4b71Sopenharmony_ci [0, "foo"], 1569e41f4b71Sopenharmony_ci [1, "bar"] 1570e41f4b71Sopenharmony_ci]); 1571e41f4b71Sopenharmony_ci 1572e41f4b71Sopenharmony_ciconst iterator = myMap.keys(); 1573e41f4b71Sopenharmony_ci// Expected output: 0 1574e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 1575e41f4b71Sopenharmony_ci// Expected output: 1 1576e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 1577e41f4b71Sopenharmony_ci``` 1578e41f4b71Sopenharmony_ci 1579e41f4b71Sopenharmony_ci### values 1580e41f4b71Sopenharmony_civalues(): IterableIterator\<V> 1581e41f4b71Sopenharmony_ci 1582e41f4b71Sopenharmony_ciReturns a map iterator object that contains the value of each element in this ArkTS map. 1583e41f4b71Sopenharmony_ci 1584e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1585e41f4b71Sopenharmony_ci 1586e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1587e41f4b71Sopenharmony_ci 1588e41f4b71Sopenharmony_ci**Return value** 1589e41f4b71Sopenharmony_ci 1590e41f4b71Sopenharmony_ci| Type | Description | 1591e41f4b71Sopenharmony_ci| ------------------------- | ----------------------- | 1592e41f4b71Sopenharmony_ci| IterableIterator<V> | Map iterator object.| 1593e41f4b71Sopenharmony_ci 1594e41f4b71Sopenharmony_ci**Error codes** 1595e41f4b71Sopenharmony_ci 1596e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1597e41f4b71Sopenharmony_ci 1598e41f4b71Sopenharmony_ci| ID| Error Message | 1599e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 1600e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound with non-sendable. | 1601e41f4b71Sopenharmony_ci 1602e41f4b71Sopenharmony_ci**Example** 1603e41f4b71Sopenharmony_ci 1604e41f4b71Sopenharmony_ci```ts 1605e41f4b71Sopenharmony_ciconst myMap = new collections.Map<number, string>([ 1606e41f4b71Sopenharmony_ci [0, "foo"], 1607e41f4b71Sopenharmony_ci [1, "bar"] 1608e41f4b71Sopenharmony_ci]); 1609e41f4b71Sopenharmony_ci 1610e41f4b71Sopenharmony_ciconst iterator = myMap.values(); 1611e41f4b71Sopenharmony_ci// Expected output: "foo" 1612e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 1613e41f4b71Sopenharmony_ci// Expected output: "bar" 1614e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 1615e41f4b71Sopenharmony_ci``` 1616e41f4b71Sopenharmony_ci 1617e41f4b71Sopenharmony_ci### clear 1618e41f4b71Sopenharmony_ciclear(): void 1619e41f4b71Sopenharmony_ci 1620e41f4b71Sopenharmony_ciRemoves all elements from this ArkTS map. 1621e41f4b71Sopenharmony_ci 1622e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1623e41f4b71Sopenharmony_ci 1624e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1625e41f4b71Sopenharmony_ci 1626e41f4b71Sopenharmony_ci**Error codes** 1627e41f4b71Sopenharmony_ci 1628e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1629e41f4b71Sopenharmony_ci 1630e41f4b71Sopenharmony_ci| ID| Error Message | 1631e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 1632e41f4b71Sopenharmony_ci| 10200011 | The clear method cannot be bound with non-sendable. | 1633e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 1634e41f4b71Sopenharmony_ci 1635e41f4b71Sopenharmony_ci**Example** 1636e41f4b71Sopenharmony_ci 1637e41f4b71Sopenharmony_ci```ts 1638e41f4b71Sopenharmony_ciconst myMap = new collections.Map<number, string>([ 1639e41f4b71Sopenharmony_ci [0, "foo"], 1640e41f4b71Sopenharmony_ci [1, "bar"] 1641e41f4b71Sopenharmony_ci]); 1642e41f4b71Sopenharmony_ci// Expected output: 2 1643e41f4b71Sopenharmony_ciconsole.info("size:" + myMap.size); 1644e41f4b71Sopenharmony_cimyMap.clear(); 1645e41f4b71Sopenharmony_ci// Expected output: 0 1646e41f4b71Sopenharmony_ciconsole.info("size:" + myMap.size); 1647e41f4b71Sopenharmony_ci``` 1648e41f4b71Sopenharmony_ci 1649e41f4b71Sopenharmony_ci### delete 1650e41f4b71Sopenharmony_cidelete(key: K): boolean 1651e41f4b71Sopenharmony_ci 1652e41f4b71Sopenharmony_ciDeletes a specified key from this ArkTS map. 1653e41f4b71Sopenharmony_ci 1654e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1655e41f4b71Sopenharmony_ci 1656e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1657e41f4b71Sopenharmony_ci 1658e41f4b71Sopenharmony_ci**Parameters** 1659e41f4b71Sopenharmony_ci 1660e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1661e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------- | 1662e41f4b71Sopenharmony_ci| key | K | Yes | Key to delete.| 1663e41f4b71Sopenharmony_ci 1664e41f4b71Sopenharmony_ci**Return value** 1665e41f4b71Sopenharmony_ci 1666e41f4b71Sopenharmony_ci| Type | Description | 1667e41f4b71Sopenharmony_ci| ------- | ------------------------------------------------------------ | 1668e41f4b71Sopenharmony_ci| boolean | **true**: The key exists and has been deleted.<br>**false**: The key does not exist.| 1669e41f4b71Sopenharmony_ci 1670e41f4b71Sopenharmony_ci**Error codes** 1671e41f4b71Sopenharmony_ci 1672e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1673e41f4b71Sopenharmony_ci 1674e41f4b71Sopenharmony_ci| ID| Error Message | 1675e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 1676e41f4b71Sopenharmony_ci| 10200011 | The delete method cannot be bound with non-sendable. | 1677e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 1678e41f4b71Sopenharmony_ci 1679e41f4b71Sopenharmony_ci 1680e41f4b71Sopenharmony_ci**Example** 1681e41f4b71Sopenharmony_ci 1682e41f4b71Sopenharmony_ci```ts 1683e41f4b71Sopenharmony_ciconst myMap = new collections.Map<string, string>([ 1684e41f4b71Sopenharmony_ci ["hello", "world"], 1685e41f4b71Sopenharmony_ci]); 1686e41f4b71Sopenharmony_ci// Expected result: true 1687e41f4b71Sopenharmony_ciconsole.info("result:" + myMap.delete("hello")); 1688e41f4b71Sopenharmony_ci// Expected result: false 1689e41f4b71Sopenharmony_ciconsole.info("result:" + myMap.has("hello")); 1690e41f4b71Sopenharmony_ci// Expected result: false 1691e41f4b71Sopenharmony_ciconsole.info("result:" + myMap.delete("hello")); 1692e41f4b71Sopenharmony_ci``` 1693e41f4b71Sopenharmony_ci 1694e41f4b71Sopenharmony_ci### forEach 1695e41f4b71Sopenharmony_ciforEach(callbackFn: (value: V, key: K, map: Map<K, V>) => void): void 1696e41f4b71Sopenharmony_ci 1697e41f4b71Sopenharmony_ciCalls a callback function for each key-value pair in this ArkTS map. 1698e41f4b71Sopenharmony_ci 1699e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1700e41f4b71Sopenharmony_ci 1701e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1702e41f4b71Sopenharmony_ci 1703e41f4b71Sopenharmony_ci**Parameters** 1704e41f4b71Sopenharmony_ci 1705e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 1706e41f4b71Sopenharmony_ci| ---------- | ------------------------------------------ | ---- | ---------- | 1707e41f4b71Sopenharmony_ci| callbackFn | (value: V, key: K, map: Map<K, V>) => void | Yes | Callback function to run for each key-value pair.| 1708e41f4b71Sopenharmony_ci 1709e41f4b71Sopenharmony_cicallbackFn 1710e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 1711e41f4b71Sopenharmony_ci| ------ | --------------- | ---- | ---------------------------- | 1712e41f4b71Sopenharmony_ci| value | V | No | Value of the element that is currently traversed.| 1713e41f4b71Sopenharmony_ci| key | K | No | Key of the element that is currently traversed.| 1714e41f4b71Sopenharmony_ci| map | Map<K, V> | No | Current map object. | 1715e41f4b71Sopenharmony_ci 1716e41f4b71Sopenharmony_ci**Error codes** 1717e41f4b71Sopenharmony_ci 1718e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1719e41f4b71Sopenharmony_ci 1720e41f4b71Sopenharmony_ci| ID| Error Message | 1721e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | 1722e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound with non-sendable. | 1723e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 1724e41f4b71Sopenharmony_ci 1725e41f4b71Sopenharmony_ci**Example** 1726e41f4b71Sopenharmony_ci 1727e41f4b71Sopenharmony_ci```ts 1728e41f4b71Sopenharmony_ci// Positive example: 1729e41f4b71Sopenharmony_cinew collections.Map<string, number>([ 1730e41f4b71Sopenharmony_ci ['foo', 0], 1731e41f4b71Sopenharmony_ci ['bar', 1], 1732e41f4b71Sopenharmony_ci ['baz', 2], 1733e41f4b71Sopenharmony_ci]).forEach((value, key, map) => { 1734e41f4b71Sopenharmony_ci console.info(`m[${key}] = ${value}`); 1735e41f4b71Sopenharmony_ci}); 1736e41f4b71Sopenharmony_ci``` 1737e41f4b71Sopenharmony_ci 1738e41f4b71Sopenharmony_ci<!--code_no_check--> 1739e41f4b71Sopenharmony_ci```ts 1740e41f4b71Sopenharmony_ci// Negative example: 1741e41f4b71Sopenharmony_cinew collections.Map<string, number>([ 1742e41f4b71Sopenharmony_ci ['foo', 0], 1743e41f4b71Sopenharmony_ci ['bar', 1], 1744e41f4b71Sopenharmony_ci ['baz', 2], 1745e41f4b71Sopenharmony_ci]).forEach((value, key, map) => { 1746e41f4b71Sopenharmony_ci // Throw exception `Concurrent modification exception.` 1747e41f4b71Sopenharmony_ci map.delete(key); 1748e41f4b71Sopenharmony_ci}); 1749e41f4b71Sopenharmony_ci``` 1750e41f4b71Sopenharmony_ci 1751e41f4b71Sopenharmony_ci### get 1752e41f4b71Sopenharmony_ciget(key: K): V | undefined 1753e41f4b71Sopenharmony_ci 1754e41f4b71Sopenharmony_ciObtains the value of the specified key in this ArkTS map. 1755e41f4b71Sopenharmony_ci 1756e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1757e41f4b71Sopenharmony_ci 1758e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1759e41f4b71Sopenharmony_ci 1760e41f4b71Sopenharmony_ci**Parameters** 1761e41f4b71Sopenharmony_ci 1762e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1763e41f4b71Sopenharmony_ci| ------ | ---- | ---- | --------- | 1764e41f4b71Sopenharmony_ci| key | K | Yes | Target key.| 1765e41f4b71Sopenharmony_ci 1766e41f4b71Sopenharmony_ci**Return value** 1767e41f4b71Sopenharmony_ci 1768e41f4b71Sopenharmony_ci| Type| Description | 1769e41f4b71Sopenharmony_ci| ---- | ------------------------------------------------------------ | 1770e41f4b71Sopenharmony_ci| V | Value obtained. If the key is not found, **undefined** is returned.| 1771e41f4b71Sopenharmony_ci 1772e41f4b71Sopenharmony_ci**Error codes** 1773e41f4b71Sopenharmony_ci 1774e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1775e41f4b71Sopenharmony_ci 1776e41f4b71Sopenharmony_ci| ID| Error Message | 1777e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 1778e41f4b71Sopenharmony_ci| 10200011 | The get method cannot be bound with non-sendable. | 1779e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 1780e41f4b71Sopenharmony_ci 1781e41f4b71Sopenharmony_ci**Example** 1782e41f4b71Sopenharmony_ci 1783e41f4b71Sopenharmony_ci```ts 1784e41f4b71Sopenharmony_ciconst myMap = new collections.Map<string, string>([ 1785e41f4b71Sopenharmony_ci ["hello", "world"], 1786e41f4b71Sopenharmony_ci]); 1787e41f4b71Sopenharmony_ci// Expected output: "world" 1788e41f4b71Sopenharmony_ciconsole.info(myMap.get("hello")); 1789e41f4b71Sopenharmony_ci// Expected output: undefined 1790e41f4b71Sopenharmony_ciconsole.info(myMap.get("world")); 1791e41f4b71Sopenharmony_ci``` 1792e41f4b71Sopenharmony_ci 1793e41f4b71Sopenharmony_ci### has 1794e41f4b71Sopenharmony_cihas(key: K): boolean 1795e41f4b71Sopenharmony_ci 1796e41f4b71Sopenharmony_ciChecks whether a key exists in this ArkTS map. 1797e41f4b71Sopenharmony_ci 1798e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1799e41f4b71Sopenharmony_ci 1800e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1801e41f4b71Sopenharmony_ci 1802e41f4b71Sopenharmony_ci**Return value** 1803e41f4b71Sopenharmony_ci 1804e41f4b71Sopenharmony_ci| Type | Description | 1805e41f4b71Sopenharmony_ci| ------- | --------------------------------------------- | 1806e41f4b71Sopenharmony_ci| boolean | **true**: The key exists.<br>**false**: The key does not exist.| 1807e41f4b71Sopenharmony_ci 1808e41f4b71Sopenharmony_ci**Error codes** 1809e41f4b71Sopenharmony_ci 1810e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1811e41f4b71Sopenharmony_ci 1812e41f4b71Sopenharmony_ci| ID| Error Message | 1813e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 1814e41f4b71Sopenharmony_ci| 10200011 | The has method cannot be bound with non-sendable. | 1815e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 1816e41f4b71Sopenharmony_ci 1817e41f4b71Sopenharmony_ci**Example** 1818e41f4b71Sopenharmony_ci 1819e41f4b71Sopenharmony_ci```ts 1820e41f4b71Sopenharmony_ciconst myMap = new collections.Map<string, string>([ 1821e41f4b71Sopenharmony_ci ["hello", "world"], 1822e41f4b71Sopenharmony_ci]); 1823e41f4b71Sopenharmony_ci// Expected output: true 1824e41f4b71Sopenharmony_ciconsole.info("result:" + myMap.has("hello")); 1825e41f4b71Sopenharmony_ci// Expected output: false 1826e41f4b71Sopenharmony_ciconsole.info("result:" + myMap.has("world")); 1827e41f4b71Sopenharmony_ci``` 1828e41f4b71Sopenharmony_ci 1829e41f4b71Sopenharmony_ci### set 1830e41f4b71Sopenharmony_ciset(key: K, value: V): Map<K, V> 1831e41f4b71Sopenharmony_ci 1832e41f4b71Sopenharmony_ciAdds or updates a key-value pair to this ArkTS map. 1833e41f4b71Sopenharmony_ci 1834e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1835e41f4b71Sopenharmony_ci 1836e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1837e41f4b71Sopenharmony_ci 1838e41f4b71Sopenharmony_ci**Return value** 1839e41f4b71Sopenharmony_ci 1840e41f4b71Sopenharmony_ci| Type | Description | 1841e41f4b71Sopenharmony_ci| --------------- | ------- | 1842e41f4b71Sopenharmony_ci| Map<K, V> | New map obtained.| 1843e41f4b71Sopenharmony_ci 1844e41f4b71Sopenharmony_ci**Error codes** 1845e41f4b71Sopenharmony_ci 1846e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1847e41f4b71Sopenharmony_ci 1848e41f4b71Sopenharmony_ci| ID| Error Message | 1849e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 1850e41f4b71Sopenharmony_ci| 10200011 | The set method cannot be bound with non-sendable. | 1851e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 1852e41f4b71Sopenharmony_ci 1853e41f4b71Sopenharmony_ci**Example** 1854e41f4b71Sopenharmony_ci 1855e41f4b71Sopenharmony_ci```ts 1856e41f4b71Sopenharmony_ci// Positive example: 1857e41f4b71Sopenharmony_ciconst myMap = new collections.Map<string, string>(); 1858e41f4b71Sopenharmony_cimyMap.set("foo", "bar") 1859e41f4b71Sopenharmony_ci``` 1860e41f4b71Sopenharmony_ci 1861e41f4b71Sopenharmony_ci<!--code_no_check--> 1862e41f4b71Sopenharmony_ci```ts 1863e41f4b71Sopenharmony_ci// Negative example: 1864e41f4b71Sopenharmony_cilet obj = new Object(); 1865e41f4b71Sopenharmony_ciconst myMap: collections.Map<string, Object> = new collections.Map<string, Object>(); 1866e41f4b71Sopenharmony_ci// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types) 1867e41f4b71Sopenharmony_cimyMap.set("foo", obj); 1868e41f4b71Sopenharmony_ci``` 1869e41f4b71Sopenharmony_ci 1870e41f4b71Sopenharmony_ci### [Symbol.iterator] 1871e41f4b71Sopenharmony_ci 1872e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<[K, V]> 1873e41f4b71Sopenharmony_ci 1874e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object. 1875e41f4b71Sopenharmony_ci 1876e41f4b71Sopenharmony_ci> **NOTE** 1877e41f4b71Sopenharmony_ci> 1878e41f4b71Sopenharmony_ci> This API cannot be used in .ets files. 1879e41f4b71Sopenharmony_ci 1880e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1881e41f4b71Sopenharmony_ci 1882e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1883e41f4b71Sopenharmony_ci 1884e41f4b71Sopenharmony_ci**Return value** 1885e41f4b71Sopenharmony_ci| Type| Description| 1886e41f4b71Sopenharmony_ci| -------- | -------- | 1887e41f4b71Sopenharmony_ci| IterableIterator<[K, V]> | Iterator obtained.| 1888e41f4b71Sopenharmony_ci 1889e41f4b71Sopenharmony_ci**Error codes** 1890e41f4b71Sopenharmony_ci 1891e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1892e41f4b71Sopenharmony_ci 1893e41f4b71Sopenharmony_ci| ID| Error Message| 1894e41f4b71Sopenharmony_ci| -------- | -------- | 1895e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. | 1896e41f4b71Sopenharmony_ci 1897e41f4b71Sopenharmony_ci**Example** 1898e41f4b71Sopenharmony_ci 1899e41f4b71Sopenharmony_ci```ts 1900e41f4b71Sopenharmony_cilet map = new collections.Map<number, string>([ 1901e41f4b71Sopenharmony_ci [0, "one"], 1902e41f4b71Sopenharmony_ci [1, "two"], 1903e41f4b71Sopenharmony_ci [2, "three"], 1904e41f4b71Sopenharmony_ci [3, "four"] 1905e41f4b71Sopenharmony_ci]); 1906e41f4b71Sopenharmony_ci 1907e41f4b71Sopenharmony_cilet keys = Array.from(map.keys()); 1908e41f4b71Sopenharmony_cifor (let key of keys) { 1909e41f4b71Sopenharmony_ci console.info("key:" + key); 1910e41f4b71Sopenharmony_ci console.info("value:" + map.get(key)); 1911e41f4b71Sopenharmony_ci} 1912e41f4b71Sopenharmony_ci``` 1913e41f4b71Sopenharmony_ci 1914e41f4b71Sopenharmony_ci## collections.Set 1915e41f4b71Sopenharmony_ci 1916e41f4b71Sopenharmony_ciA non-linear data structure. 1917e41f4b71Sopenharmony_ci 1918e41f4b71Sopenharmony_ciThis section uses the following to identify the use of generics: 1919e41f4b71Sopenharmony_ci 1920e41f4b71Sopenharmony_ci- T: type, which can be any of the [Sendable Data Types](../../arkts-utils/arkts-sendable.md). 1921e41f4b71Sopenharmony_ci 1922e41f4b71Sopenharmony_ci### Properties 1923e41f4b71Sopenharmony_ci 1924e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1925e41f4b71Sopenharmony_ci 1926e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1927e41f4b71Sopenharmony_ci 1928e41f4b71Sopenharmony_ci| Name| Type | Read Only| Optional| Description | 1929e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---- | --------------- | 1930e41f4b71Sopenharmony_ci| size | number | Yes | No | Number of elements in a set.| 1931e41f4b71Sopenharmony_ci 1932e41f4b71Sopenharmony_ci### constructor 1933e41f4b71Sopenharmony_ci 1934e41f4b71Sopenharmony_ciconstructor(values?: readonly T[] | null) 1935e41f4b71Sopenharmony_ci 1936e41f4b71Sopenharmony_ciA constructor used to create an ArkTS set. 1937e41f4b71Sopenharmony_ci 1938e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1939e41f4b71Sopenharmony_ci 1940e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1941e41f4b71Sopenharmony_ci 1942e41f4b71Sopenharmony_ci**Parameters** 1943e41f4b71Sopenharmony_ci 1944e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 1945e41f4b71Sopenharmony_ci| ------ | ---- | ---- | --------------------------------------------------------- | 1946e41f4b71Sopenharmony_ci| values | T[] \| null | No| Array or iterator object. The default value is **null**, indicating that an empty set is created.| 1947e41f4b71Sopenharmony_ci 1948e41f4b71Sopenharmony_ci**Error codes** 1949e41f4b71Sopenharmony_ci 1950e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 1951e41f4b71Sopenharmony_ci 1952e41f4b71Sopenharmony_ci| ID| Error Message | 1953e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 1954e41f4b71Sopenharmony_ci| 10200012 | The ArkTS Set's constructor cannot be directly invoked. | 1955e41f4b71Sopenharmony_ci 1956e41f4b71Sopenharmony_ci**Example** 1957e41f4b71Sopenharmony_ci 1958e41f4b71Sopenharmony_ci```ts 1959e41f4b71Sopenharmony_ci// Positive example 1: 1960e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>(); 1961e41f4b71Sopenharmony_ci``` 1962e41f4b71Sopenharmony_ci 1963e41f4b71Sopenharmony_ci```ts 1964e41f4b71Sopenharmony_ci// Positive example 2: 1965e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>([1, 2, 3, 4, 5]); 1966e41f4b71Sopenharmony_ci``` 1967e41f4b71Sopenharmony_ci 1968e41f4b71Sopenharmony_ci<!--code_no_check--> 1969e41f4b71Sopenharmony_ci```ts 1970e41f4b71Sopenharmony_ci// Negative example: 1971e41f4b71Sopenharmony_ci@Sendable 1972e41f4b71Sopenharmony_ciclass SharedClass { 1973e41f4b71Sopenharmony_ci constructor() { 1974e41f4b71Sopenharmony_ci } 1975e41f4b71Sopenharmony_ci} 1976e41f4b71Sopenharmony_ci 1977e41f4b71Sopenharmony_cilet sObj = new SharedClass(); 1978e41f4b71Sopenharmony_ciconst mySet1: collections.Set<number|SharedClass> = new collections.Set<number|SharedClass>([1, sObj]); 1979e41f4b71Sopenharmony_ci// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types) 1980e41f4b71Sopenharmony_cilet obj = new Object(); 1981e41f4b71Sopenharmony_ciconst mySet2: collections.Set<number|SharedClass> = new collections.Set<number|Object>([1, obj]); 1982e41f4b71Sopenharmony_ci``` 1983e41f4b71Sopenharmony_ci 1984e41f4b71Sopenharmony_ci### entries 1985e41f4b71Sopenharmony_cientries(): IterableIterator<[T, T]> 1986e41f4b71Sopenharmony_ci 1987e41f4b71Sopenharmony_ciReturns a set iterator object that contains the key-value pair of each element in this ArkTS set. 1988e41f4b71Sopenharmony_ci 1989e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 1990e41f4b71Sopenharmony_ci 1991e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 1992e41f4b71Sopenharmony_ci 1993e41f4b71Sopenharmony_ci**Return value** 1994e41f4b71Sopenharmony_ci 1995e41f4b71Sopenharmony_ci| Type | Description | 1996e41f4b71Sopenharmony_ci| ------------------------------ | ----------------------- | 1997e41f4b71Sopenharmony_ci| IterableIterator<[T, T]> | Set iterator object.| 1998e41f4b71Sopenharmony_ci 1999e41f4b71Sopenharmony_ci**Error codes** 2000e41f4b71Sopenharmony_ci 2001e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2002e41f4b71Sopenharmony_ci 2003e41f4b71Sopenharmony_ci| ID| Error Message | 2004e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | 2005e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound with non-sendable. | 2006e41f4b71Sopenharmony_ci 2007e41f4b71Sopenharmony_ci**Example** 2008e41f4b71Sopenharmony_ci 2009e41f4b71Sopenharmony_ci```ts 2010e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>([0, 1, 2, 3]); 2011e41f4b71Sopenharmony_ci 2012e41f4b71Sopenharmony_ciconst iterator = mySet.entries(); 2013e41f4b71Sopenharmony_ci// Expected output: [0, 0] 2014e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 2015e41f4b71Sopenharmony_ci// Expected output: [1, 1] 2016e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 2017e41f4b71Sopenharmony_ci``` 2018e41f4b71Sopenharmony_ci 2019e41f4b71Sopenharmony_ci### keys 2020e41f4b71Sopenharmony_cikeys(): IterableIterator\<T> 2021e41f4b71Sopenharmony_ci 2022e41f4b71Sopenharmony_ciReturns a set iterator object that contains the key of each element in this ArkTS set. 2023e41f4b71Sopenharmony_ci 2024e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2025e41f4b71Sopenharmony_ci 2026e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2027e41f4b71Sopenharmony_ci 2028e41f4b71Sopenharmony_ci**Return value** 2029e41f4b71Sopenharmony_ci 2030e41f4b71Sopenharmony_ci| Type | Description | 2031e41f4b71Sopenharmony_ci| ------------------------- | ----------------------- | 2032e41f4b71Sopenharmony_ci| IterableIterator<T> | Set iterator object.| 2033e41f4b71Sopenharmony_ci 2034e41f4b71Sopenharmony_ci**Error codes** 2035e41f4b71Sopenharmony_ci 2036e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2037e41f4b71Sopenharmony_ci 2038e41f4b71Sopenharmony_ci| ID| Error Message | 2039e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------- | 2040e41f4b71Sopenharmony_ci| 10200011 | The keys method cannot be bound with non-sendable. | 2041e41f4b71Sopenharmony_ci 2042e41f4b71Sopenharmony_ci**Example** 2043e41f4b71Sopenharmony_ci 2044e41f4b71Sopenharmony_ci```ts 2045e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>([0, 1, 2, 3]); 2046e41f4b71Sopenharmony_ci 2047e41f4b71Sopenharmony_ciconst iterator = mySet.keys(); 2048e41f4b71Sopenharmony_ci// Expected output: 0 2049e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 2050e41f4b71Sopenharmony_ci// Expected output: 1 2051e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 2052e41f4b71Sopenharmony_ci``` 2053e41f4b71Sopenharmony_ci 2054e41f4b71Sopenharmony_ci### values 2055e41f4b71Sopenharmony_civalues(): IterableIterator\<T> 2056e41f4b71Sopenharmony_ci 2057e41f4b71Sopenharmony_ciReturns a set iterator object that contains the value of each element in this ArkTS set. 2058e41f4b71Sopenharmony_ci 2059e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2060e41f4b71Sopenharmony_ci 2061e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2062e41f4b71Sopenharmony_ci 2063e41f4b71Sopenharmony_ci**Return value** 2064e41f4b71Sopenharmony_ci 2065e41f4b71Sopenharmony_ci| Type | Description | 2066e41f4b71Sopenharmony_ci| ------------------------- | ----------------------- | 2067e41f4b71Sopenharmony_ci| IterableIterator<T> | Set iterator object.| 2068e41f4b71Sopenharmony_ci 2069e41f4b71Sopenharmony_ci**Error codes** 2070e41f4b71Sopenharmony_ci 2071e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2072e41f4b71Sopenharmony_ci 2073e41f4b71Sopenharmony_ci| ID| Error Message | 2074e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 2075e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound with non-sendable. | 2076e41f4b71Sopenharmony_ci 2077e41f4b71Sopenharmony_ci**Example** 2078e41f4b71Sopenharmony_ci 2079e41f4b71Sopenharmony_ci```ts 2080e41f4b71Sopenharmony_ci// Example 1: 2081e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>([0, 1, 2, 3]); 2082e41f4b71Sopenharmony_ci 2083e41f4b71Sopenharmony_ciconst iterator = mySet.values(); 2084e41f4b71Sopenharmony_ci// Expected output: 0 2085e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 2086e41f4b71Sopenharmony_ci// Expected output: 1 2087e41f4b71Sopenharmony_ciconsole.info(iterator.next().value); 2088e41f4b71Sopenharmony_ci``` 2089e41f4b71Sopenharmony_ci 2090e41f4b71Sopenharmony_ci```ts 2091e41f4b71Sopenharmony_ci// Example 2: 2092e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>([0, 1, 2, 3]); 2093e41f4b71Sopenharmony_ci 2094e41f4b71Sopenharmony_ciconst valueIter = mySet.values(); 2095e41f4b71Sopenharmony_cifor (let value of valueIter) { 2096e41f4b71Sopenharmony_ci if (value % 2 == 0) { 2097e41f4b71Sopenharmony_ci mySet.delete(value); 2098e41f4b71Sopenharmony_ci } 2099e41f4b71Sopenharmony_ci} 2100e41f4b71Sopenharmony_ci 2101e41f4b71Sopenharmony_ci// Expected output: 2 2102e41f4b71Sopenharmony_ciconsole.info("size:" + mySet.size); 2103e41f4b71Sopenharmony_ci``` 2104e41f4b71Sopenharmony_ci 2105e41f4b71Sopenharmony_ci### clear 2106e41f4b71Sopenharmony_ciclear(): void 2107e41f4b71Sopenharmony_ci 2108e41f4b71Sopenharmony_ciRemoves all elements from this ArkTS set. 2109e41f4b71Sopenharmony_ci 2110e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2111e41f4b71Sopenharmony_ci 2112e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2113e41f4b71Sopenharmony_ci 2114e41f4b71Sopenharmony_ci**Error codes** 2115e41f4b71Sopenharmony_ci 2116e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2117e41f4b71Sopenharmony_ci 2118e41f4b71Sopenharmony_ci| ID| Error Message | 2119e41f4b71Sopenharmony_ci| -------- | --------------------------------------------------- | 2120e41f4b71Sopenharmony_ci| 10200011 | The clear method cannot be bound with non-sendable. | 2121e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2122e41f4b71Sopenharmony_ci 2123e41f4b71Sopenharmony_ci**Example** 2124e41f4b71Sopenharmony_ci 2125e41f4b71Sopenharmony_ci```ts 2126e41f4b71Sopenharmony_ciconst mySet = new collections.Set<number>([0, 1]); 2127e41f4b71Sopenharmony_ci// Expected output: 2 2128e41f4b71Sopenharmony_ciconsole.info("size:" + mySet.size); 2129e41f4b71Sopenharmony_cimySet.clear(); 2130e41f4b71Sopenharmony_ci// Expected output: 0 2131e41f4b71Sopenharmony_ciconsole.info("size:" + mySet.size); 2132e41f4b71Sopenharmony_ci``` 2133e41f4b71Sopenharmony_ci 2134e41f4b71Sopenharmony_ci### delete 2135e41f4b71Sopenharmony_cidelete(value: T): boolean 2136e41f4b71Sopenharmony_ci 2137e41f4b71Sopenharmony_ciDeletes an element from this ArkTS set. 2138e41f4b71Sopenharmony_ci 2139e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2140e41f4b71Sopenharmony_ci 2141e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2142e41f4b71Sopenharmony_ci 2143e41f4b71Sopenharmony_ci**Parameters** 2144e41f4b71Sopenharmony_ci 2145e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description | 2146e41f4b71Sopenharmony_ci| ------ | ---- | ---- | ---------------- | 2147e41f4b71Sopenharmony_ci| key | K | Yes | Key to delete.| 2148e41f4b71Sopenharmony_ci 2149e41f4b71Sopenharmony_ci**Return value** 2150e41f4b71Sopenharmony_ci 2151e41f4b71Sopenharmony_ci| Type | Description | 2152e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 2153e41f4b71Sopenharmony_ci| boolean | **true**: The key is deleted.<br>**false**: The key fails to be deleted.| 2154e41f4b71Sopenharmony_ci 2155e41f4b71Sopenharmony_ci**Error codes** 2156e41f4b71Sopenharmony_ci 2157e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2158e41f4b71Sopenharmony_ci 2159e41f4b71Sopenharmony_ci| ID| Error Message | 2160e41f4b71Sopenharmony_ci| -------- | ---------------------------------------------------- | 2161e41f4b71Sopenharmony_ci| 10200011 | The delete method cannot be bound with non-sendable. | 2162e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2163e41f4b71Sopenharmony_ci 2164e41f4b71Sopenharmony_ci 2165e41f4b71Sopenharmony_ci**Example** 2166e41f4b71Sopenharmony_ci 2167e41f4b71Sopenharmony_ci```ts 2168e41f4b71Sopenharmony_ciconst mySet = new collections.Set<string>(["hello", "world"]); 2169e41f4b71Sopenharmony_ci// Expected result: true 2170e41f4b71Sopenharmony_ciconsole.info("result:" + mySet.delete("hello")); 2171e41f4b71Sopenharmony_ci// Expected result: false 2172e41f4b71Sopenharmony_ciconsole.info("result:" + mySet.has("hello")); 2173e41f4b71Sopenharmony_ci// Expected result: false 2174e41f4b71Sopenharmony_ciconsole.info("result:" + mySet.delete("hello")); 2175e41f4b71Sopenharmony_ci``` 2176e41f4b71Sopenharmony_ci 2177e41f4b71Sopenharmony_ci### forEach 2178e41f4b71Sopenharmony_ciforEach(callbackFn: (value1: T, value2: T, set: Set\<T>) => void): void 2179e41f4b71Sopenharmony_ci 2180e41f4b71Sopenharmony_ciCalls a callback function for each key-value pair in this ArkTS set. 2181e41f4b71Sopenharmony_ci 2182e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2183e41f4b71Sopenharmony_ci 2184e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2185e41f4b71Sopenharmony_ci 2186e41f4b71Sopenharmony_ci**Parameters** 2187e41f4b71Sopenharmony_ci 2188e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2189e41f4b71Sopenharmony_ci| ---------- | -------------------------------------------- | ---- | ---------- | 2190e41f4b71Sopenharmony_ci| callbackFn | (value1: T, value2: T, set: Set\<T>) => void | Yes | Callback function to run for each key-value pair.| 2191e41f4b71Sopenharmony_ci 2192e41f4b71Sopenharmony_cicallbackFn 2193e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2194e41f4b71Sopenharmony_ci| ------ | ------------ | ---- | ---------------------------- | 2195e41f4b71Sopenharmony_ci| value1 | T | No | Value of the element that is currently traversed.| 2196e41f4b71Sopenharmony_ci| value2 | T | No | Key of the element that is currently traversed.| 2197e41f4b71Sopenharmony_ci| set | Set<T> | No | Current set object. | 2198e41f4b71Sopenharmony_ci 2199e41f4b71Sopenharmony_ci**Error codes** 2200e41f4b71Sopenharmony_ci 2201e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2202e41f4b71Sopenharmony_ci 2203e41f4b71Sopenharmony_ci| ID| Error Message | 2204e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | 2205e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound with non-sendable. | 2206e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2207e41f4b71Sopenharmony_ci 2208e41f4b71Sopenharmony_ci**Example** 2209e41f4b71Sopenharmony_ci 2210e41f4b71Sopenharmony_ci```ts 2211e41f4b71Sopenharmony_ci// Positive example: 2212e41f4b71Sopenharmony_cinew collections.Set<string>(['foo', 'bar', 'baz']).forEach((value1, value2, set) => { 2213e41f4b71Sopenharmony_ci console.info(`s[${value1}] = ${value2}`); 2214e41f4b71Sopenharmony_ci}); 2215e41f4b71Sopenharmony_ci``` 2216e41f4b71Sopenharmony_ci 2217e41f4b71Sopenharmony_ci<!--code_no_check--> 2218e41f4b71Sopenharmony_ci```ts 2219e41f4b71Sopenharmony_ci// Negative example: 2220e41f4b71Sopenharmony_cinew collections.Set<string>(['foo', 'bar', 'baz']).forEach((value1, value2, set) => { 2221e41f4b71Sopenharmony_ci // Throw exception `Concurrent modification exception.` 2222e41f4b71Sopenharmony_ci set.delete(value1); 2223e41f4b71Sopenharmony_ci}); 2224e41f4b71Sopenharmony_ci``` 2225e41f4b71Sopenharmony_ci 2226e41f4b71Sopenharmony_ci### has 2227e41f4b71Sopenharmony_cihas(value: T): boolean 2228e41f4b71Sopenharmony_ci 2229e41f4b71Sopenharmony_ciChecks whether a value exists in this ArkTS set. 2230e41f4b71Sopenharmony_ci 2231e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2232e41f4b71Sopenharmony_ci 2233e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2234e41f4b71Sopenharmony_ci 2235e41f4b71Sopenharmony_ci**Return value** 2236e41f4b71Sopenharmony_ci 2237e41f4b71Sopenharmony_ci| Type | Description | 2238e41f4b71Sopenharmony_ci| ------- | --------------------------------------------- | 2239e41f4b71Sopenharmony_ci| boolean | **true**: The value exists.<br>**false**: The value does not exist.| 2240e41f4b71Sopenharmony_ci 2241e41f4b71Sopenharmony_ci**Error codes** 2242e41f4b71Sopenharmony_ci 2243e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2244e41f4b71Sopenharmony_ci 2245e41f4b71Sopenharmony_ci| ID| Error Message | 2246e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 2247e41f4b71Sopenharmony_ci| 10200011 | The has method cannot be bound with non-sendable. | 2248e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2249e41f4b71Sopenharmony_ci 2250e41f4b71Sopenharmony_ci**Example** 2251e41f4b71Sopenharmony_ci 2252e41f4b71Sopenharmony_ci```ts 2253e41f4b71Sopenharmony_ciconst mySet = new collections.Set<string>(["hello", "world"]); 2254e41f4b71Sopenharmony_ci// Expected output: true 2255e41f4b71Sopenharmony_ciconsole.info("result:" + mySet.has("hello")); 2256e41f4b71Sopenharmony_ci// Expected output: true 2257e41f4b71Sopenharmony_ciconsole.info("result:" + mySet.has("world")); 2258e41f4b71Sopenharmony_ci``` 2259e41f4b71Sopenharmony_ci 2260e41f4b71Sopenharmony_ci### add 2261e41f4b71Sopenharmony_ciadd(value: T): Set\<T> 2262e41f4b71Sopenharmony_ci 2263e41f4b71Sopenharmony_ciChecks whether a value exists in this ArkTS set, and if not, adds the value to the set. 2264e41f4b71Sopenharmony_ci 2265e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2266e41f4b71Sopenharmony_ci 2267e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2268e41f4b71Sopenharmony_ci 2269e41f4b71Sopenharmony_ci**Return value** 2270e41f4b71Sopenharmony_ci 2271e41f4b71Sopenharmony_ci| Type | Description | 2272e41f4b71Sopenharmony_ci| ------------ | --------- | 2273e41f4b71Sopenharmony_ci| Set<T> | Set object.| 2274e41f4b71Sopenharmony_ci 2275e41f4b71Sopenharmony_ci**Error codes** 2276e41f4b71Sopenharmony_ci 2277e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2278e41f4b71Sopenharmony_ci 2279e41f4b71Sopenharmony_ci| ID| Error Message | 2280e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 2281e41f4b71Sopenharmony_ci| 10200011 | The add method cannot be bound with non-sendable. | 2282e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2283e41f4b71Sopenharmony_ci 2284e41f4b71Sopenharmony_ci**Example** 2285e41f4b71Sopenharmony_ci 2286e41f4b71Sopenharmony_ci```ts 2287e41f4b71Sopenharmony_ci// Positive example: 2288e41f4b71Sopenharmony_ciconst mySet: collections.Set<string> = new collections.Set<string>(); 2289e41f4b71Sopenharmony_cimySet.add("foo"); 2290e41f4b71Sopenharmony_ci``` 2291e41f4b71Sopenharmony_ci 2292e41f4b71Sopenharmony_ci<!--code_no_check--> 2293e41f4b71Sopenharmony_ci```ts 2294e41f4b71Sopenharmony_ci// Negative example: 2295e41f4b71Sopenharmony_cilet obj = new Object(); 2296e41f4b71Sopenharmony_ciconst mySet: collections.Set<Object> = new collections.Set<Object>(); 2297e41f4b71Sopenharmony_ci// Type arguments of generic "Sendable" type must be a "Sendable" data type (arkts-sendable-generic-types) 2298e41f4b71Sopenharmony_cimySet.add(obj); 2299e41f4b71Sopenharmony_ci``` 2300e41f4b71Sopenharmony_ci 2301e41f4b71Sopenharmony_ci### [Symbol.iterator] 2302e41f4b71Sopenharmony_ci 2303e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<T> 2304e41f4b71Sopenharmony_ci 2305e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object. 2306e41f4b71Sopenharmony_ci 2307e41f4b71Sopenharmony_ci> **NOTE** 2308e41f4b71Sopenharmony_ci> 2309e41f4b71Sopenharmony_ci> This API cannot be used in .ets files. 2310e41f4b71Sopenharmony_ci 2311e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2312e41f4b71Sopenharmony_ci 2313e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2314e41f4b71Sopenharmony_ci 2315e41f4b71Sopenharmony_ci**Return value** 2316e41f4b71Sopenharmony_ci 2317e41f4b71Sopenharmony_ci| Type| Description| 2318e41f4b71Sopenharmony_ci| -------- | -------- | 2319e41f4b71Sopenharmony_ci| IterableIterator<T> | Iterator obtained.| 2320e41f4b71Sopenharmony_ci 2321e41f4b71Sopenharmony_ci**Error codes** 2322e41f4b71Sopenharmony_ci 2323e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2324e41f4b71Sopenharmony_ci 2325e41f4b71Sopenharmony_ci| ID| Error Message| 2326e41f4b71Sopenharmony_ci| -------- | -------- | 2327e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. | 2328e41f4b71Sopenharmony_ci 2329e41f4b71Sopenharmony_ci**Example** 2330e41f4b71Sopenharmony_ci 2331e41f4b71Sopenharmony_ci```ts 2332e41f4b71Sopenharmony_cilet set = new collections.Set<number>([1, 2, 3, 4, 5]); 2333e41f4b71Sopenharmony_ci 2334e41f4b71Sopenharmony_cilet val: Array<number> = Array.from(set.values()) 2335e41f4b71Sopenharmony_cifor (let item of val) { 2336e41f4b71Sopenharmony_ci console.info("value: " + item); 2337e41f4b71Sopenharmony_ci} 2338e41f4b71Sopenharmony_ci``` 2339e41f4b71Sopenharmony_ci 2340e41f4b71Sopenharmony_ci## collections.ArrayBuffer 2341e41f4b71Sopenharmony_ciUnderlying data structure of the ArkTS typed array. 2342e41f4b71Sopenharmony_ci 2343e41f4b71Sopenharmony_ci### Properties 2344e41f4b71Sopenharmony_ci 2345e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2346e41f4b71Sopenharmony_ci 2347e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2348e41f4b71Sopenharmony_ci 2349e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 2350e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ----------------| 2351e41f4b71Sopenharmony_ci| byteLength | number | Yes | No | Number of bytes occupied by the buffer.| 2352e41f4b71Sopenharmony_ci 2353e41f4b71Sopenharmony_ci### constructor 2354e41f4b71Sopenharmony_ciconstructor(byteLength: number) 2355e41f4b71Sopenharmony_ci 2356e41f4b71Sopenharmony_ciA constructor used to create an ArkTS ArrayBuffer of a given length. 2357e41f4b71Sopenharmony_ci 2358e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2359e41f4b71Sopenharmony_ci 2360e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2361e41f4b71Sopenharmony_ci 2362e41f4b71Sopenharmony_ci**Parameters** 2363e41f4b71Sopenharmony_ci 2364e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2365e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------| 2366e41f4b71Sopenharmony_ci| byteLength | number | Yes | Number of bytes occupied by the buffer. | 2367e41f4b71Sopenharmony_ci 2368e41f4b71Sopenharmony_ci**Error codes** 2369e41f4b71Sopenharmony_ci 2370e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2371e41f4b71Sopenharmony_ci 2372e41f4b71Sopenharmony_ci| ID| Error Message | 2373e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 2374e41f4b71Sopenharmony_ci| 10200012 | The ArrayBuffer's constructor cannot be directly invoked. | 2375e41f4b71Sopenharmony_ci 2376e41f4b71Sopenharmony_ci**Example** 2377e41f4b71Sopenharmony_ci 2378e41f4b71Sopenharmony_ci```ts 2379e41f4b71Sopenharmony_cilet arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(10); 2380e41f4b71Sopenharmony_ciconsole.info("byteLength: " + arrayBuffer.byteLength); // byteLength: 10 2381e41f4b71Sopenharmony_ci``` 2382e41f4b71Sopenharmony_ci 2383e41f4b71Sopenharmony_ci### slice 2384e41f4b71Sopenharmony_cislice(begin: number, end?: number): ArrayBuffer 2385e41f4b71Sopenharmony_ci 2386e41f4b71Sopenharmony_ciSelects a range of elements in this ArkTS ArrayBuffer to create an ArkTS ArrayBuffer. 2387e41f4b71Sopenharmony_ci 2388e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2389e41f4b71Sopenharmony_ci 2390e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2391e41f4b71Sopenharmony_ci 2392e41f4b71Sopenharmony_ci**Parameters** 2393e41f4b71Sopenharmony_ci 2394e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 2395e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------ | 2396e41f4b71Sopenharmony_ci| begin | number | Yes | Start index of the range. If a negative number is passed in, it refers to the index of **begin + arraybuffer.byteLength**.| 2397e41f4b71Sopenharmony_ci| end | number | No | End index of the range. If a negative number is passed in, it refers to the index of **end + arraybuffer.byteLength**. The default value is the length of the ArkTS ArrayBuffer.| 2398e41f4b71Sopenharmony_ci 2399e41f4b71Sopenharmony_ci**Return value** 2400e41f4b71Sopenharmony_ci 2401e41f4b71Sopenharmony_ci| Type | Description | 2402e41f4b71Sopenharmony_ci| ------------ | --------- | 2403e41f4b71Sopenharmony_ci| ArrayBuffer | New ArrayBuffer generated.| 2404e41f4b71Sopenharmony_ci 2405e41f4b71Sopenharmony_ci**Error codes** 2406e41f4b71Sopenharmony_ci 2407e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2408e41f4b71Sopenharmony_ci 2409e41f4b71Sopenharmony_ci| ID| Error Message | 2410e41f4b71Sopenharmony_ci| -------- | -------------------------------------------- | 2411e41f4b71Sopenharmony_ci| 10200011 | The slice method cannot be bound. | 2412e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 2413e41f4b71Sopenharmony_ci 2414e41f4b71Sopenharmony_ci**Example** 2415e41f4b71Sopenharmony_ci 2416e41f4b71Sopenharmony_ci```ts 2417e41f4b71Sopenharmony_cilet arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(10); 2418e41f4b71Sopenharmony_cilet slicedBuffer: collections.ArrayBuffer = arrayBuffer.slice(0, 4); 2419e41f4b71Sopenharmony_ciconsole.info("byteLength: " + slicedBuffer.byteLength); // byteLength: 4 2420e41f4b71Sopenharmony_ci``` 2421e41f4b71Sopenharmony_ci 2422e41f4b71Sopenharmony_ci## TypedArrayFromMapFn 2423e41f4b71Sopenharmony_citype TypedArrayFromMapFn\<FromElementType, ToElementType> = (value: FromElementType, index: number) => ToElementType 2424e41f4b71Sopenharmony_ci 2425e41f4b71Sopenharmony_ciDescribes the mapping function of the ArkTS typed array. 2426e41f4b71Sopenharmony_ci 2427e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2428e41f4b71Sopenharmony_ci 2429e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2430e41f4b71Sopenharmony_ci 2431e41f4b71Sopenharmony_ci**Parameters** 2432e41f4b71Sopenharmony_ci 2433e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2434e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2435e41f4b71Sopenharmony_ci| value | FromElementType | Yes| Element that is currently traversed and used to construct an ArkTS typed array.| 2436e41f4b71Sopenharmony_ci| index | number | Yes| Index of the element.| 2437e41f4b71Sopenharmony_ci 2438e41f4b71Sopenharmony_ci**Return value** 2439e41f4b71Sopenharmony_ci 2440e41f4b71Sopenharmony_ci| Type | Description | 2441e41f4b71Sopenharmony_ci| ------ | --------------------------- | 2442e41f4b71Sopenharmony_ci| ToElementType | Element value after the mapping.| 2443e41f4b71Sopenharmony_ci 2444e41f4b71Sopenharmony_ci## TypedArrayPredicateFn 2445e41f4b71Sopenharmony_citype TypedArrayPredicateFn\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => boolean 2446e41f4b71Sopenharmony_ci 2447e41f4b71Sopenharmony_ciDescribes the assertion function of the ArkTS typed array. 2448e41f4b71Sopenharmony_ci 2449e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2450e41f4b71Sopenharmony_ci 2451e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2452e41f4b71Sopenharmony_ci 2453e41f4b71Sopenharmony_ci**Parameters** 2454e41f4b71Sopenharmony_ci 2455e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2456e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2457e41f4b71Sopenharmony_ci| value | ElementType | Yes| Element that is being traversed in the ArkTS typed array.| 2458e41f4b71Sopenharmony_ci| index | number | Yes| Index of the element.| 2459e41f4b71Sopenharmony_ci| array | ArrayType | Yes| ArkTS typed array that is being traversed.| 2460e41f4b71Sopenharmony_ci 2461e41f4b71Sopenharmony_ci**Return value** 2462e41f4b71Sopenharmony_ci 2463e41f4b71Sopenharmony_ci| Type | Description | 2464e41f4b71Sopenharmony_ci| ------ | --------------------------- | 2465e41f4b71Sopenharmony_ci| boolean | **true**: The value meets the condition.<br>**false**: The value does not meet the condition.| 2466e41f4b71Sopenharmony_ci 2467e41f4b71Sopenharmony_ci## TypedArrayForEachCallback 2468e41f4b71Sopenharmony_citype TypedArrayForEachCallback\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => void 2469e41f4b71Sopenharmony_ci 2470e41f4b71Sopenharmony_ciDescribes the traversal function of the ArkTS typed array. 2471e41f4b71Sopenharmony_ci 2472e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2473e41f4b71Sopenharmony_ci 2474e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2475e41f4b71Sopenharmony_ci 2476e41f4b71Sopenharmony_ci**Parameters** 2477e41f4b71Sopenharmony_ci 2478e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2479e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2480e41f4b71Sopenharmony_ci| value | ElementType | Yes| Element that is being traversed in the ArkTS typed array.| 2481e41f4b71Sopenharmony_ci| index | number | Yes| Index of the element.| 2482e41f4b71Sopenharmony_ci| array | ArrayType | Yes| ArkTS typed array that is being traversed.| 2483e41f4b71Sopenharmony_ci 2484e41f4b71Sopenharmony_ci## TypedArrayMapCallback 2485e41f4b71Sopenharmony_citype TypedArrayMapCallback\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => ElementType 2486e41f4b71Sopenharmony_ci 2487e41f4b71Sopenharmony_ciDescribes the conversion mapping function of the ArkTS typed array. 2488e41f4b71Sopenharmony_ci 2489e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2490e41f4b71Sopenharmony_ci 2491e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2492e41f4b71Sopenharmony_ci 2493e41f4b71Sopenharmony_ci**Parameters** 2494e41f4b71Sopenharmony_ci 2495e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2496e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2497e41f4b71Sopenharmony_ci| value | ElementType | Yes| Element that is being mapped in the ArkTS typed array.| 2498e41f4b71Sopenharmony_ci| index | number | Yes| Index of the element.| 2499e41f4b71Sopenharmony_ci| array | ArrayType | Yes| ArkTS typed array that is being mapped.| 2500e41f4b71Sopenharmony_ci 2501e41f4b71Sopenharmony_ci**Return value** 2502e41f4b71Sopenharmony_ci 2503e41f4b71Sopenharmony_ci| Type | Description | 2504e41f4b71Sopenharmony_ci| ------ | --------------------------- | 2505e41f4b71Sopenharmony_ci| ElementType | Element value after conversion.| 2506e41f4b71Sopenharmony_ci 2507e41f4b71Sopenharmony_ci## TypedArrayReduceCallback 2508e41f4b71Sopenharmony_citype TypedArrayReduceCallback\<AccType, ElementType, ArrayType> = (previousValue: AccType, currentValue: ElementType, currentIndex: number, array: ArrayType) => AccType 2509e41f4b71Sopenharmony_ci 2510e41f4b71Sopenharmony_ciDescribes the reduce function of the ArkTS typed array. 2511e41f4b71Sopenharmony_ci 2512e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2513e41f4b71Sopenharmony_ci 2514e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2515e41f4b71Sopenharmony_ci 2516e41f4b71Sopenharmony_ci**Parameters** 2517e41f4b71Sopenharmony_ci 2518e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2519e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2520e41f4b71Sopenharmony_ci| previousValue | AccType | Yes| Accumulated value of the current traversal.| 2521e41f4b71Sopenharmony_ci| currentValue | ElementType | Yes| Element that is being traversed in the ArkTS typed array.| 2522e41f4b71Sopenharmony_ci| currentIndex | number | Yes| Index of the element.| 2523e41f4b71Sopenharmony_ci| array | ArrayType | Yes| ArkTS typed array that is being traversed.| 2524e41f4b71Sopenharmony_ci 2525e41f4b71Sopenharmony_ci**Return value** 2526e41f4b71Sopenharmony_ci 2527e41f4b71Sopenharmony_ci| Type | Description | 2528e41f4b71Sopenharmony_ci| ------ | --------------------------- | 2529e41f4b71Sopenharmony_ci| AccType | Result of the reduce function. The result is passed in to the **previousValue** parameter when **TypedArrayReduceCallback** is called next time.| 2530e41f4b71Sopenharmony_ci 2531e41f4b71Sopenharmony_ci## TypedArrayCompareFn 2532e41f4b71Sopenharmony_citype TypedArrayCompareFn\<ElementType> = (first: ElementType, second: ElementType) => number 2533e41f4b71Sopenharmony_ci 2534e41f4b71Sopenharmony_ciDescribes the sort function of the ArkTS typed array. 2535e41f4b71Sopenharmony_ci 2536e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2537e41f4b71Sopenharmony_ci 2538e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2539e41f4b71Sopenharmony_ci 2540e41f4b71Sopenharmony_ci**Parameters** 2541e41f4b71Sopenharmony_ci 2542e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2543e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2544e41f4b71Sopenharmony_ci| first | ElementType | Yes| First element to be compared.| 2545e41f4b71Sopenharmony_ci| second | ElementType | Yes| Second element to be compared.| 2546e41f4b71Sopenharmony_ci 2547e41f4b71Sopenharmony_ci**Return value** 2548e41f4b71Sopenharmony_ci 2549e41f4b71Sopenharmony_ci| Type | Description | 2550e41f4b71Sopenharmony_ci| ------ | --------------------------- | 2551e41f4b71Sopenharmony_ci| number | Comparison result of the elements. If **first** is less than **second**, the return value is a negative number. If **first** is greater than **second**, the return value is a positive number. If **first** is equal to **second**, the return value is 0.| 2552e41f4b71Sopenharmony_ci 2553e41f4b71Sopenharmony_ci## collections.TypedArray 2554e41f4b71Sopenharmony_ci 2555e41f4b71Sopenharmony_ciA linear data structure that is implemented on [ArkTS ArrayBuffer](#collectionsarraybuffer). Currently, Int8Array, Uint8Array, Int16Array, Uint16Array, Int32Array, Uint32Array, Uint8ClampedArray, and Float32Array are supported. 2556e41f4b71Sopenharmony_ci 2557e41f4b71Sopenharmony_ciThis section uses the following to identify the use of generics: 2558e41f4b71Sopenharmony_ci- TypedArray: ArkTS typed array of the preceding eight types. 2559e41f4b71Sopenharmony_ci 2560e41f4b71Sopenharmony_ci### Properties 2561e41f4b71Sopenharmony_ci 2562e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2563e41f4b71Sopenharmony_ci 2564e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2565e41f4b71Sopenharmony_ci 2566e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 2567e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | ----------------| 2568e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes | No | Bottom-layer buffer used by an ArkTS typed array.| 2569e41f4b71Sopenharmony_ci| byteLength | number | Yes | No | Number of bytes occupied by the ArkTS typed array.| 2570e41f4b71Sopenharmony_ci| byteOffset | number | Yes | No | Offset between the ArkTS typed array and the start position of the ArrayBuffer.| 2571e41f4b71Sopenharmony_ci| length | number | Yes | No | Number of elements in the ArkTS typed array.| 2572e41f4b71Sopenharmony_ci| BYTES_PER_ELEMENT | number | Yes | No | Number of bytes occupied by each element in the ArkTS typed array.| 2573e41f4b71Sopenharmony_ci 2574e41f4b71Sopenharmony_ci### constructor 2575e41f4b71Sopenharmony_ciconstructor() 2576e41f4b71Sopenharmony_ci 2577e41f4b71Sopenharmony_ciA constructor used to create an empty ArkTS typed array. 2578e41f4b71Sopenharmony_ci 2579e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2580e41f4b71Sopenharmony_ci 2581e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2582e41f4b71Sopenharmony_ci 2583e41f4b71Sopenharmony_ci**Error codes** 2584e41f4b71Sopenharmony_ci 2585e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2586e41f4b71Sopenharmony_ci 2587e41f4b71Sopenharmony_ci| ID| Error Message | 2588e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 2589e41f4b71Sopenharmony_ci| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2590e41f4b71Sopenharmony_ci 2591e41f4b71Sopenharmony_ci**Example** 2592e41f4b71Sopenharmony_ci 2593e41f4b71Sopenharmony_ci```ts 2594e41f4b71Sopenharmony_cilet int8Array: collections.Int8Array = new collections.Int8Array(); 2595e41f4b71Sopenharmony_cilet uint8Array: collections.Uint8Array = new collections.Uint8Array(); 2596e41f4b71Sopenharmony_cilet int16Array: collections.Int16Array = new collections.Int16Array(); 2597e41f4b71Sopenharmony_cilet uint16Array: collections.Uint16Array = new collections.Uint16Array(); 2598e41f4b71Sopenharmony_cilet int32Array: collections.Int32Array = new collections.Int32Array(); 2599e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = new collections.Uint32Array(); 2600e41f4b71Sopenharmony_cilet uint8ClampedArray: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(); 2601e41f4b71Sopenharmony_cilet float32Array: collections.Float32Array = new collections.Float32Array(); 2602e41f4b71Sopenharmony_ci``` 2603e41f4b71Sopenharmony_ci 2604e41f4b71Sopenharmony_ci### constructor 2605e41f4b71Sopenharmony_ciconstructor(length: number) 2606e41f4b71Sopenharmony_ci 2607e41f4b71Sopenharmony_ciA constructor used to create an ArkTS typed array of a given length. 2608e41f4b71Sopenharmony_ci 2609e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2610e41f4b71Sopenharmony_ci 2611e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2612e41f4b71Sopenharmony_ci 2613e41f4b71Sopenharmony_ci**Parameters** 2614e41f4b71Sopenharmony_ci 2615e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2616e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------- | 2617e41f4b71Sopenharmony_ci| length | number | Yes| Length of the ArkTS typed array.| 2618e41f4b71Sopenharmony_ci 2619e41f4b71Sopenharmony_ci**Error codes** 2620e41f4b71Sopenharmony_ci 2621e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2622e41f4b71Sopenharmony_ci 2623e41f4b71Sopenharmony_ci| ID| Error Message | 2624e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 2625e41f4b71Sopenharmony_ci| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2626e41f4b71Sopenharmony_ci 2627e41f4b71Sopenharmony_ci 2628e41f4b71Sopenharmony_ci**Example** 2629e41f4b71Sopenharmony_ci 2630e41f4b71Sopenharmony_ci```ts 2631e41f4b71Sopenharmony_ci// Construct an object with the length parameter. 2632e41f4b71Sopenharmony_cilet int8Array: collections.Int8Array = new collections.Int8Array(12); 2633e41f4b71Sopenharmony_cilet uint8Array: collections.Uint8Array = new collections.Uint8Array(12); 2634e41f4b71Sopenharmony_cilet int16Array: collections.Int16Array = new collections.Int16Array(12); 2635e41f4b71Sopenharmony_cilet uint16Array: collections.Uint16Array = new collections.Uint16Array(12); 2636e41f4b71Sopenharmony_cilet int32Array: collections.Int32Array = new collections.Int32Array(12); 2637e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = new collections.Uint32Array(12); 2638e41f4b71Sopenharmony_cilet uint8ClampedArray: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(12); 2639e41f4b71Sopenharmony_cilet float32Array: collections.Float32Array = new collections.Float32Array(12); 2640e41f4b71Sopenharmony_ci``` 2641e41f4b71Sopenharmony_ci 2642e41f4b71Sopenharmony_ci### constructor 2643e41f4b71Sopenharmony_ciconstructor(array: ArrayLike\<number> | ArrayBuffer) 2644e41f4b71Sopenharmony_ci 2645e41f4b71Sopenharmony_ciA constructor that creates an ArkTS typed array from an array-like object or ArkTS ArrayBuffer. 2646e41f4b71Sopenharmony_ci 2647e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2648e41f4b71Sopenharmony_ci 2649e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2650e41f4b71Sopenharmony_ci 2651e41f4b71Sopenharmony_ci**Parameters** 2652e41f4b71Sopenharmony_ci 2653e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2654e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 2655e41f4b71Sopenharmony_ci| array | ArrayLike\<number> \| ArrayBuffer | Yes| Object used to construct the ArkTS typed array. When the parameter type is ArrayBuffer, the number of bytes occupied by the buffer must be an integer multiple of 4.| 2656e41f4b71Sopenharmony_ci 2657e41f4b71Sopenharmony_ci**Error codes** 2658e41f4b71Sopenharmony_ci 2659e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2660e41f4b71Sopenharmony_ci 2661e41f4b71Sopenharmony_ci| ID| Error Message | 2662e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 2663e41f4b71Sopenharmony_ci| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2664e41f4b71Sopenharmony_ci 2665e41f4b71Sopenharmony_ci**Example** 2666e41f4b71Sopenharmony_ci 2667e41f4b71Sopenharmony_ci```ts 2668e41f4b71Sopenharmony_ci// Example 1: Construct an object from an array-like object. 2669e41f4b71Sopenharmony_cilet arrayLike = [1, 3, 5]; 2670e41f4b71Sopenharmony_cilet array: collections.Uint32Array = new collections.Uint32Array(arrayLike); 2671e41f4b71Sopenharmony_ci``` 2672e41f4b71Sopenharmony_ci 2673e41f4b71Sopenharmony_ci```ts 2674e41f4b71Sopenharmony_ci// Example 2: Construct an object from an ArkTS typed array. 2675e41f4b71Sopenharmony_cilet arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12); 2676e41f4b71Sopenharmony_cilet array: collections.Uint32Array = new collections.Uint32Array(arrayBuffer); 2677e41f4b71Sopenharmony_ci``` 2678e41f4b71Sopenharmony_ci 2679e41f4b71Sopenharmony_ci```ts 2680e41f4b71Sopenharmony_ci// Example 3: Construct an object from another ArkTS typed array. 2681e41f4b71Sopenharmony_cilet arrayLike = [1, 3, 5]; 2682e41f4b71Sopenharmony_cilet uint8Array: collections.Uint8Array = new collections.Uint8Array(arrayLike); 2683e41f4b71Sopenharmony_ci// Uint8Array [1, 3, 5] 2684e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = new collections.Uint32Array(uint8Array); 2685e41f4b71Sopenharmony_ci// Uint32Array [1, 3, 5] 2686e41f4b71Sopenharmony_ci``` 2687e41f4b71Sopenharmony_ci 2688e41f4b71Sopenharmony_ci### constructor 2689e41f4b71Sopenharmony_ciconstructor(buffer: ArrayBuffer, byteOffset?: number, length?: number) 2690e41f4b71Sopenharmony_ci 2691e41f4b71Sopenharmony_ciA constructor that creates an ArkTS typed array from an ArrayBuffer. 2692e41f4b71Sopenharmony_ci 2693e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2694e41f4b71Sopenharmony_ci 2695e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2696e41f4b71Sopenharmony_ci 2697e41f4b71Sopenharmony_ci**Parameters** 2698e41f4b71Sopenharmony_ci 2699e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2700e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------ | 2701e41f4b71Sopenharmony_ci| buffer | ArrayBuffer | Yes| ArrayBuffer object used to construct the ArkTS typed array. The number of bytes occupied by the buffer must be an integer multiple of 4.| 2702e41f4b71Sopenharmony_ci| byteOffset | number | No| Byte offset of the specified buffer. The default value is **0**.| 2703e41f4b71Sopenharmony_ci| length | number | No| Length of the ArkTS typed array. The default value is **0**.| 2704e41f4b71Sopenharmony_ci 2705e41f4b71Sopenharmony_ci**Error codes** 2706e41f4b71Sopenharmony_ci 2707e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2708e41f4b71Sopenharmony_ci 2709e41f4b71Sopenharmony_ci| ID| Error Message | 2710e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------- | 2711e41f4b71Sopenharmony_ci| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2712e41f4b71Sopenharmony_ci 2713e41f4b71Sopenharmony_ci**Example** 2714e41f4b71Sopenharmony_ci 2715e41f4b71Sopenharmony_ci```ts 2716e41f4b71Sopenharmony_cilet int32Array: collections.Int32Array = collections.Int32Array.from([1, 2, 3, 4, 5, 6]); 2717e41f4b71Sopenharmony_ciconsole.info("byteLength: " + int32Array.buffer.byteLength) // byteLength: 24 2718e41f4b71Sopenharmony_ci// Start from the fourth byte of the buffer corresponding to int32Array. The length is 5. 2719e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = new collections.Uint32Array(int32Array.buffer, 4, 5); 2720e41f4b71Sopenharmony_ciconsole.info("[" + uint32Array + "]"); // [2, 3, 4, 5, 6] 2721e41f4b71Sopenharmony_ci``` 2722e41f4b71Sopenharmony_ci 2723e41f4b71Sopenharmony_ci### from 2724e41f4b71Sopenharmony_cistatic from(arrayLike: ArrayLike\<number>): TypedArray 2725e41f4b71Sopenharmony_ci 2726e41f4b71Sopenharmony_ciCreates an ArkTS typed array from an array-like or iterator object. 2727e41f4b71Sopenharmony_ci 2728e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2729e41f4b71Sopenharmony_ci 2730e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2731e41f4b71Sopenharmony_ci 2732e41f4b71Sopenharmony_ci**Parameters** 2733e41f4b71Sopenharmony_ci 2734e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2735e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------------------------------- | 2736e41f4b71Sopenharmony_ci| arrayLike | ArrayLike\<number> | Yes| Array-like object used to construct the ArkTS typed array.| 2737e41f4b71Sopenharmony_ci 2738e41f4b71Sopenharmony_ci**Return value** 2739e41f4b71Sopenharmony_ci 2740e41f4b71Sopenharmony_ci| Type | Description | 2741e41f4b71Sopenharmony_ci| ------------ | --------- | 2742e41f4b71Sopenharmony_ci| TypedArray | New ArkTS typed array generated.| 2743e41f4b71Sopenharmony_ci 2744e41f4b71Sopenharmony_ci**Example** 2745e41f4b71Sopenharmony_ci```ts 2746e41f4b71Sopenharmony_cilet arrayLike = [1, 3, 5]; 2747e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from(arrayLike); 2748e41f4b71Sopenharmony_ci// Uint32Array [1, 3, 5] 2749e41f4b71Sopenharmony_ci``` 2750e41f4b71Sopenharmony_ci 2751e41f4b71Sopenharmony_ci### from 2752e41f4b71Sopenharmony_cistatic from\<T>(arrayLike: ArrayLike\<T>, mapFn: TypedArrayFromMapFn\<T, number>): TypedArray 2753e41f4b71Sopenharmony_ci 2754e41f4b71Sopenharmony_ciCreates an ArkTS typed array from an array-like object. 2755e41f4b71Sopenharmony_ci 2756e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2757e41f4b71Sopenharmony_ci 2758e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2759e41f4b71Sopenharmony_ci 2760e41f4b71Sopenharmony_ci**Parameters** 2761e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2762e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------| 2763e41f4b71Sopenharmony_ci| arrayLike | ArrayLike\<T> | Yes| Array-like object used to construct the ArkTS typed array. | 2764e41f4b71Sopenharmony_ci| mapFn | [TypedArrayFromMapFn](#typedarrayfrommapfn)\<T, number> | Yes| Mapping function.| 2765e41f4b71Sopenharmony_ci 2766e41f4b71Sopenharmony_ci**Return value** 2767e41f4b71Sopenharmony_ci 2768e41f4b71Sopenharmony_ci| Type | Description | 2769e41f4b71Sopenharmony_ci| ------------ | --------- | 2770e41f4b71Sopenharmony_ci| TypedArray | New ArkTS typed array generated.| 2771e41f4b71Sopenharmony_ci 2772e41f4b71Sopenharmony_ci**Example** 2773e41f4b71Sopenharmony_ci 2774e41f4b71Sopenharmony_ci```ts 2775e41f4b71Sopenharmony_ci// Example 1: Create an ArkTS typed array from an object. 2776e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from<number>( 2777e41f4b71Sopenharmony_ci { length: 5 }, (v: Object, k: number) => k); 2778e41f4b71Sopenharmony_ci// Uint32Array [0, 1, 2, 3, 4] 2779e41f4b71Sopenharmony_ci``` 2780e41f4b71Sopenharmony_ci 2781e41f4b71Sopenharmony_ci```ts 2782e41f4b71Sopenharmony_ci// Example 2: Create an ArkTS typed array from a string array. 2783e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from<string>( 2784e41f4b71Sopenharmony_ci ["1", "3", "5"], (v: string, k: number) => parseInt(v)); 2785e41f4b71Sopenharmony_ci// Uint32Array [1, 3, 5] 2786e41f4b71Sopenharmony_ci``` 2787e41f4b71Sopenharmony_ci 2788e41f4b71Sopenharmony_ci```ts 2789e41f4b71Sopenharmony_ci// Example 3: Create an ArkTS typed array from a string. 2790e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from<string>( 2791e41f4b71Sopenharmony_ci "12345", (v: string, k: number) => parseInt(v)); 2792e41f4b71Sopenharmony_ci// Uint32Array [1, 2, 3, 4, 5] 2793e41f4b71Sopenharmony_ci``` 2794e41f4b71Sopenharmony_ci 2795e41f4b71Sopenharmony_ci### from 2796e41f4b71Sopenharmony_cistatic from(iterable: Iterable\<number>, mapFn?: TypedArrayFromMapFn\<number, number>): TypedArray 2797e41f4b71Sopenharmony_ci 2798e41f4b71Sopenharmony_ciCreates an ArkTS typed array from an iterator object. 2799e41f4b71Sopenharmony_ci 2800e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2801e41f4b71Sopenharmony_ci 2802e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2803e41f4b71Sopenharmony_ci 2804e41f4b71Sopenharmony_ci**Parameters** 2805e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2806e41f4b71Sopenharmony_ci| ------- | ------ | ---- | -----------------------------------| 2807e41f4b71Sopenharmony_ci| iterable | Iterable\<number> | Yes| Iterator object used to construct the ArkTS typed array. | 2808e41f4b71Sopenharmony_ci| mapFn | [TypedArrayFromMapFn](#typedarrayfrommapfn)\<number, number> | No| Mapping function. If no value is passed in, no special processing is conducted on the elements.| 2809e41f4b71Sopenharmony_ci 2810e41f4b71Sopenharmony_ci**Return value** 2811e41f4b71Sopenharmony_ci 2812e41f4b71Sopenharmony_ci| Type | Description | 2813e41f4b71Sopenharmony_ci| ------------ | --------- | 2814e41f4b71Sopenharmony_ci| TypedArray | New ArkTS typed array generated.| 2815e41f4b71Sopenharmony_ci 2816e41f4b71Sopenharmony_ci**Example** 2817e41f4b71Sopenharmony_ci 2818e41f4b71Sopenharmony_ci```ts 2819e41f4b71Sopenharmony_ci// Example 1: No mapping function is specified. 2820e41f4b71Sopenharmony_cilet set: Set<number> = new Set<number>([1, 2, 3]); 2821e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from(set); 2822e41f4b71Sopenharmony_ci// Uint32Array [1, 2, 3] 2823e41f4b71Sopenharmony_ci``` 2824e41f4b71Sopenharmony_ci 2825e41f4b71Sopenharmony_ci```ts 2826e41f4b71Sopenharmony_ci// Example 2: A mapping function is specified. 2827e41f4b71Sopenharmony_cilet set: Set<number> = new Set<number>([1, 2, 3]); 2828e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from( 2829e41f4b71Sopenharmony_ci set, (v: number, k: number) => v + k); 2830e41f4b71Sopenharmony_ci// Uint32Array [1, 3, 5] 2831e41f4b71Sopenharmony_ci``` 2832e41f4b71Sopenharmony_ci 2833e41f4b71Sopenharmony_ci### copyWithin 2834e41f4b71Sopenharmony_cicopyWithin(target: number, start: number, end?: number): TypedArray 2835e41f4b71Sopenharmony_ci 2836e41f4b71Sopenharmony_ciCopies elements within a given range from this ArkTS typed array to another position in sequence. 2837e41f4b71Sopenharmony_ci 2838e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2839e41f4b71Sopenharmony_ci 2840e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2841e41f4b71Sopenharmony_ci 2842e41f4b71Sopenharmony_ci**Parameters** 2843e41f4b71Sopenharmony_ci 2844e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2845e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 2846e41f4b71Sopenharmony_ci| target | number | Yes| Index to copy the elements to.| 2847e41f4b71Sopenharmony_ci| start | number | Yes| Start index of the range. If a negative number is passed in, it refers to the index of **start + typedarray.length**.| 2848e41f4b71Sopenharmony_ci| end | number | No| End index of the range. If a negative number is passed in, it refers to the index of **end + typedarray.length**. The default value is the length of the ArkTS typed array.| 2849e41f4b71Sopenharmony_ci 2850e41f4b71Sopenharmony_ci**Return value** 2851e41f4b71Sopenharmony_ci 2852e41f4b71Sopenharmony_ci| Type | Description | 2853e41f4b71Sopenharmony_ci| ------------ | --------- | 2854e41f4b71Sopenharmony_ci| TypedArray | ArkTS typed array after being modified.| 2855e41f4b71Sopenharmony_ci 2856e41f4b71Sopenharmony_ci**Error codes** 2857e41f4b71Sopenharmony_ci 2858e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2859e41f4b71Sopenharmony_ci 2860e41f4b71Sopenharmony_ci| ID| Error Message | 2861e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------ | 2862e41f4b71Sopenharmony_ci| 10200011 | The copyWithin method cannot be bound. | 2863e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2864e41f4b71Sopenharmony_ci 2865e41f4b71Sopenharmony_ci**Example** 2866e41f4b71Sopenharmony_ci 2867e41f4b71Sopenharmony_ci```ts 2868e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5, 6, 7, 8]); 2869e41f4b71Sopenharmony_cilet copied: collections.Uint32Array = array.copyWithin(3, 1, 3); 2870e41f4b71Sopenharmony_ci// Uint32Array [1, 2, 3, 2, 3, 6, 7, 8] 2871e41f4b71Sopenharmony_ci``` 2872e41f4b71Sopenharmony_ci 2873e41f4b71Sopenharmony_ci### some 2874e41f4b71Sopenharmony_cisome(predicate: TypedArrayPredicateFn\<number, TypedArray>): boolean 2875e41f4b71Sopenharmony_ci 2876e41f4b71Sopenharmony_ciChecks whether any element in this ArkTS typed array meets a given condition. 2877e41f4b71Sopenharmony_ci 2878e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2879e41f4b71Sopenharmony_ci 2880e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2881e41f4b71Sopenharmony_ci 2882e41f4b71Sopenharmony_ci**Parameters** 2883e41f4b71Sopenharmony_ci 2884e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2885e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ---------------------------------------------------- | 2886e41f4b71Sopenharmony_ci| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | Yes| Assertion function used for the test.| 2887e41f4b71Sopenharmony_ci 2888e41f4b71Sopenharmony_ci**Return value** 2889e41f4b71Sopenharmony_ci 2890e41f4b71Sopenharmony_ci| Type | Description | 2891e41f4b71Sopenharmony_ci| ------------ | --------- | 2892e41f4b71Sopenharmony_ci| boolean | **true**: An element meeting the given condition exists.<br>**false**: An element meeting the given condition does not exist.| 2893e41f4b71Sopenharmony_ci 2894e41f4b71Sopenharmony_ci**Error codes** 2895e41f4b71Sopenharmony_ci 2896e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2897e41f4b71Sopenharmony_ci 2898e41f4b71Sopenharmony_ci| ID| Error Message | 2899e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 2900e41f4b71Sopenharmony_ci| 10200011 | The some method cannot be bound. | 2901e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2902e41f4b71Sopenharmony_ci 2903e41f4b71Sopenharmony_ci**Example** 2904e41f4b71Sopenharmony_ci 2905e41f4b71Sopenharmony_ci```ts 2906e41f4b71Sopenharmony_cilet arrayLike = [-10, 20, -30, 40, -50]; 2907e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = new collections.Uint32Array(arrayLike); 2908e41f4b71Sopenharmony_ciuint32Array.some((element: number) => element < 0); // false 2909e41f4b71Sopenharmony_ci 2910e41f4b71Sopenharmony_cilet int32Array: collections.Int32Array = new collections.Int32Array(arrayLike); 2911e41f4b71Sopenharmony_ciint32Array.some((element: number) => element < 0); // true 2912e41f4b71Sopenharmony_ci``` 2913e41f4b71Sopenharmony_ci 2914e41f4b71Sopenharmony_ci### every 2915e41f4b71Sopenharmony_cievery(predicate: TypedArrayPredicateFn\<number, TypedArray>): boolean 2916e41f4b71Sopenharmony_ci 2917e41f4b71Sopenharmony_ciChecks whether all elements in this ArkTS typed array meet a given condition. 2918e41f4b71Sopenharmony_ci 2919e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2920e41f4b71Sopenharmony_ci 2921e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2922e41f4b71Sopenharmony_ci 2923e41f4b71Sopenharmony_ci**Parameters** 2924e41f4b71Sopenharmony_ci 2925e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2926e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ----------------------------------------------------- | 2927e41f4b71Sopenharmony_ci| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | Yes| Assertion function used for the test.| 2928e41f4b71Sopenharmony_ci 2929e41f4b71Sopenharmony_ci**Return value** 2930e41f4b71Sopenharmony_ci 2931e41f4b71Sopenharmony_ci| Type | Description | 2932e41f4b71Sopenharmony_ci| ------------ | --------- | 2933e41f4b71Sopenharmony_ci| boolean | **true**: All elements meet the given condition.<br>**false**: Not all elements meet the given condition.| 2934e41f4b71Sopenharmony_ci 2935e41f4b71Sopenharmony_ci**Error codes** 2936e41f4b71Sopenharmony_ci 2937e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2938e41f4b71Sopenharmony_ci 2939e41f4b71Sopenharmony_ci| ID| Error Message | 2940e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 2941e41f4b71Sopenharmony_ci| 10200011 | The every method cannot be bound. | 2942e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2943e41f4b71Sopenharmony_ci 2944e41f4b71Sopenharmony_ci**Example** 2945e41f4b71Sopenharmony_ci 2946e41f4b71Sopenharmony_ci```ts 2947e41f4b71Sopenharmony_cilet arrayLike = [-10, 20, -30, 40, -50]; 2948e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = new collections.Uint32Array(arrayLike); 2949e41f4b71Sopenharmony_ciuint32Array.every((element: number) => element > 0); // true 2950e41f4b71Sopenharmony_ci 2951e41f4b71Sopenharmony_cilet int32Array: collections.Int32Array = new collections.Int32Array(arrayLike); 2952e41f4b71Sopenharmony_ciint32Array.every((element: number) => element > 0); // false 2953e41f4b71Sopenharmony_ci``` 2954e41f4b71Sopenharmony_ci 2955e41f4b71Sopenharmony_ci### fill 2956e41f4b71Sopenharmony_cifill(value: number, start?: number, end?: number): TypedArray 2957e41f4b71Sopenharmony_ci 2958e41f4b71Sopenharmony_ciFills all elements in a given range in this ArkTS typed array with a value. 2959e41f4b71Sopenharmony_ci 2960e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 2961e41f4b71Sopenharmony_ci 2962e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 2963e41f4b71Sopenharmony_ci 2964e41f4b71Sopenharmony_ci**Parameters** 2965e41f4b71Sopenharmony_ci 2966e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 2967e41f4b71Sopenharmony_ci| ------- | ------ | ---- | --------------------------------------------------------| 2968e41f4b71Sopenharmony_ci| value | number | Yes| Value to fill in.| 2969e41f4b71Sopenharmony_ci| start | number | No| Start index of the range. If a negative number is passed in, it refers to the index of **start + typedarray.length**. The default value is **0**.| 2970e41f4b71Sopenharmony_ci| end | number | No| End index of the range. If a negative number is passed in, it refers to the index of **end + typedarray.length**. The default value is the length of the ArkTS typed array.| 2971e41f4b71Sopenharmony_ci 2972e41f4b71Sopenharmony_ci**Return value** 2973e41f4b71Sopenharmony_ci 2974e41f4b71Sopenharmony_ci| Type | Description | 2975e41f4b71Sopenharmony_ci| ------------ | --------- | 2976e41f4b71Sopenharmony_ci| TypedArray | Filled ArkTS typed array.| 2977e41f4b71Sopenharmony_ci 2978e41f4b71Sopenharmony_ci**Error codes** 2979e41f4b71Sopenharmony_ci 2980e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 2981e41f4b71Sopenharmony_ci 2982e41f4b71Sopenharmony_ci| ID| Error Message | 2983e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 2984e41f4b71Sopenharmony_ci| 10200011 | The fill method cannot be bound. | 2985e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 2986e41f4b71Sopenharmony_ci 2987e41f4b71Sopenharmony_ci**Example** 2988e41f4b71Sopenharmony_ci 2989e41f4b71Sopenharmony_ci```ts 2990e41f4b71Sopenharmony_cilet arrayLike = [1, 2, 3]; 2991e41f4b71Sopenharmony_cinew collections.Uint32Array(arrayLike).fill(4); // Uint32Array [4, 4, 4] 2992e41f4b71Sopenharmony_cinew collections.Uint32Array(arrayLike).fill(4, 1); // Uint32Array [1, 4, 4] 2993e41f4b71Sopenharmony_cinew collections.Uint32Array(arrayLike).fill(4, 1, 2); // Uint32Array [1, 4, 3] 2994e41f4b71Sopenharmony_ci``` 2995e41f4b71Sopenharmony_ci 2996e41f4b71Sopenharmony_ci### filter 2997e41f4b71Sopenharmony_cifilter(predicate: TypedArrayPredicateFn\<number, TypedArray>): TypedArray 2998e41f4b71Sopenharmony_ci 2999e41f4b71Sopenharmony_ciReturns a new ArkTS typed array that contains all elements that meet the given condition. 3000e41f4b71Sopenharmony_ci 3001e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3002e41f4b71Sopenharmony_ci 3003e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3004e41f4b71Sopenharmony_ci 3005e41f4b71Sopenharmony_ci**Parameters** 3006e41f4b71Sopenharmony_ci 3007e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3008e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------ | 3009e41f4b71Sopenharmony_ci| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | Yes| Assertion function used for the test.| 3010e41f4b71Sopenharmony_ci 3011e41f4b71Sopenharmony_ci**Return value** 3012e41f4b71Sopenharmony_ci 3013e41f4b71Sopenharmony_ci| Type | Description | 3014e41f4b71Sopenharmony_ci| ------------ | --------- | 3015e41f4b71Sopenharmony_ci| TypedArray| Filtered ArkTS typed array.| 3016e41f4b71Sopenharmony_ci 3017e41f4b71Sopenharmony_ci**Error codes** 3018e41f4b71Sopenharmony_ci 3019e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3020e41f4b71Sopenharmony_ci 3021e41f4b71Sopenharmony_ci| ID| Error Message | 3022e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3023e41f4b71Sopenharmony_ci| 10200011 | The filter method cannot be bound. | 3024e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3025e41f4b71Sopenharmony_ci 3026e41f4b71Sopenharmony_ci**Example** 3027e41f4b71Sopenharmony_ci 3028e41f4b71Sopenharmony_ci```ts 3029e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([0, 1, 2, 3, 4]); 3030e41f4b71Sopenharmony_cilet filtered: collections.Uint32Array = array.filter((element: number) => element % 2 == 0); 3031e41f4b71Sopenharmony_ci// Uint32Array [0, 2, 4] 3032e41f4b71Sopenharmony_ci``` 3033e41f4b71Sopenharmony_ci 3034e41f4b71Sopenharmony_ci### find 3035e41f4b71Sopenharmony_cifind(predicate: TypedArrayPredicateFn\<number, TypedArray>): number | undefined 3036e41f4b71Sopenharmony_ci 3037e41f4b71Sopenharmony_ciReturns the value of the first element that passes a test provided by a callback function. If none of the elements pass the test, **undefined** is returned. 3038e41f4b71Sopenharmony_ci 3039e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3040e41f4b71Sopenharmony_ci 3041e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3042e41f4b71Sopenharmony_ci 3043e41f4b71Sopenharmony_ci**Parameters** 3044e41f4b71Sopenharmony_ci 3045e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3046e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 3047e41f4b71Sopenharmony_ci| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | Yes| Assertion function used for the test.| 3048e41f4b71Sopenharmony_ci 3049e41f4b71Sopenharmony_ci**Return value** 3050e41f4b71Sopenharmony_ci 3051e41f4b71Sopenharmony_ci| Type | Description | 3052e41f4b71Sopenharmony_ci| ------------ | --------- | 3053e41f4b71Sopenharmony_ci| number \| undefined | Value of the first element that passes the test. If none of the elements pass the test, **undefined** is returned.| 3054e41f4b71Sopenharmony_ci 3055e41f4b71Sopenharmony_ci**Error codes** 3056e41f4b71Sopenharmony_ci 3057e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3058e41f4b71Sopenharmony_ci 3059e41f4b71Sopenharmony_ci| ID| Error Message | 3060e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3061e41f4b71Sopenharmony_ci| 10200011 | The find method cannot be bound. | 3062e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3063e41f4b71Sopenharmony_ci 3064e41f4b71Sopenharmony_ci**Example** 3065e41f4b71Sopenharmony_ci 3066e41f4b71Sopenharmony_ci```ts 3067e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([0, 1, 2, 3, 4]); 3068e41f4b71Sopenharmony_ciarray.find((element: number) => element > 2); // 3 3069e41f4b71Sopenharmony_ciarray.find((element: number) => element > 4); // undefined 3070e41f4b71Sopenharmony_ci``` 3071e41f4b71Sopenharmony_ci 3072e41f4b71Sopenharmony_ci### findIndex 3073e41f4b71Sopenharmony_cifindIndex(predicate: TypedArrayPredicateFn\<number, TypedArray>): number 3074e41f4b71Sopenharmony_ci 3075e41f4b71Sopenharmony_ciReturns the index of the first element that passes a test provided by a callback function. If none of the elements pass the test, **-1** is returned. 3076e41f4b71Sopenharmony_ci 3077e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3078e41f4b71Sopenharmony_ci 3079e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3080e41f4b71Sopenharmony_ci 3081e41f4b71Sopenharmony_ci**Parameters** 3082e41f4b71Sopenharmony_ci 3083e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3084e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 3085e41f4b71Sopenharmony_ci| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | Yes| Assertion function used for the test.| 3086e41f4b71Sopenharmony_ci 3087e41f4b71Sopenharmony_ci**Return value** 3088e41f4b71Sopenharmony_ci 3089e41f4b71Sopenharmony_ci| Type | Description | 3090e41f4b71Sopenharmony_ci| ------------ | --------- | 3091e41f4b71Sopenharmony_ci| number | Index of the first element that passes the test. If none of the elements pass the test, **-1** is returned.| 3092e41f4b71Sopenharmony_ci 3093e41f4b71Sopenharmony_ci**Error codes** 3094e41f4b71Sopenharmony_ci 3095e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3096e41f4b71Sopenharmony_ci 3097e41f4b71Sopenharmony_ci| ID| Error Message | 3098e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3099e41f4b71Sopenharmony_ci| 10200011 | The findIndex method cannot be bound. | 3100e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3101e41f4b71Sopenharmony_ci 3102e41f4b71Sopenharmony_ci**Example** 3103e41f4b71Sopenharmony_ci 3104e41f4b71Sopenharmony_ci```ts 3105e41f4b71Sopenharmony_ciconst array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3106e41f4b71Sopenharmony_cilet foundIndex: number = array.findIndex((element: number) => element % 2 === 0); // 1 3107e41f4b71Sopenharmony_ci``` 3108e41f4b71Sopenharmony_ci 3109e41f4b71Sopenharmony_ci### forEach 3110e41f4b71Sopenharmony_ciforEach(callbackFn: TypedArrayForEachCallback\<number, TypedArray>): void 3111e41f4b71Sopenharmony_ci 3112e41f4b71Sopenharmony_ciCalls a callback function for each element in this ArkTS typed array. 3113e41f4b71Sopenharmony_ci 3114e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3115e41f4b71Sopenharmony_ci 3116e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3117e41f4b71Sopenharmony_ci 3118e41f4b71Sopenharmony_ci**Parameters** 3119e41f4b71Sopenharmony_ci 3120e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3121e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------------------------------ | 3122e41f4b71Sopenharmony_ci| callbackFn | [TypedArrayForEachCallback](#typedarrayforeachcallback)\<number, TypedArray> | Yes| Callback function to run for each element.| 3123e41f4b71Sopenharmony_ci 3124e41f4b71Sopenharmony_ci 3125e41f4b71Sopenharmony_ci**Error codes** 3126e41f4b71Sopenharmony_ci 3127e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3128e41f4b71Sopenharmony_ci 3129e41f4b71Sopenharmony_ci| ID| Error Message | 3130e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3131e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound. | 3132e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3133e41f4b71Sopenharmony_ci 3134e41f4b71Sopenharmony_ci**Example** 3135e41f4b71Sopenharmony_ci 3136e41f4b71Sopenharmony_ci```ts 3137e41f4b71Sopenharmony_cilet uint32Array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3]); 3138e41f4b71Sopenharmony_ciuint32Array.forEach((value: number, index: number, array: collections.Uint32Array) => { 3139e41f4b71Sopenharmony_ci console.info(`Element ${value} at index ${index}`); 3140e41f4b71Sopenharmony_ci}); 3141e41f4b71Sopenharmony_ci``` 3142e41f4b71Sopenharmony_ci 3143e41f4b71Sopenharmony_ci### indexOf 3144e41f4b71Sopenharmony_ciindexOf(searchElement: number, fromIndex?: number): number 3145e41f4b71Sopenharmony_ci 3146e41f4b71Sopenharmony_ciReturns the index of the first occurrence of a value in this ArkTS typed array. If the value is not found, **-1** is returned. 3147e41f4b71Sopenharmony_ci 3148e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3149e41f4b71Sopenharmony_ci 3150e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3151e41f4b71Sopenharmony_ci 3152e41f4b71Sopenharmony_ci**Parameters** 3153e41f4b71Sopenharmony_ci 3154e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3155e41f4b71Sopenharmony_ci| ------------- | ------ | ---- | ---------------------------| 3156e41f4b71Sopenharmony_ci| searchElement | number | Yes | Value to search for. | 3157e41f4b71Sopenharmony_ci| fromIndex | number | No | Index from which the search starts. The default value is **0**. If the index is greater than or equal to the length of the ArkTS typed array, **-1** is returned. If a negative number is passed in, the search starts from the end of the ArkTS typed array.| 3158e41f4b71Sopenharmony_ci 3159e41f4b71Sopenharmony_ci**Return value** 3160e41f4b71Sopenharmony_ci 3161e41f4b71Sopenharmony_ci| Type | Description | 3162e41f4b71Sopenharmony_ci| ------------ | --------- | 3163e41f4b71Sopenharmony_ci| number | Index of the first occurrence of the value. If the value is not found, **-1** is returned.| 3164e41f4b71Sopenharmony_ci 3165e41f4b71Sopenharmony_ci**Error codes** 3166e41f4b71Sopenharmony_ci 3167e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3168e41f4b71Sopenharmony_ci 3169e41f4b71Sopenharmony_ci| ID| Error Message | 3170e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3171e41f4b71Sopenharmony_ci| 10200011 | The indexOf method cannot be bound. | 3172e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3173e41f4b71Sopenharmony_ci 3174e41f4b71Sopenharmony_ci**Example** 3175e41f4b71Sopenharmony_ci 3176e41f4b71Sopenharmony_ci```ts 3177e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([3, 5, 9]); 3178e41f4b71Sopenharmony_ciarray.indexOf(3); // 0 3179e41f4b71Sopenharmony_ciarray.indexOf(7); // -1 3180e41f4b71Sopenharmony_ciarray.indexOf(9, 2); // 2 3181e41f4b71Sopenharmony_ciarray.indexOf(9, -2); // 2 3182e41f4b71Sopenharmony_ci``` 3183e41f4b71Sopenharmony_ci 3184e41f4b71Sopenharmony_ci### join 3185e41f4b71Sopenharmony_cijoin(separator?: string): string 3186e41f4b71Sopenharmony_ci 3187e41f4b71Sopenharmony_ciConcatenates all elements in this ArkTS typed array into a string, with a given separator. 3188e41f4b71Sopenharmony_ci 3189e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3190e41f4b71Sopenharmony_ci 3191e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3192e41f4b71Sopenharmony_ci 3193e41f4b71Sopenharmony_ci**Parameters** 3194e41f4b71Sopenharmony_ci 3195e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3196e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------------------------- | 3197e41f4b71Sopenharmony_ci| separator | string | No | Separator to be used. If no value is passed in, a comma (,) is used as the separator.| 3198e41f4b71Sopenharmony_ci 3199e41f4b71Sopenharmony_ci**Return value** 3200e41f4b71Sopenharmony_ci 3201e41f4b71Sopenharmony_ci| Type | Description | 3202e41f4b71Sopenharmony_ci| ------------ | --------- | 3203e41f4b71Sopenharmony_ci| string | String obtained. If the array is empty, an empty string is returned.| 3204e41f4b71Sopenharmony_ci 3205e41f4b71Sopenharmony_ci**Error codes** 3206e41f4b71Sopenharmony_ci 3207e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3208e41f4b71Sopenharmony_ci 3209e41f4b71Sopenharmony_ci| ID| Error Message | 3210e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3211e41f4b71Sopenharmony_ci| 10200011 | The join method cannot be bound. | 3212e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3213e41f4b71Sopenharmony_ci 3214e41f4b71Sopenharmony_ci**Example** 3215e41f4b71Sopenharmony_ci 3216e41f4b71Sopenharmony_ci```ts 3217e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3218e41f4b71Sopenharmony_cilet joined: string = array.join('-'); // "1-2-3-4-5" 3219e41f4b71Sopenharmony_ci``` 3220e41f4b71Sopenharmony_ci 3221e41f4b71Sopenharmony_ci### map 3222e41f4b71Sopenharmony_cimap(callbackFn: TypedArrayMapCallback\<number, TypedArray>): TypedArray 3223e41f4b71Sopenharmony_ci 3224e41f4b71Sopenharmony_ciApplies a callback function to each element in this ArkTS typed array and uses the result to create an ArkTS typed array. 3225e41f4b71Sopenharmony_ci 3226e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3227e41f4b71Sopenharmony_ci 3228e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3229e41f4b71Sopenharmony_ci 3230e41f4b71Sopenharmony_ci**Parameters** 3231e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3232e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------------------------- | 3233e41f4b71Sopenharmony_ci| callbackFn | [TypedArrayMapCallback](#typedarraymapcallback)\<number, TypedArray> | Yes | Callback function to run for each element.| 3234e41f4b71Sopenharmony_ci 3235e41f4b71Sopenharmony_ci 3236e41f4b71Sopenharmony_ci**Return value** 3237e41f4b71Sopenharmony_ci 3238e41f4b71Sopenharmony_ci| Type | Description | 3239e41f4b71Sopenharmony_ci| ------------ | --------- | 3240e41f4b71Sopenharmony_ci| TypedArray | New ArkTS typed array generated.| 3241e41f4b71Sopenharmony_ci 3242e41f4b71Sopenharmony_ci**Error codes** 3243e41f4b71Sopenharmony_ci 3244e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3245e41f4b71Sopenharmony_ci 3246e41f4b71Sopenharmony_ci| ID| Error Message | 3247e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3248e41f4b71Sopenharmony_ci| 10200011 | The map method cannot be bound. | 3249e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3250e41f4b71Sopenharmony_ci 3251e41f4b71Sopenharmony_ci**Example** 3252e41f4b71Sopenharmony_ci 3253e41f4b71Sopenharmony_ci```ts 3254e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([25, 36, 49]); 3255e41f4b71Sopenharmony_ciconst mapped: collections.Uint32Array = array.map(Math.sqrt); // Uint32Array [5, 6 ,7] 3256e41f4b71Sopenharmony_ci``` 3257e41f4b71Sopenharmony_ci 3258e41f4b71Sopenharmony_ci### reduce 3259e41f4b71Sopenharmony_cireduce(callbackFn: TypedArrayReduceCallback\<number, number, TypedArray>): number 3260e41f4b71Sopenharmony_ci 3261e41f4b71Sopenharmony_ciApplies a reduce function on each element in this ArkTS typed array and returns the final reduction result. 3262e41f4b71Sopenharmony_ci 3263e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3264e41f4b71Sopenharmony_ci 3265e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3266e41f4b71Sopenharmony_ci 3267e41f4b71Sopenharmony_ci**Parameters** 3268e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3269e41f4b71Sopenharmony_ci| ---------- | ---------------------- | ---- | ------------------------------------------------------------ | 3270e41f4b71Sopenharmony_ci| callbackFn | [TypedArrayReduceCallback](#typedarrayreducecallback)\<number, number, TypedArray> | Yes| Reduce function.| 3271e41f4b71Sopenharmony_ci 3272e41f4b71Sopenharmony_ci**Return value** 3273e41f4b71Sopenharmony_ci 3274e41f4b71Sopenharmony_ci| Type | Description | 3275e41f4b71Sopenharmony_ci| ------------ | --------- | 3276e41f4b71Sopenharmony_ci| number | Final result obtained from the last call of the reduce function.| 3277e41f4b71Sopenharmony_ci 3278e41f4b71Sopenharmony_ci**Error codes** 3279e41f4b71Sopenharmony_ci 3280e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3281e41f4b71Sopenharmony_ci 3282e41f4b71Sopenharmony_ci| ID| Error Message | 3283e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------ | 3284e41f4b71Sopenharmony_ci| 10200011 | The reduce method cannot be bound. | 3285e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3286e41f4b71Sopenharmony_ci 3287e41f4b71Sopenharmony_ci**Example** 3288e41f4b71Sopenharmony_ci 3289e41f4b71Sopenharmony_ci```ts 3290e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3291e41f4b71Sopenharmony_cilet reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value); 3292e41f4b71Sopenharmony_ci// reducedValue == 15 3293e41f4b71Sopenharmony_ci``` 3294e41f4b71Sopenharmony_ci 3295e41f4b71Sopenharmony_ci### reduce 3296e41f4b71Sopenharmony_cireduce(callbackFn: TypedArrayReduceCallback\<number, number, TypedArray>, initialValue: number): number 3297e41f4b71Sopenharmony_ci 3298e41f4b71Sopenharmony_ciApplies a reduce function for each element in this ArkTS typed array, receives an initial value as the parameter called by the reduce function for the first time, and returns the final reduction result. 3299e41f4b71Sopenharmony_ci 3300e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3301e41f4b71Sopenharmony_ci 3302e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3303e41f4b71Sopenharmony_ci 3304e41f4b71Sopenharmony_ci**Parameters** 3305e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3306e41f4b71Sopenharmony_ci| --------- | ------ | ---- | --------------------------------------------------- | 3307e41f4b71Sopenharmony_ci| callbackFn | [TypedArrayReduceCallback](#typedarrayreducecallback)\<number, number, TypedArray> | Yes | Reduce function.| 3308e41f4b71Sopenharmony_ci| initialValue | number | Yes | Initial value.| 3309e41f4b71Sopenharmony_ci 3310e41f4b71Sopenharmony_ci 3311e41f4b71Sopenharmony_ci**Return value** 3312e41f4b71Sopenharmony_ci 3313e41f4b71Sopenharmony_ci| Type | Description | 3314e41f4b71Sopenharmony_ci| ------------ | --------- | 3315e41f4b71Sopenharmony_ci| number | Final result obtained from the last call of the reduce function.| 3316e41f4b71Sopenharmony_ci 3317e41f4b71Sopenharmony_ci**Error codes** 3318e41f4b71Sopenharmony_ci 3319e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3320e41f4b71Sopenharmony_ci 3321e41f4b71Sopenharmony_ci| ID| Error Message | 3322e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3323e41f4b71Sopenharmony_ci| 10200011 | The reduce method cannot be bound. | 3324e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3325e41f4b71Sopenharmony_ci 3326e41f4b71Sopenharmony_ci**Example** 3327e41f4b71Sopenharmony_ci 3328e41f4b71Sopenharmony_ci```ts 3329e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3330e41f4b71Sopenharmony_cilet reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value, 1); 3331e41f4b71Sopenharmony_ci// reducedValue == 16 3332e41f4b71Sopenharmony_ci``` 3333e41f4b71Sopenharmony_ci 3334e41f4b71Sopenharmony_ci### reduce 3335e41f4b71Sopenharmony_cireduce\<U>(callbackFn: TypedArrayReduceCallback\<U, number, TypedArray>, initialValue: U): U 3336e41f4b71Sopenharmony_ci 3337e41f4b71Sopenharmony_ciApplies a reduce function for each element in this ArkTS typed array, receives an initial value as the parameter called by the reduce function for the first time, and returns the final reduction result. 3338e41f4b71Sopenharmony_ci 3339e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3340e41f4b71Sopenharmony_ci 3341e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3342e41f4b71Sopenharmony_ci 3343e41f4b71Sopenharmony_ci**Parameters** 3344e41f4b71Sopenharmony_ci 3345e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3346e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------------------------- | 3347e41f4b71Sopenharmony_ci| callbackFn | [TypedArrayReduceCallback](#typedarrayreducecallback)\<U, number, TypedArray> | Yes | Reduce function.| 3348e41f4b71Sopenharmony_ci| initialValue | U | Yes | Initial value.| 3349e41f4b71Sopenharmony_ci 3350e41f4b71Sopenharmony_ci**Return value** 3351e41f4b71Sopenharmony_ci 3352e41f4b71Sopenharmony_ci| Type | Description | 3353e41f4b71Sopenharmony_ci| ------------ | --------- | 3354e41f4b71Sopenharmony_ci| U | Final result obtained from the last call of the reduce function.| 3355e41f4b71Sopenharmony_ci 3356e41f4b71Sopenharmony_ci**Error codes** 3357e41f4b71Sopenharmony_ci 3358e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3359e41f4b71Sopenharmony_ci 3360e41f4b71Sopenharmony_ci| ID| Error Message | 3361e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3362e41f4b71Sopenharmony_ci| 10200011 | The reduce method cannot be bound. | 3363e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3364e41f4b71Sopenharmony_ci 3365e41f4b71Sopenharmony_ci**Example** 3366e41f4b71Sopenharmony_ci 3367e41f4b71Sopenharmony_ci```ts 3368e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3369e41f4b71Sopenharmony_cilet reducedValue: string = array.reduce<string>((accumulator: string, value: number) => accumulator + value, "initialValue"); 3370e41f4b71Sopenharmony_ci// reducedValue == initialValue12345 3371e41f4b71Sopenharmony_ci``` 3372e41f4b71Sopenharmony_ci 3373e41f4b71Sopenharmony_ci### reverse 3374e41f4b71Sopenharmony_cireverse(): TypedArray 3375e41f4b71Sopenharmony_ci 3376e41f4b71Sopenharmony_ciReverses this ArkTS typed array. 3377e41f4b71Sopenharmony_ci 3378e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3379e41f4b71Sopenharmony_ci 3380e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3381e41f4b71Sopenharmony_ci 3382e41f4b71Sopenharmony_ci**Return value** 3383e41f4b71Sopenharmony_ci 3384e41f4b71Sopenharmony_ci| Type | Description | 3385e41f4b71Sopenharmony_ci| ------------ | --------- | 3386e41f4b71Sopenharmony_ci| TypedArray | Reversed ArkTS typed array.| 3387e41f4b71Sopenharmony_ci 3388e41f4b71Sopenharmony_ci**Error codes** 3389e41f4b71Sopenharmony_ci 3390e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3391e41f4b71Sopenharmony_ci 3392e41f4b71Sopenharmony_ci| ID| Error Message | 3393e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3394e41f4b71Sopenharmony_ci| 10200011 | The reverse method cannot be bound. | 3395e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3396e41f4b71Sopenharmony_ci 3397e41f4b71Sopenharmony_ci**Example** 3398e41f4b71Sopenharmony_ci 3399e41f4b71Sopenharmony_ci```ts 3400e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3401e41f4b71Sopenharmony_cilet reversed: collections.Uint32Array = array.reverse(); // Uint32Array [5, 4, 3, 2, 1] 3402e41f4b71Sopenharmony_ci``` 3403e41f4b71Sopenharmony_ci 3404e41f4b71Sopenharmony_ci### set 3405e41f4b71Sopenharmony_ciset(array: ArrayLike\<number>, offset?: number): void 3406e41f4b71Sopenharmony_ci 3407e41f4b71Sopenharmony_ciWrites the elements in an array-like object to the given start position in sequence. 3408e41f4b71Sopenharmony_ci 3409e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3410e41f4b71Sopenharmony_ci 3411e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3412e41f4b71Sopenharmony_ci 3413e41f4b71Sopenharmony_ci**Parameters** 3414e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3415e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------------------------- | 3416e41f4b71Sopenharmony_ci| array | ArrayLike\<number> | Yes | Array-like object whose elements will be written.| 3417e41f4b71Sopenharmony_ci| offset | number | No | Start position for writing data. The default value is **0**.| 3418e41f4b71Sopenharmony_ci 3419e41f4b71Sopenharmony_ci**Error codes** 3420e41f4b71Sopenharmony_ci 3421e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3422e41f4b71Sopenharmony_ci 3423e41f4b71Sopenharmony_ci| ID| Error Message | 3424e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3425e41f4b71Sopenharmony_ci| 10200011 | The set method cannot be bound. | 3426e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3427e41f4b71Sopenharmony_ci 3428e41f4b71Sopenharmony_ci**Example** 3429e41f4b71Sopenharmony_ci 3430e41f4b71Sopenharmony_ci```ts 3431e41f4b71Sopenharmony_cilet buffer: collections.ArrayBuffer = new collections.ArrayBuffer(8); 3432e41f4b71Sopenharmony_cilet array: collections.Uint8Array = new collections.Uint8Array(buffer); 3433e41f4b71Sopenharmony_ciarray.set([1, 2, 3], 3); // Uint8Array [0, 0, 0, 1, 2, 3, 0, 0] 3434e41f4b71Sopenharmony_ci``` 3435e41f4b71Sopenharmony_ci 3436e41f4b71Sopenharmony_ci### slice 3437e41f4b71Sopenharmony_cislice(start?: number, end?: number): TypedArray 3438e41f4b71Sopenharmony_ci 3439e41f4b71Sopenharmony_ciSelects a range of elements in this ArkTS typed array to create an ArkTS typed array. 3440e41f4b71Sopenharmony_ci 3441e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3442e41f4b71Sopenharmony_ci 3443e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3444e41f4b71Sopenharmony_ci 3445e41f4b71Sopenharmony_ci**Parameters** 3446e41f4b71Sopenharmony_ci 3447e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3448e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -----------------------------------------------------| 3449e41f4b71Sopenharmony_ci| start | number | No | Start index of the range. If a negative number is passed in, it refers to the index of **start + typedarray.length**. The default value is **0**.| 3450e41f4b71Sopenharmony_ci| end | number | No | End index of the range (exclusive). If a negative number is passed in, it refers to the index of **end + typedarray.length**. The default value is the length of the ArkTS typed array.| 3451e41f4b71Sopenharmony_ci 3452e41f4b71Sopenharmony_ci**Return value** 3453e41f4b71Sopenharmony_ci 3454e41f4b71Sopenharmony_ci| Type | Description | 3455e41f4b71Sopenharmony_ci| ------------ | --------- | 3456e41f4b71Sopenharmony_ci| TypedArray | New ArkTS typed array generated.| 3457e41f4b71Sopenharmony_ci 3458e41f4b71Sopenharmony_ci**Error codes** 3459e41f4b71Sopenharmony_ci 3460e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3461e41f4b71Sopenharmony_ci 3462e41f4b71Sopenharmony_ci| ID| Error Message | 3463e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3464e41f4b71Sopenharmony_ci| 10200011 | The slice method cannot be bound. | 3465e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3466e41f4b71Sopenharmony_ci 3467e41f4b71Sopenharmony_ci**Example** 3468e41f4b71Sopenharmony_ci 3469e41f4b71Sopenharmony_ci```ts 3470e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3471e41f4b71Sopenharmony_ciarray.slice(); // Uint32Array [1, 2, 3, 4, 5] 3472e41f4b71Sopenharmony_ciarray.slice(1, 3); // Uint32Array [2, 3] 3473e41f4b71Sopenharmony_ciarray.slice(-2); // Uint32Array [4, 5] 3474e41f4b71Sopenharmony_ci``` 3475e41f4b71Sopenharmony_ci 3476e41f4b71Sopenharmony_ci### sort 3477e41f4b71Sopenharmony_cisort(compareFn?: TypedArrayCompareFn\<number>): TypedArray 3478e41f4b71Sopenharmony_ci 3479e41f4b71Sopenharmony_ciSorts elements in this ArkTS typed array and returns the sorted ArkTS typed array. 3480e41f4b71Sopenharmony_ci 3481e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3482e41f4b71Sopenharmony_ci 3483e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3484e41f4b71Sopenharmony_ci 3485e41f4b71Sopenharmony_ci**Parameters** 3486e41f4b71Sopenharmony_ci 3487e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3488e41f4b71Sopenharmony_ci| --------- | ---------------------- | ---- | ------------------------------------------ | 3489e41f4b71Sopenharmony_ci| compareFn | [TypedArrayCompareFn](#typedarraycomparefn)\<number> | No | Function that determines the sort order. By default, elements are sorted in ascending order.| 3490e41f4b71Sopenharmony_ci 3491e41f4b71Sopenharmony_ci**Return value** 3492e41f4b71Sopenharmony_ci 3493e41f4b71Sopenharmony_ci| Type | Description | 3494e41f4b71Sopenharmony_ci| ------------ | --------- | 3495e41f4b71Sopenharmony_ci| TypedArray | Sorted ArkTS typed array.| 3496e41f4b71Sopenharmony_ci 3497e41f4b71Sopenharmony_ci**Error codes** 3498e41f4b71Sopenharmony_ci 3499e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3500e41f4b71Sopenharmony_ci 3501e41f4b71Sopenharmony_ci| ID| Error Message | 3502e41f4b71Sopenharmony_ci| -------- | ------------------------------------------ | 3503e41f4b71Sopenharmony_ci| 10200011 | The sort method cannot be bound. | 3504e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3505e41f4b71Sopenharmony_ci 3506e41f4b71Sopenharmony_ci**Example** 3507e41f4b71Sopenharmony_ci 3508e41f4b71Sopenharmony_ci```ts 3509e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 3, 5, 4, 2]); 3510e41f4b71Sopenharmony_ciarray.sort(); // Uint32Array [1, 2, 3, 4, 5] 3511e41f4b71Sopenharmony_ciarray.sort((a: number, b: number) => a - b); // Uint32Array [1, 2, 3, 4, 5] 3512e41f4b71Sopenharmony_ciarray.sort((a: number, b: number) => b - a); // Uint32Array [5, 4, 3, 2, 1] 3513e41f4b71Sopenharmony_ci``` 3514e41f4b71Sopenharmony_ci 3515e41f4b71Sopenharmony_ci### subarray 3516e41f4b71Sopenharmony_cisubarray(begin?: number, end?: number): TypedArray 3517e41f4b71Sopenharmony_ci 3518e41f4b71Sopenharmony_ciReturns a new ArkTS typed array based on the same ArkTS ArrayBuffer. 3519e41f4b71Sopenharmony_ci 3520e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3521e41f4b71Sopenharmony_ci 3522e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3523e41f4b71Sopenharmony_ci 3524e41f4b71Sopenharmony_ci**Parameters** 3525e41f4b71Sopenharmony_ci 3526e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3527e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------- | 3528e41f4b71Sopenharmony_ci| begin | number | No | Start index of the range. If a negative number is passed in, it refers to the index of **begin + typedarray.length**. The default value is **0**.| 3529e41f4b71Sopenharmony_ci| end | number | No | End index of the range (exclusive). If a negative number is passed in, it refers to the index of **end + typedarray.length**. The default value is the length of the ArkTS typed array.| 3530e41f4b71Sopenharmony_ci 3531e41f4b71Sopenharmony_ci**Return value** 3532e41f4b71Sopenharmony_ci 3533e41f4b71Sopenharmony_ci| Type | Description | 3534e41f4b71Sopenharmony_ci| ------------ | --------- | 3535e41f4b71Sopenharmony_ci| TypedArray | New ArkTS typed array generated.| 3536e41f4b71Sopenharmony_ci 3537e41f4b71Sopenharmony_ci**Error codes** 3538e41f4b71Sopenharmony_ci 3539e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3540e41f4b71Sopenharmony_ci 3541e41f4b71Sopenharmony_ci| ID| Error Message | 3542e41f4b71Sopenharmony_ci| -------- | -------------------------------------------------| 3543e41f4b71Sopenharmony_ci| 10200011 | The subarray method cannot be bound. | 3544e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3545e41f4b71Sopenharmony_ci 3546e41f4b71Sopenharmony_ci**Example** 3547e41f4b71Sopenharmony_ci 3548e41f4b71Sopenharmony_ci```ts 3549e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3550e41f4b71Sopenharmony_cilet subArray: collections.Uint32Array = array.subarray(); // Uint32Array [1, 2, 3, 4, 5] 3551e41f4b71Sopenharmony_cisubArray.set([10, 20, 30]); // Uint32Array [10, 20, 30, 4, 5] 3552e41f4b71Sopenharmony_ci``` 3553e41f4b71Sopenharmony_ci 3554e41f4b71Sopenharmony_ci### at 3555e41f4b71Sopenharmony_ciat(index: number): number | undefined 3556e41f4b71Sopenharmony_ci 3557e41f4b71Sopenharmony_ciReturns the element at the given index. If no element is found, **undefined** is returned. 3558e41f4b71Sopenharmony_ci 3559e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3560e41f4b71Sopenharmony_ci 3561e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3562e41f4b71Sopenharmony_ci 3563e41f4b71Sopenharmony_ci**Parameters** 3564e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3565e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------------ | 3566e41f4b71Sopenharmony_ci| index | number | Yes | Index of the element. The index in an array always starts from 0 and is an integer. If a negative number is passed in, it refers to the index of **index + typedarray.length**.| 3567e41f4b71Sopenharmony_ci 3568e41f4b71Sopenharmony_ci**Return value** 3569e41f4b71Sopenharmony_ci 3570e41f4b71Sopenharmony_ci| Type | Description | 3571e41f4b71Sopenharmony_ci| ------------ | --------- | 3572e41f4b71Sopenharmony_ci| number \| undefined| Element obtained. If no element is found, **undefined** is returned.| 3573e41f4b71Sopenharmony_ci 3574e41f4b71Sopenharmony_ci**Error codes** 3575e41f4b71Sopenharmony_ci 3576e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3577e41f4b71Sopenharmony_ci 3578e41f4b71Sopenharmony_ci| ID| Error Message | 3579e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------ | 3580e41f4b71Sopenharmony_ci| 10200011 | The at method cannot be bound. | 3581e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3582e41f4b71Sopenharmony_ci 3583e41f4b71Sopenharmony_ci**Example** 3584e41f4b71Sopenharmony_ci 3585e41f4b71Sopenharmony_ci```ts 3586e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3587e41f4b71Sopenharmony_ciconsole.info("element: " + array.at(2)); // element: 3 3588e41f4b71Sopenharmony_ciconsole.info("element: " + array.at(-1)); // element: 5 3589e41f4b71Sopenharmony_ciconsole.info("element: " + array.at(6)); // element: undefined 3590e41f4b71Sopenharmony_ci``` 3591e41f4b71Sopenharmony_ci 3592e41f4b71Sopenharmony_ci### includes 3593e41f4b71Sopenharmony_ciincludes(searchElement: number, fromIndex?: number): boolean 3594e41f4b71Sopenharmony_ci 3595e41f4b71Sopenharmony_ciChecks whether elements are contained in this ArkTS typed array. 3596e41f4b71Sopenharmony_ci 3597e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3598e41f4b71Sopenharmony_ci 3599e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3600e41f4b71Sopenharmony_ci 3601e41f4b71Sopenharmony_ci**Parameters** 3602e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3603e41f4b71Sopenharmony_ci| ------ | ------ | ---- | --------------------------------------- | 3604e41f4b71Sopenharmony_ci| searchElement | number | Yes | Element to search for.| 3605e41f4b71Sopenharmony_ci| fromIndex | number | No | Index from which the search starts. If a negative number is passed in, it refers to the index of **fromIndex + typedarray.length**. The default value is **0**.| 3606e41f4b71Sopenharmony_ci 3607e41f4b71Sopenharmony_ci**Return value** 3608e41f4b71Sopenharmony_ci 3609e41f4b71Sopenharmony_ci| Type | Description | 3610e41f4b71Sopenharmony_ci| ------- | ---------------------------------------------------------- | 3611e41f4b71Sopenharmony_ci| boolean | **true**: The element exists.<br>**false**: The element does not exist.| 3612e41f4b71Sopenharmony_ci 3613e41f4b71Sopenharmony_ci 3614e41f4b71Sopenharmony_ci**Error codes** 3615e41f4b71Sopenharmony_ci 3616e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3617e41f4b71Sopenharmony_ci 3618e41f4b71Sopenharmony_ci| ID| Error Message | 3619e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3620e41f4b71Sopenharmony_ci| 10200011 | The includes method cannot be bound. | 3621e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3622e41f4b71Sopenharmony_ci 3623e41f4b71Sopenharmony_ci**Example** 3624e41f4b71Sopenharmony_ci 3625e41f4b71Sopenharmony_ci```ts 3626e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3]); 3627e41f4b71Sopenharmony_ciconsole.info("includes: " + array.includes(2)); // includes: true 3628e41f4b71Sopenharmony_ciconsole.info("includes: " + array.includes(4)); // includes: false 3629e41f4b71Sopenharmony_ciconsole.info("includes: " + array.includes(3, 3)); // includes: false 3630e41f4b71Sopenharmony_ci``` 3631e41f4b71Sopenharmony_ci 3632e41f4b71Sopenharmony_ci### entries 3633e41f4b71Sopenharmony_cientries(): IterableIterator\<[number, number]> 3634e41f4b71Sopenharmony_ci 3635e41f4b71Sopenharmony_ciReturns an iterator object that contains the key-value pair of each element in this ArkTS typed array. 3636e41f4b71Sopenharmony_ci 3637e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3638e41f4b71Sopenharmony_ci 3639e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3640e41f4b71Sopenharmony_ci 3641e41f4b71Sopenharmony_ci**Return value** 3642e41f4b71Sopenharmony_ci 3643e41f4b71Sopenharmony_ci| Type | Description | 3644e41f4b71Sopenharmony_ci| ------------ | --------- | 3645e41f4b71Sopenharmony_ci| IterableIterator\<[number, number]>| Iterator object.| 3646e41f4b71Sopenharmony_ci 3647e41f4b71Sopenharmony_ci**Error codes** 3648e41f4b71Sopenharmony_ci 3649e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3650e41f4b71Sopenharmony_ci 3651e41f4b71Sopenharmony_ci| ID| Error Message | 3652e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3653e41f4b71Sopenharmony_ci| 10200011 | The entries method cannot be bound. | 3654e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3655e41f4b71Sopenharmony_ci 3656e41f4b71Sopenharmony_ci**Example** 3657e41f4b71Sopenharmony_ci 3658e41f4b71Sopenharmony_ci```ts 3659e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([11, 22, 33]); 3660e41f4b71Sopenharmony_cilet iterator: IterableIterator<[number, number]> = array.entries(); 3661e41f4b71Sopenharmony_ciconsole.info("value: " + iterator.next().value); // value: [0, 11] 3662e41f4b71Sopenharmony_ciconsole.info("value: " + iterator.next().value); // value: [1, 22] 3663e41f4b71Sopenharmony_ciconsole.info("value: " + iterator.next().value); // value: [2, 33] 3664e41f4b71Sopenharmony_ci``` 3665e41f4b71Sopenharmony_ci 3666e41f4b71Sopenharmony_ci### keys 3667e41f4b71Sopenharmony_cikeys(): IterableIterator\<number> 3668e41f4b71Sopenharmony_ci 3669e41f4b71Sopenharmony_ciReturns an iterator object that contains the key (index) of each element in this ArkTS typed array. 3670e41f4b71Sopenharmony_ci 3671e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3672e41f4b71Sopenharmony_ci 3673e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3674e41f4b71Sopenharmony_ci 3675e41f4b71Sopenharmony_ci**Return value** 3676e41f4b71Sopenharmony_ci 3677e41f4b71Sopenharmony_ci| Type | Description | 3678e41f4b71Sopenharmony_ci| ------------ | --------- | 3679e41f4b71Sopenharmony_ci| IterableIterator\<number> | Iterator object.| 3680e41f4b71Sopenharmony_ci 3681e41f4b71Sopenharmony_ci**Error codes** 3682e41f4b71Sopenharmony_ci 3683e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3684e41f4b71Sopenharmony_ci 3685e41f4b71Sopenharmony_ci| ID| Error Message | 3686e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3687e41f4b71Sopenharmony_ci| 10200011 | The keys method cannot be bound. | 3688e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3689e41f4b71Sopenharmony_ci 3690e41f4b71Sopenharmony_ci**Example** 3691e41f4b71Sopenharmony_ci 3692e41f4b71Sopenharmony_ci```ts 3693e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3694e41f4b71Sopenharmony_cilet iterator: IterableIterator<number> = array.keys(); 3695e41f4b71Sopenharmony_cifor (const key of iterator) { 3696e41f4b71Sopenharmony_ci console.info("" + key); // 0, 1, 2, 3, and 4 are returned in sequence. 3697e41f4b71Sopenharmony_ci} 3698e41f4b71Sopenharmony_ci``` 3699e41f4b71Sopenharmony_ci 3700e41f4b71Sopenharmony_ci### values 3701e41f4b71Sopenharmony_civalues(): IterableIterator\<number> 3702e41f4b71Sopenharmony_ci 3703e41f4b71Sopenharmony_ciReturns an iterator object that contains the value of each element in this ArkTS typed array. 3704e41f4b71Sopenharmony_ci 3705e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3706e41f4b71Sopenharmony_ci 3707e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3708e41f4b71Sopenharmony_ci 3709e41f4b71Sopenharmony_ci**Return value** 3710e41f4b71Sopenharmony_ci 3711e41f4b71Sopenharmony_ci| Type | Description | 3712e41f4b71Sopenharmony_ci| ------------ | --------- | 3713e41f4b71Sopenharmony_ci| IterableIterator\<number> | Iterator object.| 3714e41f4b71Sopenharmony_ci 3715e41f4b71Sopenharmony_ci**Error codes** 3716e41f4b71Sopenharmony_ci 3717e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3718e41f4b71Sopenharmony_ci 3719e41f4b71Sopenharmony_ci| ID| Error Message | 3720e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------- | 3721e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound. | 3722e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification exception. | 3723e41f4b71Sopenharmony_ci 3724e41f4b71Sopenharmony_ci**Example** 3725e41f4b71Sopenharmony_ci 3726e41f4b71Sopenharmony_ci```ts 3727e41f4b71Sopenharmony_cilet array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]); 3728e41f4b71Sopenharmony_cilet iterator: IterableIterator<number> = array.values(); 3729e41f4b71Sopenharmony_cifor (const value of iterator) { 3730e41f4b71Sopenharmony_ci console.info("" + value); // 1, 2, 3, 4, and 5 are returned in sequence. 3731e41f4b71Sopenharmony_ci} 3732e41f4b71Sopenharmony_ci``` 3733e41f4b71Sopenharmony_ci 3734e41f4b71Sopenharmony_ci### [Symbol.iterator] 3735e41f4b71Sopenharmony_ci 3736e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<number> 3737e41f4b71Sopenharmony_ci 3738e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object. 3739e41f4b71Sopenharmony_ci 3740e41f4b71Sopenharmony_ci> **NOTE** 3741e41f4b71Sopenharmony_ci> 3742e41f4b71Sopenharmony_ci> This API cannot be used in .ets files. 3743e41f4b71Sopenharmony_ci 3744e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3745e41f4b71Sopenharmony_ci 3746e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3747e41f4b71Sopenharmony_ci 3748e41f4b71Sopenharmony_ci**Return value** 3749e41f4b71Sopenharmony_ci 3750e41f4b71Sopenharmony_ci| Type | Description | 3751e41f4b71Sopenharmony_ci| ------------------------- | ---------------- | 3752e41f4b71Sopenharmony_ci| IterableIterator<T> | Iterator obtained.| 3753e41f4b71Sopenharmony_ci 3754e41f4b71Sopenharmony_ci**Error codes** 3755e41f4b71Sopenharmony_ci 3756e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3757e41f4b71Sopenharmony_ci 3758e41f4b71Sopenharmony_ci| ID| Error Message | 3759e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | 3760e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. | 3761e41f4b71Sopenharmony_ci 3762e41f4b71Sopenharmony_ci**Example** 3763e41f4b71Sopenharmony_ci 3764e41f4b71Sopenharmony_ci```ts 3765e41f4b71Sopenharmony_cilet int32Array: collections.Int32Array = collections.Int32Array.from([1, 2, 3, 4, 5, 6]); 3766e41f4b71Sopenharmony_ci 3767e41f4b71Sopenharmony_cifor (let item of int32Array) { 3768e41f4b71Sopenharmony_ci console.info(`value : ${item}`); 3769e41f4b71Sopenharmony_ci} 3770e41f4b71Sopenharmony_ci``` 3771e41f4b71Sopenharmony_ci 3772e41f4b71Sopenharmony_ci### [index: number] 3773e41f4b71Sopenharmony_ci 3774e41f4b71Sopenharmony_ci[index: number]: number 3775e41f4b71Sopenharmony_ci 3776e41f4b71Sopenharmony_ciReturns the element at a given index in this TypedArray. This API is applicable to Int8Array, Int16Array, Int32Array, Uint8Array, Uint16Array, Uint32Array, Float32Array, and Float64Array. 3777e41f4b71Sopenharmony_ci 3778e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3779e41f4b71Sopenharmony_ci 3780e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3781e41f4b71Sopenharmony_ci 3782e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3783e41f4b71Sopenharmony_ci| ----- | ------ | ---- | -------------------------- | 3784e41f4b71Sopenharmony_ci| index | number | Yes | Index of the element. The index starts from zero.| 3785e41f4b71Sopenharmony_ci 3786e41f4b71Sopenharmony_ci**Return value** 3787e41f4b71Sopenharmony_ci 3788e41f4b71Sopenharmony_ci| Type | Description | 3789e41f4b71Sopenharmony_ci| ----- | ---------------------| 3790e41f4b71Sopenharmony_ci| number | Number data type.| 3791e41f4b71Sopenharmony_ci 3792e41f4b71Sopenharmony_ci**Example** 3793e41f4b71Sopenharmony_ci 3794e41f4b71Sopenharmony_ci```ts 3795e41f4b71Sopenharmony_cilet int8Array = collections.Int8Array.from([1, 2, 4]); 3796e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", int8Array[1]); 3797e41f4b71Sopenharmony_cilet int16Array = collections.Int16Array.from([1, 2, 4]); 3798e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", int16Array[1]); 3799e41f4b71Sopenharmony_cilet int32Array = collections.Int32Array.from([1, 2, 4]); 3800e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", int32Array[1]); 3801e41f4b71Sopenharmony_cilet uint8Array = collections.Uint8Array.from([1, 2, 4]); 3802e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", uint8Array[1]); 3803e41f4b71Sopenharmony_cilet uint16Array = collections.Uint16Array.from([1, 2, 4]); 3804e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", uint16Array[1]); 3805e41f4b71Sopenharmony_cilet uint32Array = collections.Uint32Array.from([1, 2, 4]); 3806e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", uint32Array[1]); 3807e41f4b71Sopenharmony_cilet float32Array = collections.Float32Array.from([1, 2, 4]); 3808e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", float32Array[1]); 3809e41f4b71Sopenharmony_cilet uint8Clamped = collections.Uint8ClampedArray.from([1, 2, 4]); 3810e41f4b71Sopenharmony_ciconsole.info("Element at index 1: ", uint8Clamped[1]); 3811e41f4b71Sopenharmony_ci``` 3812e41f4b71Sopenharmony_ci 3813e41f4b71Sopenharmony_ci## collections.BitVector 3814e41f4b71Sopenharmony_ci 3815e41f4b71Sopenharmony_ciA linear data structure that is implemented on arrays. A bit vector stores bit values and provides bit-level storage and processing. 3816e41f4b71Sopenharmony_ci 3817e41f4b71Sopenharmony_ci### Properties 3818e41f4b71Sopenharmony_ci 3819e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3820e41f4b71Sopenharmony_ci 3821e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3822e41f4b71Sopenharmony_ci 3823e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 3824e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---- | --------------------- | 3825e41f4b71Sopenharmony_ci| length | number | Yes | No | Number of elements in a bit vector.| 3826e41f4b71Sopenharmony_ci 3827e41f4b71Sopenharmony_ci 3828e41f4b71Sopenharmony_ci### constructor 3829e41f4b71Sopenharmony_ci 3830e41f4b71Sopenharmony_ciconstructor(length: number) 3831e41f4b71Sopenharmony_ci 3832e41f4b71Sopenharmony_ciConstructor used to create a bit vector. 3833e41f4b71Sopenharmony_ci 3834e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3835e41f4b71Sopenharmony_ci 3836e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3837e41f4b71Sopenharmony_ci 3838e41f4b71Sopenharmony_ci**Parameters** 3839e41f4b71Sopenharmony_ci 3840e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 3841e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ----------------------- | 3842e41f4b71Sopenharmony_ci| length | number | Yes | Length of the bit vector.| 3843e41f4b71Sopenharmony_ci 3844e41f4b71Sopenharmony_ci**Example** 3845e41f4b71Sopenharmony_ci 3846e41f4b71Sopenharmony_ci```ts 3847e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 3848e41f4b71Sopenharmony_ci``` 3849e41f4b71Sopenharmony_ci 3850e41f4b71Sopenharmony_ci 3851e41f4b71Sopenharmony_ci### push 3852e41f4b71Sopenharmony_ci 3853e41f4b71Sopenharmony_cipush(element:number): boolean 3854e41f4b71Sopenharmony_ci 3855e41f4b71Sopenharmony_ciAdds an element at the end of this bit vector. 3856e41f4b71Sopenharmony_ci 3857e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3858e41f4b71Sopenharmony_ci 3859e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3860e41f4b71Sopenharmony_ci 3861e41f4b71Sopenharmony_ci**Parameters** 3862e41f4b71Sopenharmony_ci 3863e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3864e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ----------------------------------- | 3865e41f4b71Sopenharmony_ci| element | number | Yes | Element to add. The value **0** indicates bit value 0, and other values indicate bit value 1.| 3866e41f4b71Sopenharmony_ci 3867e41f4b71Sopenharmony_ci**Return value** 3868e41f4b71Sopenharmony_ci 3869e41f4b71Sopenharmony_ci| Type | Description | 3870e41f4b71Sopenharmony_ci| ------- | --------------------------------- | 3871e41f4b71Sopenharmony_ci| boolean | **true**: The element is added.<br>**false**: The element fails to add.| 3872e41f4b71Sopenharmony_ci 3873e41f4b71Sopenharmony_ci**Error codes** 3874e41f4b71Sopenharmony_ci 3875e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3876e41f4b71Sopenharmony_ci 3877e41f4b71Sopenharmony_ci| ID| Error Message | 3878e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 3879e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 3880e41f4b71Sopenharmony_ci| 10200011 | The push method cannot be bound. | 3881e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 3882e41f4b71Sopenharmony_ci 3883e41f4b71Sopenharmony_ci**Example** 3884e41f4b71Sopenharmony_ci 3885e41f4b71Sopenharmony_ci```ts 3886e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 3887e41f4b71Sopenharmony_cibitVector.push(0); 3888e41f4b71Sopenharmony_cibitVector.push(1); 3889e41f4b71Sopenharmony_cibitVector.push(0); 3890e41f4b71Sopenharmony_cibitVector.push(1); 3891e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 3892e41f4b71Sopenharmony_ci``` 3893e41f4b71Sopenharmony_ci 3894e41f4b71Sopenharmony_ci### pop 3895e41f4b71Sopenharmony_ci 3896e41f4b71Sopenharmony_cipop(): number 3897e41f4b71Sopenharmony_ci 3898e41f4b71Sopenharmony_ciRemoves the last element from this bit vector. 3899e41f4b71Sopenharmony_ci 3900e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3901e41f4b71Sopenharmony_ci 3902e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3903e41f4b71Sopenharmony_ci 3904e41f4b71Sopenharmony_ci**Return value** 3905e41f4b71Sopenharmony_ci 3906e41f4b71Sopenharmony_ci| Type | Description | 3907e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | 3908e41f4b71Sopenharmony_ci| number | Element (bit value) removed.| 3909e41f4b71Sopenharmony_ci 3910e41f4b71Sopenharmony_ci**Error codes** 3911e41f4b71Sopenharmony_ci 3912e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 3913e41f4b71Sopenharmony_ci 3914e41f4b71Sopenharmony_ci| ID| Error Message | 3915e41f4b71Sopenharmony_ci| -------- | ------------------------------- | 3916e41f4b71Sopenharmony_ci| 10200011 | The pop method cannot be bound. | 3917e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 3918e41f4b71Sopenharmony_ci 3919e41f4b71Sopenharmony_ci**Example** 3920e41f4b71Sopenharmony_ci 3921e41f4b71Sopenharmony_ci```ts 3922e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 3923e41f4b71Sopenharmony_cibitVector.push(0); 3924e41f4b71Sopenharmony_cibitVector.push(1); 3925e41f4b71Sopenharmony_cibitVector.push(0); 3926e41f4b71Sopenharmony_cibitVector.push(1); 3927e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 3928e41f4b71Sopenharmony_cilet res = bitVector.pop(); // bitVector: [0, 1, 0, 1] 3929e41f4b71Sopenharmony_ciconsole.info("bitVector pop:", res) // 0 3930e41f4b71Sopenharmony_ci``` 3931e41f4b71Sopenharmony_ci 3932e41f4b71Sopenharmony_ci### has 3933e41f4b71Sopenharmony_ci 3934e41f4b71Sopenharmony_cihas(element: number, fromIndex: number, toIndex: number): boolean 3935e41f4b71Sopenharmony_ci 3936e41f4b71Sopenharmony_ciChecks whether a bit value is included in a given range of this bit vector. 3937e41f4b71Sopenharmony_ci 3938e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3939e41f4b71Sopenharmony_ci 3940e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3941e41f4b71Sopenharmony_ci 3942e41f4b71Sopenharmony_ci**Parameters** 3943e41f4b71Sopenharmony_ci 3944e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3945e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------------ | 3946e41f4b71Sopenharmony_ci| element | number | Yes | Bit value. The value **0** indicates bit value 0, and other values indicate bit value 1.| 3947e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 3948e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive). | 3949e41f4b71Sopenharmony_ci 3950e41f4b71Sopenharmony_ci**Return value** 3951e41f4b71Sopenharmony_ci 3952e41f4b71Sopenharmony_ci| Type | Description | 3953e41f4b71Sopenharmony_ci| ------- | -------------------------------------- | 3954e41f4b71Sopenharmony_ci| boolean | **true**: The bit value exists.<br>**false**: The bit value does not exist.| 3955e41f4b71Sopenharmony_ci 3956e41f4b71Sopenharmony_ci**Error codes** 3957e41f4b71Sopenharmony_ci 3958e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 3959e41f4b71Sopenharmony_ci 3960e41f4b71Sopenharmony_ci| ID| Error Message | 3961e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 3962e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 3963e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 3964e41f4b71Sopenharmony_ci| 10200011 | The has method cannot be bound. | 3965e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 3966e41f4b71Sopenharmony_ci 3967e41f4b71Sopenharmony_ci**Example** 3968e41f4b71Sopenharmony_ci 3969e41f4b71Sopenharmony_ci```ts 3970e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 3971e41f4b71Sopenharmony_cibitVector.push(0); 3972e41f4b71Sopenharmony_cibitVector.push(1); 3973e41f4b71Sopenharmony_cibitVector.push(0); 3974e41f4b71Sopenharmony_cibitVector.push(1); 3975e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 3976e41f4b71Sopenharmony_cilet res0: boolean = bitVector.has(0, 1, 4); 3977e41f4b71Sopenharmony_ciconsole.info("bitVector has 0:", res0) // true 3978e41f4b71Sopenharmony_ci``` 3979e41f4b71Sopenharmony_ci 3980e41f4b71Sopenharmony_ci### setBitsByRange 3981e41f4b71Sopenharmony_ci 3982e41f4b71Sopenharmony_cisetBitsByRange(element: number, fromIndex: number, toIndex: number): void 3983e41f4b71Sopenharmony_ci 3984e41f4b71Sopenharmony_ciSets elements in a given range in this bit vector to a bit value. 3985e41f4b71Sopenharmony_ci 3986e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 3987e41f4b71Sopenharmony_ci 3988e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 3989e41f4b71Sopenharmony_ci 3990e41f4b71Sopenharmony_ci**Parameters** 3991e41f4b71Sopenharmony_ci 3992e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 3993e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ---------------------------------- | 3994e41f4b71Sopenharmony_ci| element | number | Yes | Bit value to set. The value **0** indicates bit value 0, and other values indicate bit value 1.| 3995e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 3996e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive). | 3997e41f4b71Sopenharmony_ci 3998e41f4b71Sopenharmony_ci**Error codes** 3999e41f4b71Sopenharmony_ci 4000e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4001e41f4b71Sopenharmony_ci 4002e41f4b71Sopenharmony_ci| ID| Error Message | 4003e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4004e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4005e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 4006e41f4b71Sopenharmony_ci| 10200011 | The setBitsByRange method cannot be bound. | 4007e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4008e41f4b71Sopenharmony_ci 4009e41f4b71Sopenharmony_ci**Example** 4010e41f4b71Sopenharmony_ci 4011e41f4b71Sopenharmony_ci```ts 4012e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4013e41f4b71Sopenharmony_cibitVector.push(0); 4014e41f4b71Sopenharmony_cibitVector.push(1); 4015e41f4b71Sopenharmony_cibitVector.push(0); 4016e41f4b71Sopenharmony_cibitVector.push(1); 4017e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4018e41f4b71Sopenharmony_cibitVector.setBitsByRange(1, 1, 3); // bitVector: [0, 1, 1, 1, 0] 4019e41f4b71Sopenharmony_ci``` 4020e41f4b71Sopenharmony_ci 4021e41f4b71Sopenharmony_ci### setAllBits 4022e41f4b71Sopenharmony_ci 4023e41f4b71Sopenharmony_cisetAllBits(element: number): void 4024e41f4b71Sopenharmony_ci 4025e41f4b71Sopenharmony_ciSets all elements in this bit vector to a bit value. 4026e41f4b71Sopenharmony_ci 4027e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4028e41f4b71Sopenharmony_ci 4029e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4030e41f4b71Sopenharmony_ci 4031e41f4b71Sopenharmony_ci**Parameters** 4032e41f4b71Sopenharmony_ci 4033e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4034e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ----------------------------------- | 4035e41f4b71Sopenharmony_ci| element | number | Yes | Bit value to set. The value **0** indicates bit value 0, and other values indicate bit value 1.| 4036e41f4b71Sopenharmony_ci 4037e41f4b71Sopenharmony_ci**Error codes** 4038e41f4b71Sopenharmony_ci 4039e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4040e41f4b71Sopenharmony_ci 4041e41f4b71Sopenharmony_ci| ID| Error Message | 4042e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4043e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4044e41f4b71Sopenharmony_ci| 10200011 | The setAllBits method cannot be bound. | 4045e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4046e41f4b71Sopenharmony_ci 4047e41f4b71Sopenharmony_ci**Example** 4048e41f4b71Sopenharmony_ci 4049e41f4b71Sopenharmony_ci```ts 4050e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4051e41f4b71Sopenharmony_cibitVector.push(0); 4052e41f4b71Sopenharmony_cibitVector.push(1); 4053e41f4b71Sopenharmony_cibitVector.push(0); 4054e41f4b71Sopenharmony_cibitVector.push(1); 4055e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4056e41f4b71Sopenharmony_cibitVector.setAllBits(1); // bitVector: [1, 1, 1, 1, 1] 4057e41f4b71Sopenharmony_ci``` 4058e41f4b71Sopenharmony_ci 4059e41f4b71Sopenharmony_ci### getBitsByRange 4060e41f4b71Sopenharmony_ci 4061e41f4b71Sopenharmony_cigetBitsByRange(fromIndex: number, toIndex: number): BitVector 4062e41f4b71Sopenharmony_ci 4063e41f4b71Sopenharmony_ciObtains bit values within a given range of this bit vector. 4064e41f4b71Sopenharmony_ci 4065e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4066e41f4b71Sopenharmony_ci 4067e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4068e41f4b71Sopenharmony_ci 4069e41f4b71Sopenharmony_ci**Parameters** 4070e41f4b71Sopenharmony_ci 4071e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4072e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------ | 4073e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 4074e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive).| 4075e41f4b71Sopenharmony_ci 4076e41f4b71Sopenharmony_ci**Return value** 4077e41f4b71Sopenharmony_ci 4078e41f4b71Sopenharmony_ci| Type | Description | 4079e41f4b71Sopenharmony_ci| --------- | ---------------------------------- | 4080e41f4b71Sopenharmony_ci| BitVector | Bit vector containing the bit values obtained.| 4081e41f4b71Sopenharmony_ci 4082e41f4b71Sopenharmony_ci**Error codes** 4083e41f4b71Sopenharmony_ci 4084e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4085e41f4b71Sopenharmony_ci 4086e41f4b71Sopenharmony_ci| ID| Error Message | 4087e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4088e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4089e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 4090e41f4b71Sopenharmony_ci| 10200011 | The getBitsByRange method cannot be bound. | 4091e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4092e41f4b71Sopenharmony_ci 4093e41f4b71Sopenharmony_ci**Example** 4094e41f4b71Sopenharmony_ci 4095e41f4b71Sopenharmony_ci```ts 4096e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4097e41f4b71Sopenharmony_cibitVector.push(0); 4098e41f4b71Sopenharmony_cibitVector.push(1); 4099e41f4b71Sopenharmony_cibitVector.push(0); 4100e41f4b71Sopenharmony_cibitVector.push(1); 4101e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4102e41f4b71Sopenharmony_cilet bitVector2 = bitVector.getBitsByRange(1, 3); // bitVector2: [1, 0] 4103e41f4b71Sopenharmony_ciconsole.info("bitVector2 length:", bitVector2.length) // 2 4104e41f4b71Sopenharmony_ci``` 4105e41f4b71Sopenharmony_ci 4106e41f4b71Sopenharmony_ci### resize 4107e41f4b71Sopenharmony_ci 4108e41f4b71Sopenharmony_ciresize(size: number): void 4109e41f4b71Sopenharmony_ci 4110e41f4b71Sopenharmony_ciResizes this bit vector. 4111e41f4b71Sopenharmony_ci 4112e41f4b71Sopenharmony_ciIf **size** is greater than the length of the existing bit vector, the bit vector is extended, and elements of the extra part are set to 0. 4113e41f4b71Sopenharmony_ci 4114e41f4b71Sopenharmony_ciIf **size** is less than or equal to the length of the existing bit vector, the bit vector is shrunk according to the size. 4115e41f4b71Sopenharmony_ci 4116e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4117e41f4b71Sopenharmony_ci 4118e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4119e41f4b71Sopenharmony_ci 4120e41f4b71Sopenharmony_ci**Parameters** 4121e41f4b71Sopenharmony_ci 4122e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 4123e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------- | 4124e41f4b71Sopenharmony_ci| size | number | Yes | New length.| 4125e41f4b71Sopenharmony_ci 4126e41f4b71Sopenharmony_ci**Error codes** 4127e41f4b71Sopenharmony_ci 4128e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4129e41f4b71Sopenharmony_ci 4130e41f4b71Sopenharmony_ci| ID| Error Message | 4131e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4132e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4133e41f4b71Sopenharmony_ci| 10200011 | The resize method cannot be bound. | 4134e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4135e41f4b71Sopenharmony_ci 4136e41f4b71Sopenharmony_ci**Example** 4137e41f4b71Sopenharmony_ci 4138e41f4b71Sopenharmony_ci```ts 4139e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4140e41f4b71Sopenharmony_cibitVector.push(0); 4141e41f4b71Sopenharmony_cibitVector.push(1); 4142e41f4b71Sopenharmony_cibitVector.push(0); 4143e41f4b71Sopenharmony_cibitVector.push(1); 4144e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4145e41f4b71Sopenharmony_cibitVector.resize(10); // bitVector: [0, 1, 0, 1, 0, 0, 0, 0, 0, 0] 4146e41f4b71Sopenharmony_ciconsole.info("bitVector get bit vector's length:", bitVector.length) // 10 4147e41f4b71Sopenharmony_cibitVector.resize(3); // bitVector: [0, 1, 0] 4148e41f4b71Sopenharmony_ciconsole.info("bitVector get bit vector's length:", bitVector.length) // 3 4149e41f4b71Sopenharmony_ci``` 4150e41f4b71Sopenharmony_ci 4151e41f4b71Sopenharmony_ci### getBitCountByRange 4152e41f4b71Sopenharmony_ci 4153e41f4b71Sopenharmony_cigetBitCountByRange(element: number, fromIndex: number, toIndex: number): number 4154e41f4b71Sopenharmony_ci 4155e41f4b71Sopenharmony_ciCounts the number of bit values in a given range of this bit vector. 4156e41f4b71Sopenharmony_ci 4157e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4158e41f4b71Sopenharmony_ci 4159e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4160e41f4b71Sopenharmony_ci 4161e41f4b71Sopenharmony_ci**Parameters** 4162e41f4b71Sopenharmony_ci 4163e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4164e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------------ | 4165e41f4b71Sopenharmony_ci| element | number | Yes | Bit value. The value **0** indicates bit value 0, and other values indicate bit value 1.| 4166e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 4167e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive). | 4168e41f4b71Sopenharmony_ci 4169e41f4b71Sopenharmony_ci**Return value** 4170e41f4b71Sopenharmony_ci 4171e41f4b71Sopenharmony_ci| Type | Description | 4172e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | 4173e41f4b71Sopenharmony_ci| number | Number of bit values.| 4174e41f4b71Sopenharmony_ci 4175e41f4b71Sopenharmony_ci**Error codes** 4176e41f4b71Sopenharmony_ci 4177e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4178e41f4b71Sopenharmony_ci 4179e41f4b71Sopenharmony_ci| ID| Error Message | 4180e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4181e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4182e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 4183e41f4b71Sopenharmony_ci| 10200011 | The getBitCountByRange method cannot be bound. | 4184e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4185e41f4b71Sopenharmony_ci 4186e41f4b71Sopenharmony_ci**Example** 4187e41f4b71Sopenharmony_ci 4188e41f4b71Sopenharmony_ci```ts 4189e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4190e41f4b71Sopenharmony_cibitVector.push(0); 4191e41f4b71Sopenharmony_cibitVector.push(1); 4192e41f4b71Sopenharmony_cibitVector.push(0); 4193e41f4b71Sopenharmony_cibitVector.push(1); 4194e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4195e41f4b71Sopenharmony_cilet res: number = bitVector.getBitCountByRange(1, 1, 4); 4196e41f4b71Sopenharmony_ciconsole.info("bitVector getBitCountByRange:", res) // 2 4197e41f4b71Sopenharmony_ci``` 4198e41f4b71Sopenharmony_ci 4199e41f4b71Sopenharmony_ci### getIndexOf 4200e41f4b71Sopenharmony_ci 4201e41f4b71Sopenharmony_cigetIndexOf(element: number, fromIndex: number, toIndex: number): number 4202e41f4b71Sopenharmony_ci 4203e41f4b71Sopenharmony_ciReturns the index of the first occurrence of a bit value in this bit vector. If the bit value is not found, **-1** is returned. 4204e41f4b71Sopenharmony_ci 4205e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4206e41f4b71Sopenharmony_ci 4207e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4208e41f4b71Sopenharmony_ci 4209e41f4b71Sopenharmony_ci**Parameters** 4210e41f4b71Sopenharmony_ci 4211e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4212e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------------ | 4213e41f4b71Sopenharmony_ci| element | number | Yes | Bit value. The value **0** indicates bit value 0, and other values indicate bit value 1.| 4214e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 4215e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive). | 4216e41f4b71Sopenharmony_ci 4217e41f4b71Sopenharmony_ci**Return value** 4218e41f4b71Sopenharmony_ci 4219e41f4b71Sopenharmony_ci| Type | Description | 4220e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------- | 4221e41f4b71Sopenharmony_ci| number | Index of the first occurrence of the bit value. If the bit value is not found, **-1** is returned.| 4222e41f4b71Sopenharmony_ci 4223e41f4b71Sopenharmony_ci**Error codes** 4224e41f4b71Sopenharmony_ci 4225e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4226e41f4b71Sopenharmony_ci 4227e41f4b71Sopenharmony_ci| ID| Error Message | 4228e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4229e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4230e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 4231e41f4b71Sopenharmony_ci| 10200011 | The getIndexOf method cannot be bound. | 4232e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4233e41f4b71Sopenharmony_ci 4234e41f4b71Sopenharmony_ci**Example** 4235e41f4b71Sopenharmony_ci 4236e41f4b71Sopenharmony_ci```ts 4237e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4238e41f4b71Sopenharmony_cibitVector.push(0); 4239e41f4b71Sopenharmony_cibitVector.push(1); 4240e41f4b71Sopenharmony_cibitVector.push(0); 4241e41f4b71Sopenharmony_cibitVector.push(1); 4242e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4243e41f4b71Sopenharmony_cilet res: number = bitVector.getIndexOf(0, 1, 4); 4244e41f4b71Sopenharmony_ciconsole.info("bitVector getIndexOf:", res) // 2 4245e41f4b71Sopenharmony_ci``` 4246e41f4b71Sopenharmony_ci 4247e41f4b71Sopenharmony_ci### getLastIndexOf 4248e41f4b71Sopenharmony_ci 4249e41f4b71Sopenharmony_cigetLastIndexOf(element: number, fromIndex: number, toIndex: number): number 4250e41f4b71Sopenharmony_ci 4251e41f4b71Sopenharmony_ciReturns the index of the last occurrence of a bit value in this bit vector. If the bit value is not found, **-1** is returned. 4252e41f4b71Sopenharmony_ci 4253e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4254e41f4b71Sopenharmony_ci 4255e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4256e41f4b71Sopenharmony_ci 4257e41f4b71Sopenharmony_ci**Parameters** 4258e41f4b71Sopenharmony_ci 4259e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4260e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------------ | 4261e41f4b71Sopenharmony_ci| element | number | Yes | Bit value. The value **0** indicates bit value 0, and other values indicate bit value 1.| 4262e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 4263e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive). | 4264e41f4b71Sopenharmony_ci 4265e41f4b71Sopenharmony_ci**Return value** 4266e41f4b71Sopenharmony_ci 4267e41f4b71Sopenharmony_ci| Type | Description | 4268e41f4b71Sopenharmony_ci| ------ | ----------------------------------------------------- | 4269e41f4b71Sopenharmony_ci| number | Index of the last occurrence of the bit value. If the bit value is not found, **-1** is returned.| 4270e41f4b71Sopenharmony_ci 4271e41f4b71Sopenharmony_ci**Error codes** 4272e41f4b71Sopenharmony_ci 4273e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4274e41f4b71Sopenharmony_ci 4275e41f4b71Sopenharmony_ci| ID| Error Message | 4276e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4277e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4278e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 4279e41f4b71Sopenharmony_ci| 10200011 | The getLastIndexOf method cannot be bound. | 4280e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4281e41f4b71Sopenharmony_ci 4282e41f4b71Sopenharmony_ci**Example** 4283e41f4b71Sopenharmony_ci 4284e41f4b71Sopenharmony_ci```ts 4285e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4286e41f4b71Sopenharmony_cibitVector.push(0); 4287e41f4b71Sopenharmony_cibitVector.push(1); 4288e41f4b71Sopenharmony_cibitVector.push(0); 4289e41f4b71Sopenharmony_cibitVector.push(1); 4290e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4291e41f4b71Sopenharmony_cilet res: number = bitVector.getLastIndexOf(0, 1, 4); 4292e41f4b71Sopenharmony_ciconsole.info("bitVector getLastIndexOf:", res) // 2 4293e41f4b71Sopenharmony_ci``` 4294e41f4b71Sopenharmony_ci 4295e41f4b71Sopenharmony_ci### flipBitByIndex 4296e41f4b71Sopenharmony_ci 4297e41f4b71Sopenharmony_ciflipBitByIndex(index: number): void 4298e41f4b71Sopenharmony_ci 4299e41f4b71Sopenharmony_ciFlips the bit value (from 0 to 1 or from 1 to 0) at a given index in this bit vector. 4300e41f4b71Sopenharmony_ci 4301e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4302e41f4b71Sopenharmony_ci 4303e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4304e41f4b71Sopenharmony_ci 4305e41f4b71Sopenharmony_ci**Parameters** 4306e41f4b71Sopenharmony_ci 4307e41f4b71Sopenharmony_ci| Name| Type | Mandatory| Description | 4308e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------- | 4309e41f4b71Sopenharmony_ci| index | number | Yes | Index.| 4310e41f4b71Sopenharmony_ci 4311e41f4b71Sopenharmony_ci**Error codes** 4312e41f4b71Sopenharmony_ci 4313e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4314e41f4b71Sopenharmony_ci 4315e41f4b71Sopenharmony_ci| ID| Error Message | 4316e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4317e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4318e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. | 4319e41f4b71Sopenharmony_ci| 10200011 | The flipBitByIndex method cannot be bound. | 4320e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4321e41f4b71Sopenharmony_ci 4322e41f4b71Sopenharmony_ci**Example** 4323e41f4b71Sopenharmony_ci 4324e41f4b71Sopenharmony_ci```ts 4325e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4326e41f4b71Sopenharmony_cibitVector.push(0); 4327e41f4b71Sopenharmony_cibitVector.push(1); 4328e41f4b71Sopenharmony_cibitVector.push(0); 4329e41f4b71Sopenharmony_cibitVector.push(1); 4330e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4331e41f4b71Sopenharmony_cibitVector.flipBitByIndex(3); // bitVector: [0, 1, 0, 0, 0] 4332e41f4b71Sopenharmony_ci``` 4333e41f4b71Sopenharmony_ci 4334e41f4b71Sopenharmony_ci### flipBitsByRange 4335e41f4b71Sopenharmony_ci 4336e41f4b71Sopenharmony_ciflipBitsByRange(fromIndex: number, toIndex: number): void 4337e41f4b71Sopenharmony_ci 4338e41f4b71Sopenharmony_ciFlips the bit values (from 0 to 1 or from 1 to 0) in a given range in this bit vector. 4339e41f4b71Sopenharmony_ci 4340e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4341e41f4b71Sopenharmony_ci 4342e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4343e41f4b71Sopenharmony_ci 4344e41f4b71Sopenharmony_ci**Parameters** 4345e41f4b71Sopenharmony_ci 4346e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4347e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------ | 4348e41f4b71Sopenharmony_ci| fromIndex | number | Yes | Start index of the range (inclusive). | 4349e41f4b71Sopenharmony_ci| toIndex | number | Yes | End index of the range (exclusive).| 4350e41f4b71Sopenharmony_ci 4351e41f4b71Sopenharmony_ci**Error codes** 4352e41f4b71Sopenharmony_ci 4353e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 4354e41f4b71Sopenharmony_ci 4355e41f4b71Sopenharmony_ci| ID| Error Message | 4356e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------------ | 4357e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified;<br>2.Incorrect parameter types. | 4358e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. | 4359e41f4b71Sopenharmony_ci| 10200011 | The flipBitsByRange method cannot be bound. | 4360e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4361e41f4b71Sopenharmony_ci 4362e41f4b71Sopenharmony_ci**Example** 4363e41f4b71Sopenharmony_ci 4364e41f4b71Sopenharmony_ci```ts 4365e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4366e41f4b71Sopenharmony_cibitVector.push(0); 4367e41f4b71Sopenharmony_cibitVector.push(1); 4368e41f4b71Sopenharmony_cibitVector.push(0); 4369e41f4b71Sopenharmony_cibitVector.push(1); 4370e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4371e41f4b71Sopenharmony_cibitVector.flipBitsByRange(1, 4); // bitVector: [0, 0, 1, 0, 0] 4372e41f4b71Sopenharmony_ci``` 4373e41f4b71Sopenharmony_ci 4374e41f4b71Sopenharmony_ci### values 4375e41f4b71Sopenharmony_ci 4376e41f4b71Sopenharmony_civalues(): IterableIterator\<number> 4377e41f4b71Sopenharmony_ci 4378e41f4b71Sopenharmony_ciReturns an iterator object that contains the value of each element in this bit vector. 4379e41f4b71Sopenharmony_ci 4380e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4381e41f4b71Sopenharmony_ci 4382e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4383e41f4b71Sopenharmony_ci 4384e41f4b71Sopenharmony_ci**Return value** 4385e41f4b71Sopenharmony_ci 4386e41f4b71Sopenharmony_ci| Type | Description | 4387e41f4b71Sopenharmony_ci| ------------------------------ | ----------------------------- | 4388e41f4b71Sopenharmony_ci| IterableIterator<number> | Bit vector iterator object.| 4389e41f4b71Sopenharmony_ci 4390e41f4b71Sopenharmony_ci**Error codes** 4391e41f4b71Sopenharmony_ci 4392e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 4393e41f4b71Sopenharmony_ci 4394e41f4b71Sopenharmony_ci| ID| Error Message | 4395e41f4b71Sopenharmony_ci| -------- | ---------------------------------- | 4396e41f4b71Sopenharmony_ci| 10200011 | The values method cannot be bound. | 4397e41f4b71Sopenharmony_ci| 10200201 | Concurrent modification error. | 4398e41f4b71Sopenharmony_ci 4399e41f4b71Sopenharmony_ci**Example** 4400e41f4b71Sopenharmony_ci 4401e41f4b71Sopenharmony_ci```ts 4402e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4403e41f4b71Sopenharmony_cibitVector.push(0); 4404e41f4b71Sopenharmony_cibitVector.push(1); 4405e41f4b71Sopenharmony_cibitVector.push(0); 4406e41f4b71Sopenharmony_cibitVector.push(1); 4407e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4408e41f4b71Sopenharmony_cilet iter: IterableIterator<number> = bitVector.values(); 4409e41f4b71Sopenharmony_cilet temp: IteratorResult<number> = iter.next(); 4410e41f4b71Sopenharmony_ciwhile (!temp.done) { 4411e41f4b71Sopenharmony_ci console.info(JSON.stringify(temp.value)); 4412e41f4b71Sopenharmony_ci temp = iter.next(); 4413e41f4b71Sopenharmony_ci} // 0, 1, 0, 1, and 0 are returned in sequence. 4414e41f4b71Sopenharmony_ci``` 4415e41f4b71Sopenharmony_ci 4416e41f4b71Sopenharmony_ci### [Symbol.iterator] 4417e41f4b71Sopenharmony_ci 4418e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator<number> 4419e41f4b71Sopenharmony_ci 4420e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object. 4421e41f4b71Sopenharmony_ci 4422e41f4b71Sopenharmony_ci> **NOTE** 4423e41f4b71Sopenharmony_ci> 4424e41f4b71Sopenharmony_ci> This API cannot be used in .ets files. 4425e41f4b71Sopenharmony_ci 4426e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4427e41f4b71Sopenharmony_ci 4428e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4429e41f4b71Sopenharmony_ci 4430e41f4b71Sopenharmony_ci**Return value** 4431e41f4b71Sopenharmony_ci 4432e41f4b71Sopenharmony_ci| Type | Description | 4433e41f4b71Sopenharmony_ci| ------------------------- | ---------------- | 4434e41f4b71Sopenharmony_ci| IterableIterator<number> | Iterator obtained.| 4435e41f4b71Sopenharmony_ci 4436e41f4b71Sopenharmony_ci**Error codes** 4437e41f4b71Sopenharmony_ci 4438e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md). 4439e41f4b71Sopenharmony_ci 4440e41f4b71Sopenharmony_ci| ID| Error Message | 4441e41f4b71Sopenharmony_ci| -------- | ------------------------------------------- | 4442e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. | 4443e41f4b71Sopenharmony_ci 4444e41f4b71Sopenharmony_ci**Example** 4445e41f4b71Sopenharmony_ci 4446e41f4b71Sopenharmony_ci```ts 4447e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4448e41f4b71Sopenharmony_cibitVector.push(0); 4449e41f4b71Sopenharmony_cibitVector.push(1); 4450e41f4b71Sopenharmony_cibitVector.push(0); 4451e41f4b71Sopenharmony_cibitVector.push(1); 4452e41f4b71Sopenharmony_cibitVector.push(0); 4453e41f4b71Sopenharmony_ci 4454e41f4b71Sopenharmony_cifor (let item of bitVector) { 4455e41f4b71Sopenharmony_ci console.info("value: " + item); 4456e41f4b71Sopenharmony_ci} 4457e41f4b71Sopenharmony_ci``` 4458e41f4b71Sopenharmony_ci 4459e41f4b71Sopenharmony_ci### [index: number] 4460e41f4b71Sopenharmony_ci 4461e41f4b71Sopenharmony_ci[index: number]: number 4462e41f4b71Sopenharmony_ci 4463e41f4b71Sopenharmony_ciReturns the element at a given index in this BitVector. 4464e41f4b71Sopenharmony_ci 4465e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 4466e41f4b71Sopenharmony_ci 4467e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang 4468e41f4b71Sopenharmony_ci 4469e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 4470e41f4b71Sopenharmony_ci| ----- | ------ | ---- | -------------------------- | 4471e41f4b71Sopenharmony_ci| index | number | Yes | Index of the element. The index starts from zero.| 4472e41f4b71Sopenharmony_ci 4473e41f4b71Sopenharmony_ci**Return value** 4474e41f4b71Sopenharmony_ci 4475e41f4b71Sopenharmony_ci| Type | Description | 4476e41f4b71Sopenharmony_ci| ----- | ---------------------| 4477e41f4b71Sopenharmony_ci| number | Number data type.| 4478e41f4b71Sopenharmony_ci 4479e41f4b71Sopenharmony_ci**Example** 4480e41f4b71Sopenharmony_ci 4481e41f4b71Sopenharmony_ci```ts 4482e41f4b71Sopenharmony_cilet bitVector: collections.BitVector = new collections.BitVector(0); 4483e41f4b71Sopenharmony_cibitVector.push(0); 4484e41f4b71Sopenharmony_cibitVector.push(1); 4485e41f4b71Sopenharmony_cibitVector.push(0); 4486e41f4b71Sopenharmony_cibitVector.push(1); 4487e41f4b71Sopenharmony_cibitVector.push(0); // bitVector: [0, 1, 0, 1, 0] 4488e41f4b71Sopenharmony_ciconsole.info("BitVector Element Index at 1: " + bitVector[1]); 4489e41f4b71Sopenharmony_ci``` 4490