1# @arkts.collections (ArkTS容器集) 2 3本模块提供的ArkTS容器集,可以用于并发场景下的高性能数据传递。功能与JavaScript内建的对应容器类似,但ArkTS容器实例无法通过\.或者\[\]添加或更新属性。 4 5ArkTS容器在多个并发实例间传递时,其默认行为是引用传递,支持多个并发实例可以同时操作同一个容器实例。另外,也支持拷贝传递,即每个并发实例持有一个ArkTS容器实例。 6 7ArkTS容器并不是线程安全的,内部使用了fail-fast(快速失败)机制:当检测多个并发实例同时对容器进行结构性改变时,会触发异常。因此,在修改场景下,容器使用方需要使用ArkTS提供的异步锁机制保证ArkTS容器的安全访问。 8 9当前ArkTS容器集主要包含以下几种容器:[Array](#collectionsarray)、[Map](#collectionsmap)、[Set](#collectionsset)、[TypedArray](#collectionstypedarray)、[ArrayBuffer](#collectionsarraybuffer)、[BitVector](#collectionsbitvector)、[ConcatArray](#collectionsconcatarray)。 10 11> **说明:** 12> 13> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14 15## 导入模块 16 17```ts 18import { collections } from '@kit.ArkTS'; 19``` 20 21## ISendable 22 23type ISendable = lang.ISendable 24 25ISendable是所有Sendable类型(除`null`和`undefined`)的父类型。自身没有任何必须的方法和属性。 26 27**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 28 29**系统能力:** SystemCapability.Utils.Lang 30 31| 类型 | 说明 | 32| ------ | ------ | 33| [lang.ISendable](js-apis-arkts-lang.md#langisendable) | 所有Sendable类型的父类型。 | 34 35## collections.ConcatArray 36表示可以进行连接的类似数组的对象。该接口扩展了`ISendable`接口。 37 38文档中存在泛型的使用,涉及以下泛型标记符: 39 40- T:Type,支持[Sendable支持的数据类型](../../arkts-utils/arkts-sendable.md#sendable支持的数据类型)。 41 42### 属性 43 44**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 45 46**系统能力:** SystemCapability.Utils.Lang 47 48| 名称 | 类型 | 只读 | 可选 | 说明 | 49| ------ | ------ | ---- | ---- | ----------------- | 50| length | number | 是 | 否 | ConcatArray的元素个数。 | 51 52### [index: number] 53 54readonly [index: number]: T 55 56返回ConcatArray指定索引位置的元素。 57 58**系统能力:** SystemCapability.Utils.Lang 59 60| 参数名 | 类型 | 必填 | 说明 | 61| ----- | ------ | ---- | ------------------------------------------------------------------ | 62| index | number | 是 | 所需代码单元的从零开始的索引。当index<0 或者index>=length,则会抛出错误。 | 63 64**返回值:** 65 66| 类型 | 说明 | 67| ----- | ------------------------ | 68| T | ConcatArray给定的元素数据类型。| 69 70**错误码**: 71 72以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 73 74| 错误码ID | 错误信息 | 75| ------- | ------------------------------------ | 76| 401 | Parameter error. Illegal index. | 77| 10200001 | The value of index is out of range. | 78 79**示例:** 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 90将ConcatArray的所有元素连接成一个字符串,元素之间可以用指定的分隔符分隔。 91 92**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 93 94**系统能力:** SystemCapability.Utils.Lang 95 96**参数:** 97 98| 参数名 | 类型 | 必填 | 说明 | 99| --------- | ------ | ---- | ---------------------------------------------------- | 100| separator | string | 否 | 用于分隔ConcatArray元素的字符串。如果省略,则使用逗号分隔。 | 101 102**返回值:** 103 104| 类型 | 说明 | 105| ------ | ------------------------------------------------------------ | 106| string | 包含所有ConcatArray元素连接成的字符串。如果ConcatArray为空,则返回空字符串。 | 107 108**错误码**: 109 110以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 111 112| 错误码ID | 错误信息 | 113| ------- | -------- | 114| 401 | Parameter error. Invalid separator. | 115 116**示例:** 117 118```ts 119let concatArray : collections.ConcatArray<string> = new collections.Array<string>('a', 'b', 'c'); 120let joinedString = concatArray.join('-'); // 返回 "a-b-c" 121``` 122 123### slice 124 125slice(start?: number, end?: number): ConcatArray\<T> 126 127返回一个新的ConcatArray,该ConcatArray是原始ConcatArray的切片。 128 129**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 130 131**系统能力:** SystemCapability.Utils.Lang 132 133**参数:** 134| 参数名 | 类型 | 必填 | 说明 | 135| ------ | ------ | ---- | ------------------------------------------------------------ | 136| start | number | 否 | 开始索引。如果`start < 0`,则会从`start + array.length`位置开始。默认值为0。 | 137| end | number | 否 | 结束索引(不包括该元素)。如果`end < 0`,则会到`end + array.length`位置结束。默认为ArkTS Array的长度。 | 138 139**返回值:** 140 141| 类型 | 说明 | 142| --------- | -------------------------- | 143| ConcatArray\<T> | 包含原始ConcatArray切片的新ConcatArray。 | 144 145**错误码**: 146 147以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 148 149| 错误码ID | 错误信息 | 150| ------- | -------- | 151| 401 | Parameter error. Invalid `start` or `end` parameters. | 152 153**示例:** 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],原Array保持不变 158``` 159 160## collections.Array 161 162一种线性数据结构,底层基于数组实现,可以在ArkTS上并发实例间传递。 163 164当需要在ArkTS上并发实例间传递Array时,可以通过传递Array引用提升传递性能。 165 166文档中存在泛型的使用,涉及以下泛型标记符: 167 168- T:Type,支持[Sendable支持的数据类型](../../arkts-utils/arkts-sendable.md#sendable支持的数据类型)。 169 170**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 171 172### 属性 173 174**系统能力:** SystemCapability.Utils.Lang 175 176| 名称 | 类型 | 只读 | 可选 | 说明 | 177| ------ | ------ | ---- | ---- | ----------------- | 178| length | number | 是 | 否 | Array的元素个数。 | 179 180 181### constructor 182 183**构造函数** 184 185constructor() 186 187创建一个ArkTS Array的构造函数。 188 189**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 190 191**系统能力:** SystemCapability.Utils.Lang 192 193**错误码:** 194 195以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 196 197| 错误码ID | 错误信息 | 198| -------- | --------------------------------------------------- | 199| 10200012 | The Array's constructor cannot be directly invoked. | 200 201**示例:** 202 203```ts 204let array = new collections.Array<number>(); 205``` 206 207### constructor 208 209constructor(first: T, ...left: T[]) 210 211ArkTS Array的构造函数,通过开发者提供的元素进行初始化。 212 213**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 214 215**系统能力:** SystemCapability.Utils.Lang 216 217**参数:** 218 219| 参数名 | 类型 | 必填 | 说明 | 220| ------ | ---- | ---- | ------------------------------- | 221| first | T | 是 | 初始化ArkTS Array的第一个元素。 | 222| left | T[] | 否 | 初始化ArkTS Array的剩余元素。 | 223 224**错误码:** 225 226以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 227 228| 错误码ID | 错误信息 | 229| -------- | --------------------------------------------------- | 230| 10200012 | The Array's constructor cannot be directly invoked. | 231 232**示例:** 233 234```ts 235let array = new collections.Array<number>(1, 2, 3, 4); 236``` 237### constructor 238 239constructor(...items: T[]) 240 241ArkTS Array的构造函数,通过开发者提供的元素进行初始化。 242 243**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 244 245**系统能力:** SystemCapability.Utils.Lang 246 247**参数:** 248 249| 参数名 | 类型 | 必填 | 说明 | 250| ------ | ---- | ---- | ------------------------------- | 251| items | T[] | 否 | 初始化ArkTS Array的元素。 | 252 253**错误码:** 254 255以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 256 257| 错误码ID | 错误信息 | 258| -------- | --------------------------------------------------- | 259| 401 | Parameter error. | 260| 10200012 | The Array's constructor cannot be directly invoked. | 261 262**示例:** 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 273生成一个固定长度的Array,其中,每个元素的初始值为initialValue。 274 275**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 276 277**系统能力:** SystemCapability.Utils.Lang 278 279**参数:** 280 281| 参数名 | 类型 | 必填 | 说明 | 282| --------- | ------------- | ---- | ------------------------------- | 283| arrayLength | number | 是 | 用于构造ArkTS Array的长度。 | 284| initialValue | T | 是 | 用于填充ArkTS Array的值。 | 285 286**返回值:** 287 288| 类型 | 说明 | 289| --------- | ----------------------- | 290| Array\<T> | 新创建的ArkTS Array实例。 | 291 292**错误码:** 293 294以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 295 296| 错误码ID | 错误信息 | 297| -------- | -------------------------------- | 298| 10200011 | The create method cannot be bound. | 299 300**示例:** 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 310从一个实现了ArrayLike接口的对象创建一个新的ArkTS Array。 311 312**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 313 314**系统能力:** SystemCapability.Utils.Lang 315 316**参数:** 317 318| 参数名 | 类型 | 必填 | 说明 | 319| --------- | ------------- | ---- | ------------------------------- | 320| arrayLike | ArrayLike\<T> | 是 | 用于构造ArkTS Array的对象。 | 321 322**返回值:** 323 324| 类型 | 说明 | 325| --------- | ----------------------- | 326| Array\<T> | 新创建的ArkTS Array实例。 | 327 328**错误码:** 329 330以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 331 332| 错误码ID | 错误信息 | 333| -------- | -------------------------------- | 334| 10200011 | The from method cannot be bound. | 335 336**示例:** 337 338```ts 339// 正例 340let array : Array<string> = ['str1', 'str2', 'str3']; // 原生Array<T>,T是Sendable数据类型。 341let sendableArray = collections.Array.from<string>(array); // 返回Sendable Array<T> 342``` 343 344<!--code_no_check--> 345```ts 346// 反例 347let array : Array<Array<string>> = [['str1', 'str2', 'str3'], ['str4', 'str5', 'str6'], ['str7', 'str8', 'str9']]; // 原生Array<T>,T是非Sendable数据类型。 348let sendableArray = collections.Array.from<Array<string>>(array); // 打印异常信息:Parameter error.Only accept sendable value 349``` 350 351### pop 352 353pop(): T | undefined 354 355从ArkTS Array中移除并返回最后一个元素。如果Array为空,则返回undefined,且Array不发生变化。 356 357**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 358 359**系统能力:** SystemCapability.Utils.Lang 360 361**返回值:** 362 363| 类型 | 说明 | 364| -------------- | --------------------------------------------------- | 365| T \| undefined | 从Array中移除的元素;如果Array为空,则返回undefined。 | 366 367**错误码:** 368 369以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 370 371| 错误码ID | 错误信息 | 372| -------- | ------------------------------- | 373| 10200011 | The pop method cannot be bound. | 374| 10200201 | Concurrent modification error. | 375 376**示例:** 377 378```ts 379let array = new collections.Array<number>(1, 2, 3); 380let lastElement = array.pop(); // 返回3,Array变为[1, 2] 381``` 382 383### push 384 385push(...items: T[]): number 386 387在ArkTS Array的末尾添加一个或多个元素,并返回新的Array长度。 388 389**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 390 391**系统能力:** SystemCapability.Utils.Lang 392 393**参数:** 394 395| 参数名 | 类型 | 必填 | 说明 | 396| ------ | ---- | ---- | ---------------------------------- | 397| items | T[] | 是 | 要添加到Array末尾的一个或多个元素。 | 398 399**返回值:** 400 401| 类型 | 说明 | 402| ------ | ------------------ | 403| number | 返回新Array的长度。 | 404 405**错误码:** 406 407以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 408 409| 错误码ID | 错误信息 | 410| -------- | -------------------------------- | 411| 10200011 | The push method cannot be bound. | 412| 10200201 | Concurrent modification error. | 413 414**示例:** 415 416```ts 417let array = new collections.Array<number>(1, 2, 3); 418let length = array.push(4, 5); // 返回5,Array变为[1, 2, 3, 4, 5] 419``` 420 421### join 422 423join(separator?: string): string 424 425将ArkTS Array的所有元素连接成一个字符串,元素之间可以用指定的分隔符分隔。 426 427**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 428 429**系统能力:** SystemCapability.Utils.Lang 430 431**参数:** 432 433| 参数名 | 类型 | 必填 | 说明 | 434| --------- | ------ | ---- | ---------------------------------------------------- | 435| separator | string | 否 | 用于分隔Array元素的字符串。如果省略,则使用逗号分隔。 | 436 437**返回值:** 438 439| 类型 | 说明 | 440| ------ | ------------------------------------------------------------ | 441| string | 包含所有Array元素连接成的字符串。如果Array为空,则返回空字符串。 | 442 443**错误码:** 444 445以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 446 447| 错误码ID | 错误信息 | 448| -------- | -------------------------------- | 449| 10200011 | The join method cannot be bound. | 450| 10200201 | Concurrent modification error. | 451 452**示例:** 453 454```ts 455let array = new collections.Array<string>('a', 'b', 'c'); 456let joinedString = array.join('-'); // 返回 "a-b-c" 457``` 458 459### shift 460 461shift(): T | undefined 462 463从ArkTS Array中移除并返回第一个元素。如果Array为空,则返回undefined,且Array不发生变化。 464 465**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 466 467**系统能力:** SystemCapability.Utils.Lang 468 469**返回值:** 470 471| 类型 | 说明 | 472| -------------- | --------------------------------------------------- | 473| T \| undefined | 从Array中移除的元素;如果Array为空,则返回undefined。 | 474 475**错误码:** 476 477以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 478 479| 错误码ID | 错误信息 | 480| -------- | --------------------------------- | 481| 10200011 | The shift method cannot be bound. | 482| 10200201 | Concurrent modification error. | 483 484**示例:** 485 486```ts 487let array = new collections.Array<number>(1, 2, 3); 488let firstElement = array.shift(); // 返回1,Array变为[2, 3] 489``` 490 491### unshift 492 493unshift(...items: T[]): number 494 495在ArkTS Array的首端插入一个或多个元素,并返回新的Array长度。 496 497**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 498 499**系统能力:** SystemCapability.Utils.Lang 500 501**参数:** 502 503| 参数名 | 类型 | 必填 | 说明 | 504| ------ | ---- | ---- | ------------------------ | 505| items | T[] | 是 | 要插入到Array首端的元素。 | 506 507**返回值:** 508 509| 类型 | 说明 | 510| ------ | -------------- | 511| number | 新Array的长度。 | 512 513**错误码:** 514 515以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 516 517| 错误码ID | 错误信息 | 518| -------- | ----------------------------------- | 519| 10200011 | The unshift method cannot be bound. | 520| 10200201 | Concurrent modification error. | 521 522**示例:** 523 524```ts 525let array = new collections.Array<number>(1, 2, 3); 526let newLength = array.unshift(0); // 返回4,Array变为[0, 1, 2, 3] 527``` 528 529### slice 530 531slice(start?: number, end?: number): Array\<T> 532 533返回一个新的Array,该Array是原始ArkTS Array的切片。 534 535**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 536 537**系统能力:** SystemCapability.Utils.Lang 538 539**参数:** 540| 参数名 | 类型 | 必填 | 说明 | 541| ------ | ------ | ---- | ------------------------------------------------------------ | 542| start | number | 否 | 开始索引。如果`start < 0`,则会从`start + array.length`位置开始。默认值为0。 | 543| end | number | 否 | 结束索引(不包括该元素)。如果`end < 0`,则会到`end + array.length`位置结束。默认为ArkTS Array的长度。 | 544 545**返回值:** 546 547| 类型 | 说明 | 548| --------- | -------------------------- | 549| Array\<T> | 包含原始Array切片的新Array。 | 550 551**错误码:** 552 553以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 554 555| 错误码ID | 错误信息 | 556| -------- | --------------------------------- | 557| 10200011 | The slice method cannot be bound. | 558| 10200201 | Concurrent modification error. | 559 560**示例:** 561 562```ts 563let array = new collections.Array<number>(1, 2, 3, 4, 5); 564let slicedArray = array.slice(1, 3); // 返回[2, 3],Array保持不变 565``` 566 567### sort 568 569sort(compareFn?: (a: T, b: T) => number): Array\<T> 570 571对ArkTS Array进行排序,并返回排序后的Array。 572 573**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 574 575**系统能力:** SystemCapability.Utils.Lang 576 577**参数:** 578 579| 参数名 | 类型 | 必填 | 说明 | 580| --------- | ---------------------- | ---- | ------------------------------------------ | 581| compareFn | (a: T, b: T) => number | 否 | 用于确定元素顺序的函数。默认使用升序排序。 | 582 583**返回值:** 584 585| 类型 | 说明 | 586| --------- | -------------- | 587| Array\<T> | 排序后的Array。 | 588 589**错误码:** 590 591以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 592 593| 错误码ID | 错误信息 | 594| -------- | -------------------------------- | 595| 10200011 | The sort method cannot be bound. | 596| 10200201 | Concurrent modification error. | 597 598**示例:** 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 610返回在ArkTS Array中搜索元素首次出现的索引,如果不存在则返回-1。 611 612**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 613 614**系统能力:** SystemCapability.Utils.Lang 615 616**参数:** 617 618| 参数名 | 类型 | 必填 | 说明 | 619| ------------- | ------ | ---- | --------------------------- | 620| searchElement | T | 是 | 要搜索的值。 | 621| fromIndex | number | 否 | 开始搜索的索引。默认值为0。 | 622 623**返回值:** 624 625| 类型 | 说明 | 626| ------ | ---------------------------------------------- | 627| number | 搜索元素首次出现的索引;如果不存在,则返回-1。 | 628 629**错误码:** 630 631以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 632 633| 错误码ID | 错误信息 | 634| -------- | ----------------------------------- | 635| 10200011 | The indexOf method cannot be bound. | 636| 10200201 | Concurrent modification error. | 637 638**示例:** 639 640```ts 641let array = new collections.Array<string>('a', 'b', 'c'); 642let index = array.indexOf('b'); // 返回1,因为'b'在索引1的位置 643``` 644 645### forEach 646 647forEach(callbackFn: (value: T, index: number, array: Array\<T>) => void): void 648 649对Array中的每个元素执行提供的回调函数。 650 651**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 652 653**系统能力:** SystemCapability.Utils.Lang 654 655**参数:** 656 657| 参数名 | 类型 | 必填 | 说明 | 658| ---------- | --------------------------------------------------- | ---- | ------------------------------ | 659| callbackFn | (value: T, index: number, array: Array\<T>) => void | 是 | 用于对每个元素执行的回调函数。 | 660 661**错误码:** 662 663以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 664 665| 错误码ID | 错误信息 | 666| -------- | ----------------------------------- | 667| 10200011 | The forEach method cannot be bound. | 668| 10200201 | Concurrent modification error. | 669 670**示例:** 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 683对Array中的每个元素执行提供的回调函数,并返回一个新的Array,该Array包含回调函数的结果。 684 685**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 686 687**系统能力:** SystemCapability.Utils.Lang 688 689**参数:** 690 691| 参数名 | 类型 | 必填 | 说明 | 692| ---------- | ------------------------------------------------ | ---- | ------------------------------ | 693| callbackFn | (value: T, index: number, array: Array\<T>) => U | 是 | 用于对每个元素执行的回调函数。 | 694 695**返回值:** 696 697| 类型 | 说明 | 698| --------- | -------------------------- | 699| Array\<U> | 包含回调函数结果的新Array。 | 700 701**错误码:** 702 703以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 704 705| 错误码ID | 错误信息 | 706| -------- | ------------------------------- | 707| 10200011 | The map method cannot be bound. | 708| 10200201 | Concurrent modification error. | 709 710**示例:** 711 712```ts 713// 此处将原始Array中的每个字符串元素转换为大写形式,并返回一个新Array,其中包含转换后的字符串 714let array = new collections.Array<string>('a', 'b', 'c'); 715let mappedArray = array.map((value, index, array) => { 716 return value.toUpperCase(); // 将每个字符串元素转换为大写 717}); 718console.info("" + mappedArray); // 输出: ['A', 'B', 'C'] 719``` 720 721### filter 722 723filter(predicate: (value: T, index: number, array: Array\<T>) => boolean): Array\<T> 724 725返回一个新Array,其中包含通过指定回调函数测试的所有元素。 726 727**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 728 729**系统能力:** SystemCapability.Utils.Lang 730 731**参数:** 732 733| 参数名 | 类型 | 必填 | 说明 | 734| --------- | ------------------------------------------------------ | ---- | ------------------------------------------------------------ | 735| predicate | (value: T, index: number, array: Array\<T>) => boolean | 是 | 一个接受三个参数的函数,用于测试每个元素是否应该包含在新Array中。 | 736 737**返回值:** 738 739| 类型 | 说明 | 740| --------- | ---------------------------- | 741| Array\<T> | 包含通过测试的元素的新Array。 | 742 743**错误码:** 744 745以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 746 747| 错误码ID | 错误信息 | 748| -------- | ---------------------------------- | 749| 10200011 | The filter method cannot be bound. | 750| 10200201 | Concurrent modification error. | 751 752**示例:** 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],只包含偶数 757``` 758 759### reduce 760 761reduce(callbackFn: (previousValue: T, currentValue: T, currentIndex: number, array: Array\<T>) => T): T 762 763对Array中的每个元素执行回调函数,将其结果作为累加值,并返回最终的结果。 764 765**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 766 767**系统能力:** SystemCapability.Utils.Lang 768 769**参数:** 770 771| 参数名 | 类型 | 必填 | 说明 | 772| ---------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 773| callbackFn | (previousValue: T, currentValue: T, currentIndex: number, array: Array\<T>) => T | 是 | 一个接受四个参数的函数,用于对每个元素执行操作,并将结果作为累加值传递给下一个元素。 | 774 775**返回值:** 776 777| 类型 | 说明 | 778| ---- | -------------------------- | 779| T | 回调函数执行后的最终结果。 | 780 781**错误码:** 782 783以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 784 785| 错误码ID | 错误信息 | 786| -------- | ---------------------------------- | 787| 10200011 | The reduce method cannot be bound. | 788| 10200201 | Concurrent modification error. | 789 790**示例:** 791 792```ts 793let array = new collections.Array<number>(1, 2, 3, 4, 5); 794let reducedValue = array.reduce((accumulator, value) => accumulator + value); // 返回15,累加所有元素 795``` 796 797### reduce 798 799reduce\<U>(callbackFn: (previousValue: U, currentValue: T, currentIndex: number, array: Array\<T>) => U, initialValue: U): U 800 801与 reduce方法类似,但它接受一个初始值作为第二个参数,用于在Array遍历开始前初始化累加器。 802 803**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 804 805**系统能力:** SystemCapability.Utils.Lang 806 807**参数:** 808 809| 参数名 | 类型 | 必填 | 说明 | 810| ------------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 811| callbackFn | callbackFn: (previousValue: U, currentValue: T, currentIndex: number, array: Array\<T>) => U | 是 | 一个接受四个参数的函数,用于对每个元素执行操作,并将结果作为累加值传递给下一个元素。 | 812| initialValue | U | 是 | 用于初始化累加器的值。 | 813 814**返回值:** 815 816| 类型 | 说明 | 817| ---- | -------------------------- | 818| U | 回调函数执行后的最终结果。 | 819 820**错误码:** 821 822以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 823 824| 错误码ID | 错误信息 | 825| -------- | ---------------------------------- | 826| 10200011 | The reduce method cannot be bound. | 827| 10200201 | Concurrent modification error. | 828 829**示例:** 830 831```ts 832// 此处使用一个初始值为0的累加器,并将其与Array中的每个元素相加,最终返回累加后的总和 833let array = new collections.Array(1, 2, 3, 4, 5); 834let reducedValue = array.reduce<number>((accumulator: number, value: number) => accumulator + value, 0); // 返回15,累加所有元素,初始值为0 835``` 836 837### at 838 839at(index: number): T | undefined 840 841返回Array中指定索引位置的元素。 842 843**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 844 845**系统能力:** SystemCapability.Utils.Lang 846 847**参数:** 848 849| 参数名 | 类型 | 必填 | 说明 | 850| ------ | ------ | ---- | ------------------------------------------------------------ | 851| index | number | 是 | 要返回的Array元素的索引(从零开始),取值为整数。负数索引从Array末尾开始计数,如果`index < 0`,则会访问`index + array.length`位置的元素。 | 852 853 854**返回值:** 855 856| 类型 | 说明 | 857| -------------- | ------------------------------------------------------------ | 858| T \| undefined | 返回指定索引处的元素;如果索引超出范围或无效,则返回undefined。 | 859 860**错误码:** 861 862以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 863 864| 错误码ID | 错误信息 | 865| -------- | ------------------------------ | 866| 10200011 | The at method cannot be bound. | 867| 10200201 | Concurrent modification error. | 868 869**示例:** 870 871```ts 872let array = new collections.Array<number>(1, 2, 3, 4, 5); 873let elementAtIndex = array.at(2); // 返回3,因为索引是从0开始的 874``` 875 876### entries 877 878entries(): IterableIterator<[number, T]> 879 880返回一个新的可迭代对象,该对象包含Array中每个元素的键值对。 881 882**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 883 884**系统能力:** SystemCapability.Utils.Lang 885 886**返回值:** 887 888| 类型 | 说明 | 889| ----------------------------- | ------------------------------------------ | 890| IterableIterator<[number, T]> | 包含Array中每个元素的键值对的迭代器。 | 891 892**错误码:** 893 894以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 895 896| 错误码ID | 错误信息 | 897| -------- | ----------------------------------- | 898| 10200011 | The entries method cannot be bound. | 899| 10200201 | Concurrent modification error. | 900 901**示例:** 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],第一个元素的键值对 907``` 908 909### keys 910 911keys(): IterableIterator\<number> 912 913返回一个新的可迭代对象,该对象包含Array中每个元素的键。 914 915**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 916 917**系统能力:** SystemCapability.Utils.Lang 918 919**返回值:** 920 921| 类型 | 说明 | 922| ------------------------- | -------------------------------------- | 923| IterableIterator\<number> | 包含Array中每个元素的键的可迭代迭代器。 | 924 925**错误码:** 926 927以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 928 929| 错误码ID | 错误信息 | 930| -------- | -------------------------------- | 931| 10200011 | The keys method cannot be bound. | 932| 10200201 | Concurrent modification error. | 933 934**示例:** 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,4 941} 942``` 943 944### values 945 946values(): IterableIterator\<T> 947 948返回一个新的可迭代对象,该对象包含Array中每个元素的值。 949 950**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 951 952**系统能力:** SystemCapability.Utils.Lang 953 954**返回值:** 955 956| 类型 | 说明 | 957| -------------------- | -------------------------------------- | 958| IterableIterator\<T> | 包含Array中每个元素的值的可迭代迭代器。 | 959 960**错误码:** 961 962以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 963 964| 错误码ID | 错误信息 | 965| -------- | ---------------------------------- | 966| 10200011 | The values method cannot be bound. | 967| 10200201 | Concurrent modification error. | 968 969**示例:** 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,5 976} 977``` 978 979### find 980 981find(predicate: (value: T, index: number, obj: Array\<T>) => boolean): T | undefined 982 983返回Array中第一个满足指定测试函数的元素的值,如果所有元素都不满足,则返回undefined。 984 985**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 986 987**系统能力:** SystemCapability.Utils.Lang 988 989**参数:** 990 991| 参数名 | 类型 | 必填 | 说明 | 992| --------- | ---------------------------------------------------- | ---- | ------------------------------------------------------ | 993| predicate | (value: T, index: number, obj: Array\<T>) => boolean | 是 | 一个接受三个参数的函数,用于测试每个元素是否满足条件。 | 994 995**返回值:** 996 997| 类型 | 说明 | 998| -------------- | ------------------------------------------------------------ | 999| T \| undefined | 第一个满足条件的元素的值;如果所有元素都不满足条件,则返回undefined。 | 1000 1001**错误码:** 1002 1003以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1004 1005| 错误码ID | 错误信息 | 1006| -------- | -------------------------------- | 1007| 10200011 | The find method cannot be bound. | 1008| 10200201 | Concurrent modification error. | 1009 1010**示例:** 1011 1012```ts 1013let array = new collections.Array<number>(1, 2, 3, 4, 5); 1014let foundValue = array.find((value: number) => value % 2 === 0); // 返回2,第一个偶数元素 1015``` 1016 1017### includes 1018 1019includes(searchElement: T, fromIndex?: number): boolean 1020 1021判断Array是否包含指定的元素,并返回一个布尔值。 1022 1023**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1024 1025**系统能力:** SystemCapability.Utils.Lang 1026 1027**参数:** 1028 1029| 参数名 | 类型 | 必填 | 说明 | 1030| ------------- | ------ | ---- | --------------------------- | 1031| searchElement | T | 是 | 要搜索的元素。 | 1032| fromIndex | number | 否 | 开始搜索的索引。默认值为0。 | 1033 1034**返回值:** 1035 1036| 类型 | 说明 | 1037| ------- | --------------------------------------------------- | 1038| boolean | 如果Array包含指定的元素,则返回true;否则返回false。 | 1039 1040**错误码:** 1041 1042以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1043 1044| 错误码ID | 错误信息 | 1045| -------- | ------------------------------------ | 1046| 10200011 | The includes method cannot be bound. | 1047| 10200201 | Concurrent modification error. | 1048 1049**示例:** 1050 1051```ts 1052let array = new collections.Array<number>(1, 2, 3, 4, 5); 1053let includesResult = array.includes(3); // 返回true,因为Array中包含3 1054``` 1055 1056### findIndex 1057 1058findIndex(predicate: (value: T, index: number, obj: Array\<T>) => boolean): number 1059 1060返回Array中第一个满足指定测试函数的元素的索引,如果所有元素都不满足,则返回-1。 1061 1062**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1063 1064**系统能力:** SystemCapability.Utils.Lang 1065 1066**参数:** 1067 1068| 参数名 | 类型 | 必填 | 说明 | 1069| --------- | ---------------------------------------------------- | ---- | ------------------------------------------------------ | 1070| predicate | (value: T, index: number, obj: Array\<T>) => boolean | 是 | 一个接受三个参数的函数,用于测试每个元素是否满足条件。 | 1071 1072**返回值:** 1073 1074| 类型 | 说明 | 1075| ------ | ------------------------------------------------------------ | 1076| number | 第一个满足条件的元素的索引;如果所有元素都不满足条件,则返回-1。 | 1077 1078**错误码:** 1079 1080以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1081 1082| 错误码ID | 错误信息 | 1083| -------- | ------------------------------------- | 1084| 10200011 | The findIndex method cannot be bound. | 1085| 10200201 | Concurrent modification error. | 1086 1087**示例:** 1088 1089```ts 1090let array = new collections.Array<number>(1, 2, 3, 4, 5); 1091let foundIndex = array.findIndex((value: number) => value % 2 === 0); // 返回1,因为2是第一个偶数元素 1092``` 1093 1094### fill 1095 1096fill(value: T, start?: number, end?: number): Array\<T> 1097 1098使用指定的值填充Array中指定范围的所有元素。 1099 1100**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1101 1102**系统能力:** SystemCapability.Utils.Lang 1103 1104**参数:** 1105 1106| 参数名 | 类型 | 必填 | 说明 | 1107| ------ | ------ | ---- | ------------------------------------------------------ | 1108| value | T | 是 | 要填充的值。 | 1109| start | number | 否 | 开始填充的索引。默认值为0。 | 1110| end | number | 否 | 结束填充的索引(不包括该元素)。如果省略,则填充到Array的最后一个元素。 | 1111 1112**返回值:** 1113 1114| 类型 | 说明 | 1115| --------- | -------------- | 1116| Array\<T> | 填充后的Array。 | 1117 1118**错误码:** 1119 1120以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1121 1122| 错误码ID | 错误信息 | 1123| -------- | -------------------------------- | 1124| 10200011 | The fill method cannot be bound. | 1125| 10200201 | Concurrent modification error. | 1126 1127**示例:** 1128 1129```ts 1130let array = new collections.Array(1, 2, 3, 4, 5); 1131array.fill(0, 1, 3); // 返回[1, 0, 0, 4, 5],因为1到3的索引范围内的元素被替换为0 1132``` 1133 1134### shrinkTo 1135 1136shrinkTo(arrayLength: number): void 1137 1138使Array收缩到指定长度。 1139 1140**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1141 1142**系统能力:** SystemCapability.Utils.Lang 1143 1144**参数:** 1145 1146| 参数名 | 类型 | 必填 | 说明 | 1147| ------ | ------ | ---- | ------------------------------------------------------ | 1148| arrayLength | number | 是 | Array的新长度。如果arrayLength >= array.length,则Array不变。 | 1149 1150**错误码:** 1151 1152以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1153 1154| 错误码ID | 错误信息 | 1155| -------- | -------------------------------- | 1156| 10200011 | The shrinkTo method cannot be bound. | 1157| 10200201 | Concurrent modification error. | 1158 1159**示例:** 1160 1161```ts 1162let array1 = new collections.Array(1, 2, 3, 4, 5); 1163array1.shrinkTo(1); // array内容变为:[1] 1164 1165let array2 = new collections.Array(1, 2, 3, 4, 5); 1166array2.shrinkTo(10); // array内容不变 1167``` 1168 1169### extendTo 1170 1171extendTo(arrayLength: number, initialValue: T): void 1172 1173使Array扩展到指定长度,扩展的部分使用给定值填充。 1174 1175**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1176 1177**系统能力:** SystemCapability.Utils.Lang 1178 1179**参数:** 1180 1181| 参数名 | 类型 | 必填 | 说明 | 1182| ------ | ------ | ---- | ------------------------------------------------------ | 1183| arrayLength | number | 是 | Array的新长度。如果arrayLength <= array.length,则Array不变。 | 1184| initialValue | T | 是 | 扩展的部分的填充值。 | 1185 1186**错误码:** 1187 1188以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1189 1190| 错误码ID | 错误信息 | 1191| -------- | -------------------------------- | 1192| 10200011 | The extendTo method cannot be bound. | 1193| 10200201 | Concurrent modification error. | 1194 1195**示例:** 1196 1197```ts 1198let array1 = new collections.Array(1, 2, 3); 1199array1.extendTo(5, 10); // array内容变为:[1, 2, 3, 10, 10] 1200 1201let array2 = new collections.Array(1, 2, 3); 1202array2.extendTo(1, 10); // array内容不变 1203``` 1204 1205### concat 1206 1207concat(...items: ConcatArray\<T>[]): Array\<T> 1208 1209拼接两个或多个数组。 1210 1211**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1212 1213**系统能力:** SystemCapability.Utils.Lang 1214 1215**参数:** 1216 1217| 参数名 | 类型 | 必填 | 说明 | 1218| ------ | ---- | ---- | ---------------------------------- | 1219| items | ConcatArray\<T>[] | 是 | 拼接两个或多个数组。 | 1220 1221**返回值:** 1222 1223| 类型 | 说明 | 1224| ---- | ---------------------------------- | 1225| Array\<T> | 拼接后的数组。 | 1226 1227**错误码:** 1228 1229以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1230 1231| 错误码ID | 错误信息 | 1232| ------- | -------- | 1233| 401 | Parameter error. Not a valid array. | 1234| 10200011 | The concat method cannot be bound. | 1235| 10200201 | Concurrent modification error. | 1236 1237**示例:** 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); // concatArray的内容为:[1, 2, 3, 4, 5, 6, 7, 8, 9] 1245``` 1246 1247### splice 1248 1249splice(start: number): Array\<T> 1250 1251删除Array中指定位置的元素。 1252 1253**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1254 1255**系统能力:** SystemCapability.Utils.Lang 1256 1257**参数:** 1258 1259| 参数名 | 类型 | 必填 | 说明 | 1260| ----- | ------ | -- | ------------------------------------------------------------------- | 1261| start | number | 是 | 开始索引。如果`-array.length =< start < 0`,从`start + array.length`开始,如果`start < -array.length`,则从0开始。 | 1262 1263**返回值:** 1264 1265| 类型 | 说明 | 1266| --------- | --------------------- | 1267| Array\<T> | 返回一个新的包含被删除元素的Array对象。如果没有元素被删除,返回一个空的Array对象。 | 1268 1269**错误码:** 1270 1271以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1272 1273| 错误码ID | 错误信息 | 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**示例:** 1280 1281```ts 1282let array = new collections.Array<number>(1, 2, 3, 4, 5); 1283let removeArray = array.splice(2); // array内容变为[1, 2],返回[3, 4, 5] 1284``` 1285 1286### splice 1287 1288splice(start: number, deleteCount: number, ...items: T[]): Array\<T> 1289 1290删除Array中指定位置的元素,需要时在Array的指定位置插入新元素。 1291 1292**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1293 1294**系统能力:** SystemCapability.Utils.Lang 1295 1296**参数:** 1297 1298| 参数名 | 类型 | 必填 | 说明 | 1299| ----------- | ------ | -- | ------------------------------------------------------------------- | 1300| start | number | 是 | 开始索引。如果`-array.length =< start < 0`,从`start + array.length`开始,如果`start < -array.length`,则从0开始。 | 1301| deleteCount | number | 是 | 删除元素的个数。 | 1302| items | T[] | 否 | 从`start`位置开始插入的新元素。如果省略,仅删除Array内的指定元素。 | 1303 1304**返回值:** 1305 1306| 类型 | 说明 | 1307| --------- | ------------------------------------ | 1308| Array\<T> | 返回一个新的包含被删除元素的Array对象。如果没有元素被删除,返回一个空的Array对象。 | 1309 1310**错误码:** 1311 1312以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1313 1314| 错误码ID | 错误信息 | 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**示例:** 1321 1322```ts 1323// 例1: 1324let array = new collections.Array<number>(1, 2, 3, 4, 5); 1325let removeArray = array.splice(2, 2); // array内容变为[1, 2, 5],返回[3, 4] 1326``` 1327 1328```ts 1329// 例2: 1330let array = new collections.Array<number>(1, 2, 3, 4, 5); 1331let removeArray = array.splice(2, 2, 6, 7, 8); // array内容变为[1, 2, 6, 7, 8, 5],返回[3, 4] 1332``` 1333 1334### [Symbol.iterator] 1335 1336[Symbol.iterator]\(): IterableIterator<T> 1337 1338返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 1339 1340> **说明:** 1341> 1342> 本接口不支持在.ets文件中使用。 1343 1344**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1345 1346**系统能力:** SystemCapability.Utils.Lang 1347 1348**返回值:** 1349 1350| 类型 | 说明 | 1351| ------------------------- | ---------------- | 1352| IterableIterator<T> | 返回一个迭代器。 | 1353 1354**错误码:** 1355 1356以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1357 1358| 错误码ID | 错误信息 | 1359| -------- | ------------------------------------------- | 1360| 10200011 | The Symbol.iterator method cannot be bound. | 1361 1362**示例:** 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 1376返回Array指定索引位置的元素。 1377 1378**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1379 1380**系统能力:** SystemCapability.Utils.Lang 1381 1382| 参数名 | 类型 | 必填 | 说明 | 1383| ----- | ------ | ---- | ------------------------------------------------------------------ | 1384| index | number | 是 | 所需代码单元的从零开始的索引。当index<0 或者index>=length,则会抛出错误。 | 1385 1386**返回值:** 1387 1388| 类型 | 说明 | 1389| ----- | ------------------------ | 1390| T | Array给定的元素数据类型。 | 1391 1392**错误码**: 1393 1394以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1395 1396| 错误码ID | 错误信息 | 1397| ------- | ------------------------------------ | 1398| 401 | Parameter error. | 1399| 10200001 | The value of index is out of range. | 1400 1401**示例:** 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 1410一种非线性数据结构。 1411 1412文档中存在泛型的使用,涉及以下泛型标记符: 1413 1414- K:Key,键 1415- V:Value,值 1416 1417K和V类型都需为[Sendable支持的数据类型](../../arkts-utils/arkts-sendable.md#sendable支持的数据类型)。 1418 1419### 属性 1420 1421**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1422 1423**系统能力:** SystemCapability.Utils.Lang 1424 1425| 名称 | 类型 | 只读 | 可选 | 说明 | 1426| ---- | ------ | ---- | ---- | --------------- | 1427| size | number | 是 | 否 | Map的元素个数。 | 1428 1429 1430### constructor 1431constructor(entries?: readonly (readonly [K, V])[] | null) 1432 1433构造函数,用于创建ArkTS Map对象。 1434 1435**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1436 1437**系统能力:** SystemCapability.Utils.Lang 1438 1439**参数:** 1440 1441| 参数名 | 类型 | 必填 | 说明 | 1442| ------- | ------ | ---- | ------------------------------------------------------------ | 1443| entries | [K, V][] \| null | 否 | 键值对数组或其它可迭代对象。默认值为null,创建一个空Map对象。 | 1444 1445**错误码:** 1446 1447以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1448 1449| 错误码ID | 错误信息 | 1450| -------- | ------------------------------------------------------- | 1451| 10200012 | The ArkTS Map's constructor cannot be directly invoked. | 1452 1453**示例:** 1454 1455```ts 1456// 正例1: 1457const myMap = new collections.Map<number, number>(); 1458``` 1459 1460```ts 1461// 正例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// 反例: 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 1487返回一个Map迭代器对象,该对象包含了此Map中的每个元素的[key, value]对。 1488 1489**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1490 1491**系统能力:** SystemCapability.Utils.Lang 1492 1493**返回值:** 1494 1495| 类型 | 说明 | 1496| ------------------------------ | ----------------------- | 1497| IterableIterator<[K, V]> | 返回一个Map迭代器对象。 | 1498 1499**错误码:** 1500 1501以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1502 1503| 错误码ID | 错误信息 | 1504| -------- | ----------------------------------------------------- | 1505| 10200011 | The entries method cannot be bound with non-sendable. | 1506 1507**示例:** 1508 1509```ts 1510// 例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// 例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 1544返回一个Map迭代器对象,该对象包含了此Map中每个元素的键。 1545 1546**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1547 1548**系统能力:** SystemCapability.Utils.Lang 1549 1550**返回值:** 1551 1552| 类型 | 说明 | 1553| ------------------------- | ----------------------- | 1554| IterableIterator<K> | 返回一个Map迭代器对象。 | 1555 1556**错误码:** 1557 1558以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1559 1560| 错误码ID | 错误信息 | 1561| -------- | -------------------------------------------------- | 1562| 10200011 | The keys method cannot be bound with non-sendable. | 1563 1564**示例:** 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 1582返回一个Map迭代器对象,该对象包含此Map中每个元素的值。 1583 1584**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1585 1586**系统能力:** SystemCapability.Utils.Lang 1587 1588**返回值:** 1589 1590| 类型 | 说明 | 1591| ------------------------- | ----------------------- | 1592| IterableIterator<V> | 返回一个Map迭代器对象。 | 1593 1594**错误码:** 1595 1596以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1597 1598| 错误码ID | 错误信息 | 1599| -------- | ---------------------------------------------------- | 1600| 10200011 | The values method cannot be bound with non-sendable. | 1601 1602**示例:** 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 1620删除该Map中的所有元素。 1621 1622**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1623 1624**系统能力:** SystemCapability.Utils.Lang 1625 1626**错误码:** 1627 1628以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1629 1630| 错误码ID | 错误信息 | 1631| -------- | --------------------------------------------------- | 1632| 10200011 | The clear method cannot be bound with non-sendable. | 1633| 10200201 | Concurrent modification exception. | 1634 1635**示例:** 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 1652删除该Map中指定元素。 1653 1654**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1655 1656**系统能力:** SystemCapability.Utils.Lang 1657 1658**参数:** 1659 1660| 参数名 | 类型 | 必填 | 说明 | 1661| ------ | ---- | ---- | ---------------- | 1662| key | K | 是 | 待删除元素的键。 | 1663 1664**返回值:** 1665 1666| 类型 | 说明 | 1667| ------- | ------------------------------------------------------------ | 1668| boolean | 如果元素存在并已被删除,则为true;否则该元素不存在,返回false。 | 1669 1670**错误码:** 1671 1672以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1673 1674| 错误码ID | 错误信息 | 1675| -------- | ---------------------------------------------------- | 1676| 10200011 | The delete method cannot be bound with non-sendable. | 1677| 10200201 | Concurrent modification exception. | 1678 1679 1680**示例:** 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 1697按插入顺序对该Map中的每个键/值对执行一次回调函数。 1698 1699**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1700 1701**系统能力:** SystemCapability.Utils.Lang 1702 1703**参数:** 1704 1705| 参数名 | 类型 | 必填 | 说明 | 1706| ---------- | ------------------------------------------ | ---- | ---------- | 1707| callbackFn | (value: V, key: K, map: Map<K, V>) => void | 是 | 回调函数。 | 1708 1709callbackFn的参数说明: 1710| 参数名 | 类型 | 必填 | 说明 | 1711| ------ | --------------- | ---- | ---------------------------- | 1712| value | V | 否 | 当前遍历到的元素键值对的值。 | 1713| key | K | 否 | 当前遍历到的元素键值对的键。 | 1714| map | Map<K, V> | 否 | 当前map实例对象。 | 1715 1716**错误码:** 1717 1718以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1719 1720| 错误码ID | 错误信息 | 1721| -------- | ----------------------------------------------------- | 1722| 10200011 | The forEach method cannot be bound with non-sendable. | 1723| 10200201 | Concurrent modification exception. | 1724 1725**示例:** 1726 1727```ts 1728// 正例: 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// 反例: 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 1754返回该Map中的指定元素。 1755 1756**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1757 1758**系统能力:** SystemCapability.Utils.Lang 1759 1760**参数:** 1761 1762| 参数名 | 类型 | 必填 | 说明 | 1763| ------ | ---- | ---- | --------- | 1764| key | K | 是 | 指定key。 | 1765 1766**返回值:** 1767 1768| 类型 | 说明 | 1769| ---- | ------------------------------------------------------------ | 1770| V | 与指定键相关联的元素,如果键在Map对象中找不到,则返回undefined。 | 1771 1772**错误码:** 1773 1774以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1775 1776| 错误码ID | 错误信息 | 1777| -------- | ------------------------------------------------- | 1778| 10200011 | The get method cannot be bound with non-sendable. | 1779| 10200201 | Concurrent modification exception. | 1780 1781**示例:** 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 1796判断该Map中是否存在指定元素。 1797 1798**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1799 1800**系统能力:** SystemCapability.Utils.Lang 1801 1802**返回值:** 1803 1804| 类型 | 说明 | 1805| ------- | --------------------------------------------- | 1806| boolean | 如果存在指定元素,则返回true,否则返回false。 | 1807 1808**错误码:** 1809 1810以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1811 1812| 错误码ID | 错误信息 | 1813| -------- | ------------------------------------------------- | 1814| 10200011 | The has method cannot be bound with non-sendable. | 1815| 10200201 | Concurrent modification exception. | 1816 1817**示例:** 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 1832向该Map添加或更新一个指定的键值对。 1833 1834**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1835 1836**系统能力:** SystemCapability.Utils.Lang 1837 1838**返回值:** 1839 1840| 类型 | 说明 | 1841| --------------- | ------- | 1842| Map<K, V> | Map对象 | 1843 1844**错误码:** 1845 1846以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1847 1848| 错误码ID | 错误信息 | 1849| -------- | ------------------------------------------------- | 1850| 10200011 | The set method cannot be bound with non-sendable. | 1851| 10200201 | Concurrent modification exception. | 1852 1853**示例:** 1854 1855```ts 1856// 正例: 1857const myMap = new collections.Map<string, string>(); 1858myMap.set("foo", "bar") 1859``` 1860 1861<!--code_no_check--> 1862```ts 1863// 反例: 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 1874返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 1875 1876> **说明:** 1877> 1878> 本接口不支持在.ets文件中使用。 1879 1880**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1881 1882**系统能力:** SystemCapability.Utils.Lang 1883 1884**返回值:** 1885| 类型 | 说明 | 1886| -------- | -------- | 1887| IterableIterator<[K, V]> | 返回一个迭代器。 | 1888 1889**错误码:** 1890 1891以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1892 1893| 错误码ID | 错误信息 | 1894| -------- | -------- | 1895| 10200011 | The Symbol.iterator method cannot be bound. | 1896 1897**示例:** 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 1916一种非线性数据结构。 1917 1918文档中存在泛型的使用,涉及以下泛型标记符: 1919 1920- T:Type,支持[Sendable支持的数据类型](../../arkts-utils/arkts-sendable.md#sendable支持的数据类型)。 1921 1922### 属性 1923 1924**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1925 1926**系统能力:** SystemCapability.Utils.Lang 1927 1928| 名称 | 类型 | 只读 | 可选 | 说明 | 1929| ---- | ------ | ---- | ---- | --------------- | 1930| size | number | 是 | 否 | Set的元素个数。 | 1931 1932### constructor 1933 1934constructor(values?: readonly T[] | null) 1935 1936构造函数,用于创建ArkTS Set对象。 1937 1938**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1939 1940**系统能力:** SystemCapability.Utils.Lang 1941 1942**参数:** 1943 1944| 参数名 | 类型 | 必填 | 说明 | 1945| ------ | ---- | ---- | --------------------------------------------------------- | 1946| values | T[] \| null | 否 | 数组或其它可迭代对象。默认值为null,创建一个空Set对象。 | 1947 1948**错误码:** 1949 1950以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1951 1952| 错误码ID | 错误信息 | 1953| -------- | ------------------------------------------------------- | 1954| 10200012 | The ArkTS Set's constructor cannot be directly invoked. | 1955 1956**示例:** 1957 1958```ts 1959// 正例1: 1960const mySet = new collections.Set<number>(); 1961``` 1962 1963```ts 1964// 正例2: 1965const mySet = new collections.Set<number>([1, 2, 3, 4, 5]); 1966``` 1967 1968<!--code_no_check--> 1969```ts 1970// 反例: 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 1987返回一个Set迭代器对象。 1988 1989**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 1990 1991**系统能力:** SystemCapability.Utils.Lang 1992 1993**返回值:** 1994 1995| 类型 | 说明 | 1996| ------------------------------ | ----------------------- | 1997| IterableIterator<[T, T]> | 返回一个Set迭代器对象。 | 1998 1999**错误码:** 2000 2001以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2002 2003| 错误码ID | 错误信息 | 2004| -------- | ----------------------------------------------------- | 2005| 10200011 | The entries method cannot be bound with non-sendable. | 2006 2007**示例:** 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 2022返回一个Set迭代器对象,该对象包含了此Set中每个元素的值。 2023 2024**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2025 2026**系统能力:** SystemCapability.Utils.Lang 2027 2028**返回值:** 2029 2030| 类型 | 说明 | 2031| ------------------------- | ----------------------- | 2032| IterableIterator<T> | 返回一个Set迭代器对象。 | 2033 2034**错误码:** 2035 2036以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2037 2038| 错误码ID | 错误信息 | 2039| -------- | -------------------------------------------------- | 2040| 10200011 | The keys method cannot be bound with non-sendable. | 2041 2042**示例:** 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 2057返回一个Set迭代器对象,该对象包含了此Set中每个元素的值。 2058 2059**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2060 2061**系统能力:** SystemCapability.Utils.Lang 2062 2063**返回值:** 2064 2065| 类型 | 说明 | 2066| ------------------------- | ----------------------- | 2067| IterableIterator<T> | 返回一个Set迭代器对象。 | 2068 2069**错误码:** 2070 2071以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2072 2073| 错误码ID | 错误信息 | 2074| -------- | ---------------------------------------------------- | 2075| 10200011 | The values method cannot be bound with non-sendable. | 2076 2077**示例:** 2078 2079```ts 2080// 例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// 例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 2108删除该Set中的所有元素。 2109 2110**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2111 2112**系统能力:** SystemCapability.Utils.Lang 2113 2114**错误码:** 2115 2116以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2117 2118| 错误码ID | 错误信息 | 2119| -------- | --------------------------------------------------- | 2120| 10200011 | The clear method cannot be bound with non-sendable. | 2121| 10200201 | Concurrent modification exception. | 2122 2123**示例:** 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 2137删除该Set中指定元素。 2138 2139**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2140 2141**系统能力:** SystemCapability.Utils.Lang 2142 2143**参数:** 2144 2145| 参数名 | 类型 | 必填 | 说明 | 2146| ------ | ---- | ---- | ---------------- | 2147| key | K | 是 | 待删除元素的键。 | 2148 2149**返回值:** 2150 2151| 类型 | 说明 | 2152| ------- | --------------------------------- | 2153| boolean | 成功删除返回true,否则返回false。 | 2154 2155**错误码:** 2156 2157以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2158 2159| 错误码ID | 错误信息 | 2160| -------- | ---------------------------------------------------- | 2161| 10200011 | The delete method cannot be bound with non-sendable. | 2162| 10200201 | Concurrent modification exception. | 2163 2164 2165**示例:** 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 2180按插入顺序对该Set中的每个键/值对执行一次回调函数。 2181 2182**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2183 2184**系统能力:** SystemCapability.Utils.Lang 2185 2186**参数:** 2187 2188| 参数名 | 类型 | 必填 | 说明 | 2189| ---------- | -------------------------------------------- | ---- | ---------- | 2190| callbackFn | (value1: T, value2: T, set: Set\<T>) => void | 是 | 回调函数。 | 2191 2192callbackFn的参数说明: 2193| 参数名 | 类型 | 必填 | 说明 | 2194| ------ | ------------ | ---- | ---------------------------- | 2195| value1 | T | 否 | 当前遍历到的元素键值对的值。 | 2196| value2 | T | 否 | 当前遍历到的元素键值对的键。 | 2197| set | Set<T> | 否 | 当前set实例对象。 | 2198 2199**错误码:** 2200 2201以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2202 2203| 错误码ID | 错误信息 | 2204| -------- | ----------------------------------------------------- | 2205| 10200011 | The forEach method cannot be bound with non-sendable. | 2206| 10200201 | Concurrent modification exception. | 2207 2208**示例:** 2209 2210```ts 2211// 正例: 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// 反例: 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 2229判断该Set中是否存在指定元素。 2230 2231**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2232 2233**系统能力:** SystemCapability.Utils.Lang 2234 2235**返回值:** 2236 2237| 类型 | 说明 | 2238| ------- | --------------------------------------------- | 2239| boolean | 如果存在指定元素,则返回true;否则返回false。 | 2240 2241**错误码:** 2242 2243以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2244 2245| 错误码ID | 错误信息 | 2246| -------- | ------------------------------------------------- | 2247| 10200011 | The has method cannot be bound with non-sendable. | 2248| 10200201 | Concurrent modification exception. | 2249 2250**示例:** 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 2263如果没有相同元素,则在该Set中插入一个新元素。 2264 2265**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 2266 2267**系统能力:** SystemCapability.Utils.Lang 2268 2269**返回值:** 2270 2271| 类型 | 说明 | 2272| ------------ | --------- | 2273| Set<T> | Set对象。 | 2274 2275**错误码:** 2276 2277以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2278 2279| 错误码ID | 错误信息 | 2280| -------- | ------------------------------------------------- | 2281| 10200011 | The add method cannot be bound with non-sendable. | 2282| 10200201 | Concurrent modification exception. | 2283 2284**示例:** 2285 2286```ts 2287// 正例: 2288const mySet: collections.Set<string> = new collections.Set<string>(); 2289mySet.add("foo"); 2290``` 2291 2292<!--code_no_check--> 2293```ts 2294// 反例: 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 2305返回一个迭代器,迭代器的每一项都是一个JavaScript对象,并返回该对象。 2306 2307> **说明:** 2308> 2309> 本接口不支持在.ets文件中使用。 2310 2311**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2312 2313**系统能力:** SystemCapability.Utils.Lang 2314 2315**返回值:** 2316 2317| 类型 | 说明 | 2318| -------- | -------- | 2319| IterableIterator<T> | 返回一个迭代器。 | 2320 2321**错误码:** 2322 2323以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2324 2325| 错误码ID | 错误信息 | 2326| -------- | -------- | 2327| 10200011 | The Symbol.iterator method cannot be bound. | 2328 2329**示例:** 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 2341ArkTS TypedArray的底层数据结构。 2342 2343### 属性 2344 2345**系统能力:** SystemCapability.Utils.Lang 2346 2347**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2348 2349| 名称 | 类型 | 只读 | 可选 | 说明 | 2350| ------ | ------ | ---- | ---- | ----------------| 2351| byteLength | number | 是 | 否 | buffer所占的字节数。| 2352 2353### constructor 2354constructor(byteLength: number) 2355 2356构造函数,用于创建一个指定长度的ArkTS ArrayBuffer对象。 2357 2358**系统能力:** SystemCapability.Utils.Lang 2359 2360**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2361 2362**参数:** 2363 2364| 参数名 | 类型 | 必填 | 说明 | 2365| ------ | ------ | ---- | -------------------------| 2366| byteLength | number | 是 | buffer所占的字节数。 | 2367 2368**错误码:** 2369 2370以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2371 2372| 错误码ID | 错误信息 | 2373| -------- | ------------------------------------------------------- | 2374| 10200012 | The ArrayBuffer's constructor cannot be directly invoked. | 2375 2376**示例:** 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 2386返回一个新的ArkTS ArrayBuffer对象,其包含原ArkTS ArrayBuffer指定范围的内容。 2387 2388**系统能力:** SystemCapability.Utils.Lang 2389 2390**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2391 2392**参数:** 2393 2394| 参数名 | 类型 | 必填 | 说明 | 2395| ------ | ------ | ---- | ------------------------------------------------ | 2396| begin | number | 是 | 开始索引,如果`begin < 0`,则会从`begin + arraybuffer.byteLength`位置开始。 | 2397| end | number | 否 | 结束索引(不包括该元素),如果`end < 0`,则会到`end + arraybuffer.byteLength`位置结束。默认为ArkTS ArrayBuffer的长度。| 2398 2399**返回值:** 2400 2401| 类型 | 说明 | 2402| ------------ | --------- | 2403| ArrayBuffer | 新的ArkTS ArrayBuffer对象。 | 2404 2405**错误码:** 2406 2407以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2408 2409| 错误码ID | 错误信息 | 2410| -------- | -------------------------------------------- | 2411| 10200011 | The slice method cannot be bound. | 2412| 10200201 | Concurrent modification error. | 2413 2414**示例:** 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 2425ArkTS TypedArray映射函数类型。 2426 2427**系统能力:** SystemCapability.Utils.Lang 2428 2429**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2430 2431**参数:** 2432 2433| 参数名 | 类型 | 必填 | 说明 | 2434| ------- | ------ | ---- | --------------------------- | 2435| value | FromElementType | 是 | 当前遍历的用于构造ArkTS TypedArray的元素。 | 2436| index | number | 是 | 当前遍历的用于构造ArkTS TypedArray的元素下标。 | 2437 2438**返回值:** 2439 2440| 类型 | 说明 | 2441| ------ | --------------------------- | 2442| ToElementType | 转换后的元素值。 | 2443 2444## TypedArrayPredicateFn 2445type TypedArrayPredicateFn\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => boolean 2446 2447ArkTS TypedArray断言测试函数类型。 2448 2449**系统能力:** SystemCapability.Utils.Lang 2450 2451**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2452 2453**参数:** 2454 2455| 参数名 | 类型 | 必填 | 说明 | 2456| ------- | ------ | ---- | --------------------------- | 2457| value | ElementType | 是 | 当前遍历的ArkTS TypedArray元素。 | 2458| index | number | 是 | 当前遍历的ArkTS TypedArray元素下标。 | 2459| array | ArrayType | 是 | 当前遍历的ArkTS TypedArray实例。 | 2460 2461**返回值:** 2462 2463| 类型 | 说明 | 2464| ------ | --------------------------- | 2465| boolean | 如果值符合条件,则为true,否则为false。 | 2466 2467## TypedArrayForEachCallback 2468type TypedArrayForEachCallback\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => void 2469 2470ArkTS TypedArray遍历函数类型。 2471 2472**系统能力:** SystemCapability.Utils.Lang 2473 2474**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2475 2476**参数:** 2477 2478| 参数名 | 类型 | 必填 | 说明 | 2479| ------- | ------ | ---- | --------------------------- | 2480| value | ElementType | 是 | 当前遍历的ArkTS TypedArray元素。 | 2481| index | number | 是 | 当前遍历的ArkTS TypedArray元素下标。 | 2482| array | ArrayType | 是 | 当前遍历的ArkTS TypedArray实例。 | 2483 2484## TypedArrayMapCallback 2485type TypedArrayMapCallback\<ElementType, ArrayType> = (value: ElementType, index: number, array: ArrayType) => ElementType 2486 2487ArkTS TypedArray转换映射函数类型。 2488 2489**系统能力:** SystemCapability.Utils.Lang 2490 2491**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2492 2493**参数:** 2494 2495| 参数名 | 类型 | 必填 | 说明 | 2496| ------- | ------ | ---- | --------------------------- | 2497| value | ElementType | 是 | 当前映射的ArkTS TypedArray元素。 | 2498| index | number | 是 | 当前映射的ArkTS TypedArray元素下标。 | 2499| array | ArrayType | 是 | 当前映射的ArkTS TypedArray实例。 | 2500 2501**返回值:** 2502 2503| 类型 | 说明 | 2504| ------ | --------------------------- | 2505| ElementType | 转换后的元素值。 | 2506 2507## TypedArrayReduceCallback 2508type TypedArrayReduceCallback\<AccType, ElementType, ArrayType> = (previousValue: AccType, currentValue: ElementType, currentIndex: number, array: ArrayType) => AccType 2509 2510ArkTS TypedArray归约函数类型。 2511 2512**系统能力:** SystemCapability.Utils.Lang 2513 2514**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2515 2516**参数:** 2517 2518| 参数名 | 类型 | 必填 | 说明 | 2519| ------- | ------ | ---- | --------------------------- | 2520| previousValue | AccType | 是 | 当前遍历所累积的值。| 2521| currentValue | ElementType | 是 | 当前遍历的ArkTS TypedArray元素。 | 2522| currentIndex | number | 是 | 当前遍历的ArkTS TypedArray元素下标。 | 2523| array | ArrayType | 是 | 当前遍历的ArkTS TypedArray实例。 | 2524 2525**返回值:** 2526 2527| 类型 | 说明 | 2528| ------ | --------------------------- | 2529| AccType | 归约函数的结果。该结果会作为下一次调用TypedArrayReduceCallback时的previousValue参数。 | 2530 2531## TypedArrayCompareFn 2532type TypedArrayCompareFn\<ElementType> = (first: ElementType, second: ElementType) => number 2533 2534ArkTS TypedArray排序函数类型。 2535 2536**系统能力:** SystemCapability.Utils.Lang 2537 2538**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2539 2540**参数:** 2541 2542| 参数名 | 类型 | 必填 | 说明 | 2543| ------- | ------ | ---- | --------------------------- | 2544| first | ElementType | 是 | 当前待比较的第一个元素。 | 2545| second | ElementType | 是 | 当前待比较的第二个元素。 | 2546 2547**返回值:** 2548 2549| 类型 | 说明 | 2550| ------ | --------------------------- | 2551| number | 元素比较的结果。如果`first`小于`second`,返回值为负数;如果`first`大于`second`,返回值为正数;如果两个值相等,返回值为0。 | 2552 2553## collections.TypedArray 2554 2555一种线性数据结构,底层基于[ArkTS ArrayBuffer](#collectionsarraybuffer)实现。目前支持包括Int8Array、Uint8Array、Int16Array、Uint16Array、Int32Array、Uint32Array、Uint8ClampedArray以及Float32Array。 2556 2557文档中存在泛型的使用,涉及以下泛型标记符: 2558- TypedArray: 指上述8种具体的ArkTS TypedArray。 2559 2560### 属性 2561 2562**系统能力:** SystemCapability.Utils.Lang 2563 2564**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2565 2566| 名称 | 类型 | 只读 | 可选 | 说明 | 2567| ------ | ------ | ---- | ---- | ----------------| 2568| buffer | ArrayBuffer | 是 | 否 | ArkTS TypedArray底层使用的buffer。| 2569| byteLength | number | 是 | 否 | ArkTS TypedArray的所占的字节数。| 2570| byteOffset | number | 是 | 否 | ArkTS TypedArray距离其ArrayBuffer起始位置的偏移。| 2571| length | number | 是 | 否 | ArkTS TypedArray元素个数。| 2572| BYTES_PER_ELEMENT | number | 是 | 否 | ArkTS TypedArray中每个元素所占用的字节数。| 2573 2574### constructor 2575constructor() 2576 2577构造函数,用于创建一个空ArkTS TypedArray对象。 2578 2579**系统能力:** SystemCapability.Utils.Lang 2580 2581**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2582 2583**错误码:** 2584 2585以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2586 2587| 错误码ID | 错误信息 | 2588| -------- | ------------------------------------------------------- | 2589| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2590 2591**示例:** 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 2607构造函数,用于创建一个指定长度的ArkTS TypedArray对象。 2608 2609**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2610 2611**系统能力:** SystemCapability.Utils.Lang 2612 2613**参数:** 2614 2615| 参数名 | 类型 | 必填 | 说明 | 2616| ------- | ------ | ---- | --------------------------- | 2617| length | number | 是 | 用于指定ArkTS TypedArray的长度。 | 2618 2619**错误码:** 2620 2621以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2622 2623| 错误码ID | 错误信息 | 2624| -------- | ------------------------------------------------------- | 2625| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2626 2627 2628**示例:** 2629 2630```ts 2631// 以长度参数构造对象 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 2645构造函数,以ArrayLike或ArkTS ArrayBuffer创建一个ArkTS TypedArray对象。 2646 2647**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2648 2649**系统能力:** SystemCapability.Utils.Lang 2650 2651**参数:** 2652 2653| 参数名 | 类型 | 必填 | 说明 | 2654| ------- | ------ | ---- | ------------------------------------------------------------ | 2655| array | ArrayLike\<number> \| ArrayBuffer | 是 | 用于构造ArkTS TypedArray的对象。当参数类型是ArrayBuffer时buffer所占的字节数须是4的整数倍。 | 2656 2657**错误码:** 2658 2659以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2660 2661| 错误码ID | 错误信息 | 2662| -------- | ------------------------------------------------------- | 2663| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2664 2665**示例:** 2666 2667```ts 2668// 例1 从一个ArrayLike构造对象 2669let arrayLike = [1, 3, 5]; 2670let array: collections.Uint32Array = new collections.Uint32Array(arrayLike); 2671``` 2672 2673```ts 2674// 例2 从一个ArrayBuffer构造对象 2675let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12); 2676let array: collections.Uint32Array = new collections.Uint32Array(arrayBuffer); 2677``` 2678 2679```ts 2680// 例3 从另一ArkTS TypedArray构造对象 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 2691构造函数,以ArrayBuffer创建一个ArkTS TypedArray对象。 2692 2693**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2694 2695**系统能力:** SystemCapability.Utils.Lang 2696 2697**参数:** 2698 2699| 参数名 | 类型 | 必填 | 说明 | 2700| ------- | ------ | ---- | ------------------------------------------ | 2701| buffer | ArrayBuffer | 是 | 用于构造ArkTS TypedArray的ArrayBuffer对象。buffer所占的字节数须是4的整数倍。| 2702| byteOffset | number | 否 | 指定buffer的字节偏移,默认为0。 | 2703| length | number | 否 | 指定ArkTS TypedArray的长度,默认为0。 | 2704 2705**错误码:** 2706 2707以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2708 2709| 错误码ID | 错误信息 | 2710| -------- | ------------------------------------------------------- | 2711| 10200012 | The TypedArray's constructor cannot be directly invoked. | 2712 2713**示例:** 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// 从int32Array对应buffer第4个字节开始,长度为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 2726从一个ArrayLike或者可迭代对象中创建一个ArkTS TypedArray对象。 2727 2728**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2729 2730**系统能力:** SystemCapability.Utils.Lang 2731 2732**参数:** 2733 2734| 参数名 | 类型 | 必填 | 说明 | 2735| ------- | ------ | ---- | --------------------------------------------------- | 2736| arrayLike | ArrayLike\<number> | 是 | 用于构造ArkTS TypedArray的ArrayLike对象。 | 2737 2738**返回值:** 2739 2740| 类型 | 说明 | 2741| ------------ | --------- | 2742| TypedArray | 新创建的ArkTS TypedArray对象。| 2743 2744**示例:** 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 2754从一个ArrayLike中创建一个ArkTS TypedArray对象。 2755 2756**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2757 2758**系统能力:** SystemCapability.Utils.Lang 2759 2760**参数:** 2761| 参数名 | 类型 | 必填 | 说明 | 2762| ------- | ------ | ---- | ------------------------------------------| 2763| arrayLike | ArrayLike\<T> | 是 | 用于构造ArrayLike对象。 | 2764| mapFn | [TypedArrayFromMapFn](#typedarrayfrommapfn)\<T, number> | 是 | 映射函数。| 2765 2766**返回值:** 2767 2768| 类型 | 说明 | 2769| ------------ | --------- | 2770| TypedArray | 新创建的ArkTS TypedArray对象。| 2771 2772**示例:** 2773 2774```ts 2775// 例1 从一个对象创建 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// 例2 从一个字符数组创建 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// 例3 从一个字符串创建 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 2798从一个可迭代对象中创建一个ArkTS TypedArray对象。 2799 2800**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2801 2802**系统能力:** SystemCapability.Utils.Lang 2803 2804**参数:** 2805| 参数名 | 类型 | 必填 | 说明 | 2806| ------- | ------ | ---- | -----------------------------------| 2807| iterable | Iterable\<number> | 是 | 用于构造的可迭代对象。 | 2808| mapFn | [TypedArrayFromMapFn](#typedarrayfrommapfn)\<number, number> | 否 | 映射函数。如果省略,则不对元素进行加工处理。| 2809 2810**返回值:** 2811 2812| 类型 | 说明 | 2813| ------------ | --------- | 2814| TypedArray | 新创建的ArkTS TypedArray对象。| 2815 2816**示例:** 2817 2818```ts 2819// 例1 不指定映射函数 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// 例2 指定映射函数 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 2836从ArkTS TypedArray指定范围内的元素依次拷贝到目标位置。 2837 2838**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2839 2840**系统能力:** SystemCapability.Utils.Lang 2841 2842**参数:** 2843 2844| 参数名 | 类型 | 必填 | 说明 | 2845| ------- | ------ | ---- | ------------------------------------------------------------ | 2846| target | number | 是 | 目标起始位置的下标。 | 2847| start | number | 是 | 源起始位置下标,如果`start < 0`,则会从`start + typedarray.length`位置开始。 | 2848| end | number | 否 | 源终止位置下标,如果`end < 0`,则会从`end + typedarray.length`位置终止。默认为ArkTS TypedArray的长度。| 2849 2850**返回值:** 2851 2852| 类型 | 说明 | 2853| ------------ | --------- | 2854| TypedArray | 修改后的TypedArray。 | 2855 2856**错误码:** 2857 2858以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2859 2860| 错误码ID | 错误信息 | 2861| -------- | ------------------------------------------------ | 2862| 10200011 | The copyWithin method cannot be bound. | 2863| 10200201 | Concurrent modification exception. | 2864 2865**示例:** 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 2876测试ArkTS TypedArray中的是否存在元素满足指定条件。 2877 2878**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2879 2880**系统能力:** SystemCapability.Utils.Lang 2881 2882**参数:** 2883 2884| 参数名 | 类型 | 必填 | 说明 | 2885| ------- | ------ | ---- | ---------------------------------------------------- | 2886| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | 是 | 用于测试的断言函数。| 2887 2888**返回值:** 2889 2890| 类型 | 说明 | 2891| ------------ | --------- | 2892| boolean | 如果存在元素满足指定条件返回true,否则返回false。| 2893 2894**错误码:** 2895 2896以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2897 2898| 错误码ID | 错误信息 | 2899| -------- | ---------------------------------- | 2900| 10200011 | The some method cannot be bound. | 2901| 10200201 | Concurrent modification exception. | 2902 2903**示例:** 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 2917测试ArkTS TypedArray中的所有元素是否满足指定条件。 2918 2919**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2920 2921**系统能力:** SystemCapability.Utils.Lang 2922 2923**参数:** 2924 2925| 参数名 | 类型 | 必填 | 说明 | 2926| ------- | ------ | ---- | ----------------------------------------------------- | 2927| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | 是 | 用于测试的断言函数。| 2928 2929**返回值:** 2930 2931| 类型 | 说明 | 2932| ------------ | --------- | 2933| boolean | 如果所有元素都满足指定条件则返回true,否则返回false。| 2934 2935**错误码:** 2936 2937以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2938 2939| 错误码ID | 错误信息 | 2940| -------- | ------------------------------------------------- | 2941| 10200011 | The every method cannot be bound. | 2942| 10200201 | Concurrent modification exception. | 2943 2944**示例:** 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 2958使用特定值填充ArkTS TypedArray指定范围的全部元素。 2959 2960**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 2961 2962**系统能力:** SystemCapability.Utils.Lang 2963 2964**参数:** 2965 2966| 参数名 | 类型 | 必填 | 说明 | 2967| ------- | ------ | ---- | --------------------------------------------------------| 2968| value | number | 是 | 待填充的值。| 2969| start | number | 否 | 开始填充的索引,如果`start < 0`,则会从`start + typedarray.length`位置开始。默认值为0。| 2970| end | number | 否 | 结束填充的索引,如果`end < 0`,则会到`end + typedarray.length`位置结束。默认为ArkTS TypedArray的长度。| 2971 2972**返回值:** 2973 2974| 类型 | 说明 | 2975| ------------ | --------- | 2976| TypedArray | 填充后的TypedArray。| 2977 2978**错误码:** 2979 2980以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 2981 2982| 错误码ID | 错误信息 | 2983| -------- | ------------------------------------------------- | 2984| 10200011 | The fill method cannot be bound. | 2985| 10200201 | Concurrent modification exception. | 2986 2987**示例:** 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 2999返回一个新ArkTS TypedArray,其包含满足指定条件的所有元素。 3000 3001**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3002 3003**系统能力:** SystemCapability.Utils.Lang 3004 3005**参数:** 3006 3007| 参数名 | 类型 | 必填 | 说明 | 3008| ------- | ------ | ---- | ------------------------------------------------------ | 3009| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | 是 | 用于元素过滤的断言函数。 | 3010 3011**返回值:** 3012 3013| 类型 | 说明 | 3014| ------------ | --------- | 3015| TypedArray| 过滤后的ArkTS TypedArray对象。| 3016 3017**错误码:** 3018 3019以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3020 3021| 错误码ID | 错误信息 | 3022| -------- | ------------------------------------------------- | 3023| 10200011 | The filter method cannot be bound. | 3024| 10200201 | Concurrent modification exception. | 3025 3026**示例:** 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 3037返回ArkTS TypedArray中第一个满足指定条件的元素的值,如果所有元素都不满足,则返回undefined。 3038 3039**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3040 3041**系统能力:** SystemCapability.Utils.Lang 3042 3043**参数:** 3044 3045| 参数名 | 类型 | 必填 | 说明 | 3046| ------- | ------ | ---- | ------------------------------------------------------------ | 3047| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | 是 | 用于元素查找的断言函数。| 3048 3049**返回值:** 3050 3051| 类型 | 说明 | 3052| ------------ | --------- | 3053| number \| undefined | 第一个满足条件的元素的值;如果所有元素都不满足条件,则返回undefined。| 3054 3055**错误码:** 3056 3057以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3058 3059| 错误码ID | 错误信息 | 3060| -------- | ------------------------------------------------- | 3061| 10200011 | The find method cannot be bound. | 3062| 10200201 | Concurrent modification exception. | 3063 3064**示例:** 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 3075返回ArkTS TypedArray中第一个满足指定条件的元素索引,如果所有元素都不满足,则返回-1。 3076 3077**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3078 3079**系统能力:** SystemCapability.Utils.Lang 3080 3081**参数:** 3082 3083| 参数名 | 类型 | 必填 | 说明 | 3084| ------- | ------ | ---- | ------------------------------------------------------------ | 3085| predicate | [TypedArrayPredicateFn](#typedarraypredicatefn)\<number, TypedArray> | 是 | 用于元素查找的断言函数。| 3086 3087**返回值:** 3088 3089| 类型 | 说明 | 3090| ------------ | --------- | 3091| number | 第一个满足条件的元素索引;如果所有元素都不满足条件,否返回-1。| 3092 3093**错误码:** 3094 3095以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3096 3097| 错误码ID | 错误信息 | 3098| -------- | ------------------------------------------------- | 3099| 10200011 | The findIndex method cannot be bound. | 3100| 10200201 | Concurrent modification exception. | 3101 3102**示例:** 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 3112对ArkTS TypedArray中的每个元素执行提供的回调函数。 3113 3114**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3115 3116**系统能力:** SystemCapability.Utils.Lang 3117 3118**参数:** 3119 3120| 参数名 | 类型 | 必填 | 说明 | 3121| ------- | ------ | ---- | ------------------------------------------------------------ | 3122| callbackFn | [TypedArrayForEachCallback](#typedarrayforeachcallback)\<number, TypedArray> | 是 | 用于对每个元素执行的回调函数。| 3123 3124 3125**错误码:** 3126 3127以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3128 3129| 错误码ID | 错误信息 | 3130| -------- | ------------------------------------------------- | 3131| 10200011 | The forEach method cannot be bound. | 3132| 10200201 | Concurrent modification exception. | 3133 3134**示例:** 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 3146返回在ArkTS TypedArray中给定元素的第一个索引,如果不存在,则返回-1。 3147 3148**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3149 3150**系统能力:** SystemCapability.Utils.Lang 3151 3152**参数:** 3153 3154| 参数名 | 类型 | 必填 | 说明 | 3155| ------------- | ------ | ---- | ---------------------------| 3156| searchElement | number | 是 | 待索引的值。 | 3157| fromIndex | number | 否 | 搜索的起始下标。默认值为0。如果下标大于等于ArkTS TypedArray的长度,则返回-1。如果提供的下标值是负数,则被当做距离数组尾部的偏移,从前到后搜索。 | 3158 3159**返回值:** 3160 3161| 类型 | 说明 | 3162| ------------ | --------- | 3163| number | 数组中元素的第一个索引;没有找到,则返回-1。 | 3164 3165**错误码:** 3166 3167以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3168 3169| 错误码ID | 错误信息 | 3170| -------- | ------------------------------------------------- | 3171| 10200011 | The indexOf method cannot be bound. | 3172| 10200201 | Concurrent modification exception. | 3173 3174**示例:** 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 3187将ArkTS TypedArray的所有元素拼接成一个字符串,元素之间使用指定的分隔符分隔。 3188 3189**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3190 3191**系统能力:** SystemCapability.Utils.Lang 3192 3193**参数:** 3194 3195| 参数名 | 类型 | 必填 | 说明 | 3196| --------- | ------ | ---- | ---------------------------------------------------- | 3197| separator | string | 否 | 分隔字符串。如果省略,则使用逗号分隔。 | 3198 3199**返回值:** 3200 3201| 类型 | 说明 | 3202| ------------ | --------- | 3203| string | 包含所有元素拼接成的字符串。如果ArkTS TypedArray为空,则返回空字符串。| 3204 3205**错误码:** 3206 3207以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3208 3209| 错误码ID | 错误信息 | 3210| -------- | ------------------------------------------------- | 3211| 10200011 | The join method cannot be bound. | 3212| 10200201 | Concurrent modification exception. | 3213 3214**示例:** 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 3224对ArkTS TypedArray中的每个元素应用指定的回调函数,并使用结果创建一个新的ArkTS TypedArray对象。 3225 3226**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3227 3228**系统能力:** SystemCapability.Utils.Lang 3229 3230**参数:** 3231| 参数名 | 类型 | 必填 | 说明 | 3232| --------- | ------ | ---- | ---------------------------------------------------- | 3233| callbackFn | [TypedArrayMapCallback](#typedarraymapcallback)\<number, TypedArray> | 是 | 回调函数。 | 3234 3235 3236**返回值:** 3237 3238| 类型 | 说明 | 3239| ------------ | --------- | 3240| TypedArray | 新ArkTS TypedArray对象。| 3241 3242**错误码:** 3243 3244以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3245 3246| 错误码ID | 错误信息 | 3247| -------- | ------------------------------------------------- | 3248| 10200011 | The map method cannot be bound. | 3249| 10200201 | Concurrent modification exception. | 3250 3251**示例:** 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 3261对ArkTS TypedArray中的每个元素执行归约函数,并返回最终的归约结果。 3262 3263**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3264 3265**系统能力:** SystemCapability.Utils.Lang 3266 3267**参数:** 3268| 参数名 | 类型 | 必填 | 说明 | 3269| ---------- | ---------------------- | ---- | ------------------------------------------------------------ | 3270| callbackFn | [TypedArrayReduceCallback](#typedarrayreducecallback)\<number, number, TypedArray> | 是 | 归约函数。 | 3271 3272**返回值:** 3273 3274| 类型 | 说明 | 3275| ------------ | --------- | 3276| number | 由归约函数返回的结果。| 3277 3278**错误码:** 3279 3280以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3281 3282| 错误码ID | 错误信息 | 3283| -------- | ------------------------------------------------ | 3284| 10200011 | The reduce method cannot be bound. | 3285| 10200201 | Concurrent modification exception. | 3286 3287**示例:** 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 3298对ArkTS TypedArray中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。 3299 3300**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3301 3302**系统能力:** SystemCapability.Utils.Lang 3303 3304**参数:** 3305| 参数名 | 类型 | 必填 | 说明 | 3306| --------- | ------ | ---- | --------------------------------------------------- | 3307| callbackFn | [TypedArrayReduceCallback](#typedarrayreducecallback)\<number, number, TypedArray> | 是 | 归约函数。 | 3308| initialValue | number | 是 | 初始值。 | 3309 3310 3311**返回值:** 3312 3313| 类型 | 说明 | 3314| ------------ | --------- | 3315| number | 由归约函数返回的结果。 | 3316 3317**错误码:** 3318 3319以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3320 3321| 错误码ID | 错误信息 | 3322| -------- | ------------------------------------------------- | 3323| 10200011 | The reduce method cannot be bound. | 3324| 10200201 | Concurrent modification exception. | 3325 3326**示例:** 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 3337对ArkTS TypedArray中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。 3338 3339**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3340 3341**系统能力:** SystemCapability.Utils.Lang 3342 3343**参数:** 3344 3345| 参数名 | 类型 | 必填 | 说明 | 3346| --------- | ------ | ---- | ---------------------------------------------------- | 3347| callbackFn | [TypedArrayReduceCallback](#typedarrayreducecallback)\<U, number, TypedArray> | 是 | 归约函数。 | 3348| initialValue | U | 是 | 初始值。 | 3349 3350**返回值:** 3351 3352| 类型 | 说明 | 3353| ------------ | --------- | 3354| U | 由归约函数返回的结果。 | 3355 3356**错误码:** 3357 3358以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3359 3360| 错误码ID | 错误信息 | 3361| -------- | ------------------------------------------------- | 3362| 10200011 | The reduce method cannot be bound. | 3363| 10200201 | Concurrent modification exception. | 3364 3365**示例:** 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 3376反转ArkTS TypedArray。 3377 3378**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3379 3380**系统能力:** SystemCapability.Utils.Lang 3381 3382**返回值:** 3383 3384| 类型 | 说明 | 3385| ------------ | --------- | 3386| TypedArray | 反转后的ArkTS TypedArray对象。| 3387 3388**错误码:** 3389 3390以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3391 3392| 错误码ID | 错误信息 | 3393| -------- | ------------------------------------------------- | 3394| 10200011 | The reverse method cannot be bound. | 3395| 10200201 | Concurrent modification exception. | 3396 3397**示例:** 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 3407将传入的ArrayLike元素依次写入到指定的起始位置。 3408 3409**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3410 3411**系统能力:** SystemCapability.Utils.Lang 3412 3413**参数:** 3414| 参数名 | 类型 | 必填 | 说明 | 3415| --------- | ------ | ---- | ---------------------------------------------------- | 3416| array | ArrayLike\<number> | 是 | 用于设置的ArrayLike对象。| 3417| offset | number | 否 | 写入的起始位置。默认为0。| 3418 3419**错误码:** 3420 3421以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3422 3423| 错误码ID | 错误信息 | 3424| -------- | ------------------------------------------------- | 3425| 10200011 | The set method cannot be bound. | 3426| 10200201 | Concurrent modification exception. | 3427 3428**示例:** 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 3439返回一个新的ArkTS TypedArray对象,其包含原ArkTS TypedArray指定范围的内容。 3440 3441**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3442 3443**系统能力:** SystemCapability.Utils.Lang 3444 3445**参数:** 3446 3447| 参数名 | 类型 | 必填 | 说明 | 3448| ------ | ------ | ---- | -----------------------------------------------------| 3449| start | number | 否 | 开始索引,如果`start < 0`,则会从`start + typedarray.length`位置开始。默认为0。 | 3450| end | number | 否 | 结束索引(不包括该元素),如果`end < 0`,则会到`end + typedarray.length`位置结束。默认为ArkTS TypedArray的长度。| 3451 3452**返回值:** 3453 3454| 类型 | 说明 | 3455| ------------ | --------- | 3456| TypedArray | 新的ArkTS TypedArray对象。 | 3457 3458**错误码:** 3459 3460以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3461 3462| 错误码ID | 错误信息 | 3463| -------- | ------------------------------------------------- | 3464| 10200011 | The slice method cannot be bound. | 3465| 10200201 | Concurrent modification exception. | 3466 3467**示例:** 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 3479对ArkTS TypedArray进行排序,并返回排序后的ArkTS TypedArray对象。 3480 3481**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3482 3483**系统能力:** SystemCapability.Utils.Lang 3484 3485**参数:** 3486 3487| 参数名 | 类型 | 必填 | 说明 | 3488| --------- | ---------------------- | ---- | ------------------------------------------ | 3489| compareFn | [TypedArrayCompareFn](#typedarraycomparefn)\<number> | 否 | 用于确定元素顺序的函数。默认使用升序排序。 | 3490 3491**返回值:** 3492 3493| 类型 | 说明 | 3494| ------------ | --------- | 3495| TypedArray | 排序后的ArkTS TypedArray对象。| 3496 3497**错误码:** 3498 3499以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3500 3501| 错误码ID | 错误信息 | 3502| -------- | ------------------------------------------ | 3503| 10200011 | The sort method cannot be bound. | 3504| 10200201 | Concurrent modification exception. | 3505 3506**示例:** 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 3518返回一个新的、基于相同ArkTS ArrayBuffer的ArkTS TypedArray对象。 3519 3520**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3521 3522**系统能力:** SystemCapability.Utils.Lang 3523 3524**参数:** 3525 3526| 参数名 | 类型 | 必填 | 说明 | 3527| ------ | ------ | ---- | ------------------------------------------------- | 3528| begin | number | 否 | 开始索引,如果`begin < 0`,则会从`begin + typedarray.length`位置开始。默认值为0。 | 3529| end | number | 否 | 结束索引(不包括该元素),如果`end < 0`,则会到`end + typedarray.length`位置结束。默认为ArkTS TypedArray的长度。 | 3530 3531**返回值:** 3532 3533| 类型 | 说明 | 3534| ------------ | --------- | 3535| TypedArray | 新的ArkTS TypedArray对象。| 3536 3537**错误码:** 3538 3539以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3540 3541| 错误码ID | 错误信息 | 3542| -------- | -------------------------------------------------| 3543| 10200011 | The subarray method cannot be bound. | 3544| 10200201 | Concurrent modification exception. | 3545 3546**示例:** 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 3557返回指定下标的元素,如果不存在,则返回undefined。 3558 3559**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3560 3561**系统能力:** SystemCapability.Utils.Lang 3562 3563**参数:** 3564| 参数名 | 类型 | 必填 | 说明 | 3565| ------ | ------ | ---- | ------------------------------------------------------------ | 3566| index | number | 是 | 要返回的Array元素的索引(从零开始),取值为整数。如果`index < 0`,则会访问`index + typedarray.length`位置的元素。| 3567 3568**返回值:** 3569 3570| 类型 | 说明 | 3571| ------------ | --------- | 3572| number \| undefined| 指定下标的元素;如果不存在,则返回undefined。| 3573 3574**错误码:** 3575 3576以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3577 3578| 错误码ID | 错误信息 | 3579| -------- | ------------------------------------------------ | 3580| 10200011 | The at method cannot be bound. | 3581| 10200201 | Concurrent modification exception. | 3582 3583**示例:** 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 3595判断ArkTS TypedArray是否包含特定元素。 3596 3597**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3598 3599**系统能力:** SystemCapability.Utils.Lang 3600 3601**参数:** 3602| 参数名 | 类型 | 必填 | 说明 | 3603| ------ | ------ | ---- | --------------------------------------- | 3604| searchElement | number | 是 | 待搜索的元素。 | 3605| fromIndex | number | 否 | 开始搜索的索引,如果`fromIndex < 0`,则会从`fromIndex + typedarray.length`位置开始。默认值为0。| 3606 3607**返回值:** 3608 3609| 类型 | 说明 | 3610| ------- | ---------------------------------------------------------- | 3611| boolean | 如果ArkTS TypedArray包含指定的元素,则返回true;否则返回false。| 3612 3613 3614**错误码:** 3615 3616以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3617 3618| 错误码ID | 错误信息 | 3619| -------- | ------------------------------------------------- | 3620| 10200011 | The includes method cannot be bound. | 3621| 10200201 | Concurrent modification exception. | 3622 3623**示例:** 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 3635返回一个新的迭代器对象,该对象包含ArkTS TypedArray中每个元素的键值对。 3636 3637**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3638 3639**系统能力:** SystemCapability.Utils.Lang 3640 3641**返回值:** 3642 3643| 类型 | 说明 | 3644| ------------ | --------- | 3645| IterableIterator\<[number, number]>| 新的迭代器对象。 | 3646 3647**错误码:** 3648 3649以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3650 3651| 错误码ID | 错误信息 | 3652| -------- | ------------------------------------------------- | 3653| 10200011 | The entries method cannot be bound. | 3654| 10200201 | Concurrent modification exception. | 3655 3656**示例:** 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 3669返回一个新的迭代器对象,该对象包含ArkTS TypedArray中每个元素的键(下标)。 3670 3671**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3672 3673**系统能力:** SystemCapability.Utils.Lang 3674 3675**返回值:** 3676 3677| 类型 | 说明 | 3678| ------------ | --------- | 3679| IterableIterator\<number> | 新的迭代器对象。| 3680 3681**错误码:** 3682 3683以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3684 3685| 错误码ID | 错误信息 | 3686| -------- | ------------------------------------------------- | 3687| 10200011 | The keys method cannot be bound. | 3688| 10200201 | Concurrent modification exception. | 3689 3690**示例:** 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,4 3697} 3698``` 3699 3700### values 3701values(): IterableIterator\<number> 3702 3703返回一个新的迭代器对象,该对象包含ArkTS TypedArray中每个元素的值。 3704 3705**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3706 3707**系统能力:** SystemCapability.Utils.Lang 3708 3709**返回值:** 3710 3711| 类型 | 说明 | 3712| ------------ | --------- | 3713| IterableIterator\<number> | 新的迭代器对象。| 3714 3715**错误码:** 3716 3717以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3718 3719| 错误码ID | 错误信息 | 3720| -------- | ------------------------------------------------- | 3721| 10200011 | The values method cannot be bound. | 3722| 10200201 | Concurrent modification exception. | 3723 3724**示例:** 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,5 3731} 3732``` 3733 3734### [Symbol.iterator] 3735 3736[Symbol.iterator]\(): IterableIterator<number> 3737 3738返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 3739 3740> **说明:** 3741> 3742> 本接口不支持在.ets文件中使用。 3743 3744**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3745 3746**系统能力:** SystemCapability.Utils.Lang 3747 3748**返回值:** 3749 3750| 类型 | 说明 | 3751| ------------------------- | ---------------- | 3752| IterableIterator<T> | 返回一个迭代器。 | 3753 3754**错误码:** 3755 3756以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3757 3758| 错误码ID | 错误信息 | 3759| -------- | ------------------------------------------- | 3760| 10200011 | The Symbol.iterator method cannot be bound. | 3761 3762**示例:** 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 3776返回TypedArray指定索引位置的元素,适用于Int8Array,Int16Array,Int32Array,Uint8Array,Uint16Array,Uint32Array,Float32Array和Float64Array 8种数据类型。 3777 3778**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 3779 3780**系统能力:** SystemCapability.Utils.Lang 3781 3782| 参数名 | 类型 | 必填 | 说明 | 3783| ----- | ------ | ---- | -------------------------- | 3784| index | number | 是 | 所需代码单元的从零开始的索引。| 3785 3786**返回值:** 3787 3788| 类型 | 说明 | 3789| ----- | ---------------------| 3790| number | 返回number数据类型。 | 3791 3792**示例:** 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 3815BitVector是一种线性数据结构,底层基于数组实现。BitVector中存储元素为bit值,能存储和处理bit级别的操作。 3816 3817### 属性 3818 3819**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3820 3821**系统能力:** SystemCapability.Utils.Lang 3822 3823| 名称 | 类型 | 只读 | 可选 | 说明 | 3824| ------ | ------ | ---- | ---- | --------------------- | 3825| length | number | 是 | 否 | BitVector的元素个数。 | 3826 3827 3828### constructor 3829 3830constructor(length: number) 3831 3832BitVector的构造函数。 3833 3834**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3835 3836**系统能力:** SystemCapability.Utils.Lang 3837 3838**参数:** 3839 3840| 参数名 | 类型 | 必填 | 说明 | 3841| ------ | ------ | ---- | ----------------------- | 3842| length | number | 是 | 初始化BitVector的长度。 | 3843 3844**示例:** 3845 3846```ts 3847let bitVector: collections.BitVector = new collections.BitVector(0); 3848``` 3849 3850 3851### push 3852 3853push(element:number): boolean 3854 3855在BitVector尾部插入元素。 3856 3857**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3858 3859**系统能力:** SystemCapability.Utils.Lang 3860 3861**参数:** 3862 3863| 参数名 | 类型 | 必填 | 说明 | 3864| ------- | ------ | ---- | ----------------------------------- | 3865| element | number | 是 | 待插入的元素,0表示0,其余值表示1。 | 3866 3867**返回值:** 3868 3869| 类型 | 说明 | 3870| ------- | --------------------------------- | 3871| boolean | 插入成功返回true,失败返回false。 | 3872 3873**错误码:** 3874 3875以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 3876 3877| 错误码ID | 错误信息 | 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**示例:** 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 3898弹出BitVector尾部的元素。 3899 3900**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3901 3902**系统能力:** SystemCapability.Utils.Lang 3903 3904**返回值:** 3905 3906| 类型 | 说明 | 3907| ------ | ------------------------------------------ | 3908| number | 弹出BitVector尾部的元素,其值为对应bit值。 | 3909 3910**错误码:** 3911 3912以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 3913 3914| 错误码ID | 错误信息 | 3915| -------- | ------------------------------- | 3916| 10200011 | The pop method cannot be bound. | 3917| 10200201 | Concurrent modification error. | 3918 3919**示例:** 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 3936判断范围内是否包含特定bit值。 3937 3938**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3939 3940**系统能力:** SystemCapability.Utils.Lang 3941 3942**参数:** 3943 3944| 参数名 | 类型 | 必填 | 说明 | 3945| --------- | ------ | ---- | ------------------------------------ | 3946| element | number | 是 | 待判断的bit值,0表示0,其余值表示1。 | 3947| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 3948| toIndex | number | 是 | 范围终止索引,包含本索引值。 | 3949 3950**返回值:** 3951 3952| 类型 | 说明 | 3953| ------- | -------------------------------------- | 3954| boolean | 包含特定bit值返回true,否则返回false。 | 3955 3956**错误码:** 3957 3958以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 3959 3960| 错误码ID | 错误信息 | 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**示例:** 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 3984将BitVector中指定范围的元素均设为特定bit值。 3985 3986**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 3987 3988**系统能力:** SystemCapability.Utils.Lang 3989 3990**参数:** 3991 3992| 参数名 | 类型 | 必填 | 说明 | 3993| --------- | ------ | ---- | ---------------------------------- | 3994| element | number | 是 | 待设置的bit值,0表示0,其余表示1。 | 3995| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 3996| toIndex | number | 是 | 范围终止索引,不包含本索引值。 | 3997 3998**错误码:** 3999 4000以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4001 4002| 错误码ID | 错误信息 | 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**示例:** 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 4025将BitVector中所有元素均设为特定bit值。 4026 4027**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4028 4029**系统能力:** SystemCapability.Utils.Lang 4030 4031**参数:** 4032 4033| 参数名 | 类型 | 必填 | 说明 | 4034| ------- | ------ | ---- | ----------------------------------- | 4035| element | number | 是 | 待设置的元素,0表示0,其余值表示1。 | 4036 4037**错误码:** 4038 4039以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4040 4041| 错误码ID | 错误信息 | 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**示例:** 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 4063获取指定范围内的bit值。 4064 4065**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4066 4067**系统能力:** SystemCapability.Utils.Lang 4068 4069**参数:** 4070 4071| 参数名 | 类型 | 必填 | 说明 | 4072| --------- | ------ | ---- | ------------------------------ | 4073| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 4074| toIndex | number | 是 | 范围终止索引,不包含本索引值。 | 4075 4076**返回值:** 4077 4078| 类型 | 说明 | 4079| --------- | ---------------------------------- | 4080| BitVector | 指定范围内的bit值组成的BitVector。 | 4081 4082**错误码:** 4083 4084以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4085 4086| 错误码ID | 错误信息 | 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**示例:** 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 4110改变BitVector的长度。 4111 4112若size大于原BitVector的长度,则扩充原BitVector的长度,多出的部分其元素设置为0; 4113 4114若size小于等于原BitVector的长度,则将原BitVector按size长度大小裁剪。 4115 4116**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4117 4118**系统能力:** SystemCapability.Utils.Lang 4119 4120**参数:** 4121 4122| 参数名 | 类型 | 必填 | 说明 | 4123| ------ | ------ | ---- | ---------------- | 4124| size | number | 是 | 需要改变的长度。 | 4125 4126**错误码:** 4127 4128以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4129 4130| 错误码ID | 错误信息 | 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**示例:** 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 4155统计指定范围内获取指定bit值的数量。 4156 4157**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4158 4159**系统能力:** SystemCapability.Utils.Lang 4160 4161**参数:** 4162 4163| 参数名 | 类型 | 必填 | 说明 | 4164| --------- | ------ | ---- | ------------------------------------ | 4165| element | number | 是 | 待统计的bit值,0表示0,其余值表示1。 | 4166| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 4167| toIndex | number | 是 | 范围终止索引,不包含本索引值。 | 4168 4169**返回值:** 4170 4171| 类型 | 说明 | 4172| ------ | ----------------------------------- | 4173| number | 统计指定范围内获取指定bit值的数量。 | 4174 4175**错误码:** 4176 4177以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4178 4179| 错误码ID | 错误信息 | 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**示例:** 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 4203返回指定bit值首次出现时的索引值,查找失败返回-1。 4204 4205**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4206 4207**系统能力:** SystemCapability.Utils.Lang 4208 4209**参数:** 4210 4211| 参数名 | 类型 | 必填 | 说明 | 4212| --------- | ------ | ---- | ------------------------------------ | 4213| element | number | 是 | 待统计的bit值,0表示0,其余值表示1。 | 4214| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 4215| toIndex | number | 是 | 范围终止索引,不包含本索引值。 | 4216 4217**返回值:** 4218 4219| 类型 | 说明 | 4220| ------ | ------------------------------------------------- | 4221| number | 返回指定bit值首次出现时的下标值,查找失败返回-1。 | 4222 4223**错误码:** 4224 4225以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4226 4227| 错误码ID | 错误信息 | 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**示例:** 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 4251返回指定bit值最后一次出现时的下标值,查找失败返回-1。 4252 4253**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4254 4255**系统能力:** SystemCapability.Utils.Lang 4256 4257**参数:** 4258 4259| 参数名 | 类型 | 必填 | 说明 | 4260| --------- | ------ | ---- | ------------------------------------ | 4261| element | number | 是 | 待统计的bit值,0表示0,其余值表示1。 | 4262| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 4263| toIndex | number | 是 | 范围终止索引,不包含本索引值。 | 4264 4265**返回值:** 4266 4267| 类型 | 说明 | 4268| ------ | ----------------------------------------------------- | 4269| number | 返回指定bit值最后一次出现时的下标值,查找失败返回-1。 | 4270 4271**错误码:** 4272 4273以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4274 4275| 错误码ID | 错误信息 | 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**示例:** 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 4299翻转BitVector指定索引处的bit值,0翻转为1,1翻转为0。 4300 4301**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4302 4303**系统能力:** SystemCapability.Utils.Lang 4304 4305**参数:** 4306 4307| 参数名 | 类型 | 必填 | 说明 | 4308| ------ | ------ | ---- | ---------- | 4309| index | number | 是 | 指定索引。 | 4310 4311**错误码:** 4312 4313以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4314 4315| 错误码ID | 错误信息 | 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**示例:** 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 4338翻转BitVector指定范围内的bit值,0翻转为1,1翻转为0。 4339 4340**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4341 4342**系统能力:** SystemCapability.Utils.Lang 4343 4344**参数:** 4345 4346| 参数名 | 类型 | 必填 | 说明 | 4347| --------- | ------ | ---- | ------------------------------ | 4348| fromIndex | number | 是 | 范围起始索引,包含本索引值。 | 4349| toIndex | number | 是 | 范围终止索引,不包含本索引值。 | 4350 4351**错误码:** 4352 4353以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 4354 4355| 错误码ID | 错误信息 | 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**示例:** 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 4378返回一个新的迭代器对象,该对象包含BitVector中每个元素的值。 4379 4380**原子化服务API**:从API version 12 开始,该接口支持在原子化服务中使用。 4381 4382**系统能力:** SystemCapability.Utils.Lang 4383 4384**返回值:** 4385 4386| 类型 | 说明 | 4387| ------------------------------ | ----------------------------- | 4388| IterableIterator<number> | 返回一个BitVector迭代器对象。 | 4389 4390**错误码:** 4391 4392以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 4393 4394| 错误码ID | 错误信息 | 4395| -------- | ---------------------------------- | 4396| 10200011 | The values method cannot be bound. | 4397| 10200201 | Concurrent modification error. | 4398 4399**示例:** 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,0 4414``` 4415 4416### [Symbol.iterator] 4417 4418[Symbol.iterator]\(): IterableIterator<number> 4419 4420返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 4421 4422> **说明:** 4423> 4424> 本接口不支持在.ets文件中使用。 4425 4426**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4427 4428**系统能力:** SystemCapability.Utils.Lang 4429 4430**返回值:** 4431 4432| 类型 | 说明 | 4433| ------------------------- | ---------------- | 4434| IterableIterator<number> | 返回一个迭代器。 | 4435 4436**错误码:** 4437 4438以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 4439 4440| 错误码ID | 错误信息 | 4441| -------- | ------------------------------------------- | 4442| 10200011 | The Symbol.iterator method cannot be bound. | 4443 4444**示例:** 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 4463返回BitVector指定索引位置的元素。 4464 4465**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 4466 4467**系统能力:** SystemCapability.Utils.Lang 4468 4469| 参数名 | 类型 | 必填 | 说明 | 4470| ----- | ------ | ---- | -------------------------- | 4471| index | number | 是 | 所需代码单元的从零开始的索引。| 4472 4473**返回值:** 4474 4475| 类型 | 说明 | 4476| ----- | ---------------------| 4477| number | 返回number数据类型。 | 4478 4479**示例:** 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```