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