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