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