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