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