1# Class (Uint8ClampedArray) 2<!--Kit: ArkTS--> 3<!--Subsystem: CommonLibrary--> 4<!--Owner: @lijiamin2025--> 5<!--Designer: @weng-changcheng--> 6<!--Tester: @kirl75; @zsw_zhushiwei--> 7<!--Adviser: @ge-yafang--> 8> **说明:** 9> 10> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 11> 12> 此模块仅支持在ArkTS文件(文件后缀为.ets)中导入使用。 13 14一种线性数据结构,底层基于[ArkTS ArrayBuffer](arkts-apis-arkts-collections-ArrayBuffer.md)实现。 15 16**装饰器类型:**\@Sendable 17 18## 导入模块 19 20```ts 21import { collections } from '@kit.ArkTS'; 22``` 23 24## 属性 25 26**系统能力:** SystemCapability.Utils.Lang 27 28**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 29 30| 名称 | 类型 | 只读 | 可选 | 说明 | 31| ------ | ------ | ---- | ---- | ----------------| 32| buffer | ArrayBuffer | 是 | 否 | ArkTS Uint8ClampedArray底层使用的buffer。| 33| byteLength | number | 是 | 否 | ArkTS Uint8ClampedArray的所占的字节数。| 34| byteOffset | number | 是 | 否 | ArkTS Uint8ClampedArray距离其ArrayBuffer起始位置的偏移。| 35| length | number | 是 | 否 | ArkTS Uint8ClampedArray元素个数。| 36| BYTES_PER_ELEMENT | number | 是 | 否 | ArkTS Uint8ClampedArray中每个元素所占用的字节数。| 37 38## constructor 39constructor() 40 41构造函数,用于创建一个空ArkTS Uint8ClampedArray对象。 42 43**系统能力:** SystemCapability.Utils.Lang 44 45**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 46 47**错误码:** 48 49以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 50 51| 错误码ID | 错误信息 | 52| -------- | ------------------------------------------------------- | 53| 10200012 | The Uint8ClampedArray's constructor cannot be directly invoked. | 54 55**示例:** 56 57```ts 58let uint8ClampedArray: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(); 59``` 60 61## constructor 62constructor(length: number) 63 64构造函数,用于创建一个指定长度的ArkTS Uint8ClampedArray对象。 65 66**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 67 68**系统能力:** SystemCapability.Utils.Lang 69 70**参数:** 71 72| 参数名 | 类型 | 必填 | 说明 | 73| ------- | ------ | ---- | --------------------------- | 74| length | number | 是 | 用于指定ArkTS Uint8ClampedArray的长度。 | 75 76**错误码:** 77 78以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 79 80| 错误码ID | 错误信息 | 81| -------- | ------------------------------------------------------- | 82| 401 | Parameter error. | 83| 10200012 | The Uint8ClampedArray's constructor cannot be directly invoked. | 84 85 86**示例:** 87 88```ts 89// 以长度参数构造对象 90let uint8ClampedArray: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(12); 91``` 92 93## constructor 94constructor(elements: Iterable\<number>) 95 96构造函数,以Iterable创建一个ArkTS Uint8ClampedArray对象。 97 98**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 99 100**系统能力:** SystemCapability.Utils.Lang 101 102**参数:** 103 104| 参数名 | 类型 | 必填 | 说明 | 105| ------- | ------ | ---- | ------------------------------------------------------------ | 106| elements | Iterable\<number> | 是 | 可迭代数字集合,用于构造ArkTS Uint8ClampedArray对象。 | 107 108**错误码:** 109 110以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 111 112| 错误码ID | 错误信息 | 113| -------- | ------------------------------------------------------- | 114| 401 | Parameter error. | 115| 10200012 | The Uint8ClampedArray's constructor cannot be directly invoked. | 116 117**示例:** 118 119```ts 120// 从一个Iterable构造对象 121let set: Set<number> = new Set<number>([1, 2, 3]); 122let array: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(set); 123// Uint8ClampedArray [1, 2, 3] 124``` 125 126## constructor 127constructor(array: ArrayLike\<number> | ArrayBuffer) 128 129构造函数,以ArrayLike或ArkTS ArrayBuffer创建一个ArkTS Uint8ClampedArray对象。 130 131**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 132 133**系统能力:** SystemCapability.Utils.Lang 134 135**参数:** 136 137| 参数名 | 类型 | 必填 | 说明 | 138| ------- | ------ | ---- | ------------------------------------------------------------ | 139| array | ArrayLike\<number> \| ArrayBuffer | 是 | 用于构造ArkTS Uint8ClampedArray的对象。当参数类型是ArrayBuffer时buffer所占的字节数须是4的整数倍。 | 140 141**错误码:** 142 143以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 144 145| 错误码ID | 错误信息 | 146| -------- | ------------------------------------------------------- | 147| 401 | Parameter error. | 148| 10200012 | The Uint8ClampedArray's constructor cannot be directly invoked. | 149 150**示例:** 151 152```ts 153// 例1 从一个ArrayLike构造对象 154let arrayLike = [1, 3, 5]; 155let array: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(arrayLike); 156``` 157 158```ts 159// 例2 从一个ArrayBuffer构造对象 160let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12); 161let array: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(arrayBuffer); 162``` 163 164```ts 165// 例3 从另一ArkTS Uint8ClampedArray构造对象 166let arrayLike = [1, 3, 5]; 167// uint8ClampedArray1 [1, 3, 5] 168let uint8ClampedArray1: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(arrayLike); 169// uint8ClampedArray2 [1, 3, 5] 170let uint8ClampedArray2: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(uint8ClampedArray1); 171``` 172 173## constructor 174constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number) 175 176构造函数,以ArrayBuffer创建一个ArkTS Uint8ClampedArray对象。 177 178**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 179 180**系统能力:** SystemCapability.Utils.Lang 181 182**参数:** 183 184| 参数名 | 类型 | 必填 | 说明 | 185| ------- | ------ | ---- | ------------------------------------------ | 186| buffer | ArrayBuffer | 是 | 用于构造ArkTS Uint8ClampedArray的ArrayBuffer对象。buffer所占的字节数须是4的整数倍。| 187| byteOffset | number | 否 | 指定buffer的字节偏移,从0开始,默认为0。 | 188| length | number | 否 | 指定ArkTS Uint8ClampedArray的长度,默认为0。 | 189 190**错误码:** 191 192以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 193 194| 错误码ID | 错误信息 | 195| -------- | ------------------------------------------------------- | 196| 401 | Parameter error. | 197| 10200012 | The Uint8ClampedArray's constructor cannot be directly invoked. | 198 199**示例:** 200 201```ts 202let uint8ClampedArray: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5, 6]); 203console.info("byteLength: " + uint8ClampedArray.buffer.byteLength); // byteLength: 6 204// 从uint8ClampedArray对应buffer第1个字节开始,长度为5 205let uint8ClampedArray1: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(uint8ClampedArray.buffer, 1, 5); 206console.info("[" + uint8ClampedArray1 + "]"); // [2, 3, 4, 5, 6] 207``` 208 209## from 210static from(arrayLike: ArrayLike\<number>): Uint8ClampedArray 211 212从一个ArrayLike或者可迭代对象中创建一个ArkTS Uint8ClampedArray对象。 213 214**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 215 216**系统能力:** SystemCapability.Utils.Lang 217 218**参数:** 219 220| 参数名 | 类型 | 必填 | 说明 | 221| ------- | ------ | ---- | --------------------------------------------------- | 222| arrayLike | ArrayLike\<number> | 是 | 用于构造ArkTS Uint8ClampedArray的ArrayLike对象。 | 223 224**返回值:** 225 226| 类型 | 说明 | 227| ------------ | --------- | 228| Uint8ClampedArray | 新创建的ArkTS Uint8ClampedArray对象。| 229 230**错误码:** 231 232以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 233 234| 错误码ID | 错误信息 | 235| -------- | ------------------------------------------------------- | 236| 401 | Parameter error. | 237 238**示例:** 239```ts 240let arrayLike = [1, 3, 5]; 241let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from(arrayLike); // array [1, 3, 5] 242``` 243 244## from 245static from\<T>(arrayLike: ArrayLike\<T>, mapFn: TypedArrayFromMapFn\<T, number>): Uint8ClampedArray 246 247从一个ArrayLike中创建一个ArkTS Uint8ClampedArray对象。 248 249**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 250 251**系统能力:** SystemCapability.Utils.Lang 252 253**参数:** 254| 参数名 | 类型 | 必填 | 说明 | 255| ------- | ------ | ---- | ------------------------------------------| 256| arrayLike | ArrayLike\<T> | 是 | 用于构造ArrayLike对象。 | 257| mapFn | [TypedArrayFromMapFn](arkts-apis-arkts-collections-Types.md#typedarrayfrommapfn)\<T, number> | 是 | 映射函数。| 258 259**返回值:** 260 261| 类型 | 说明 | 262| ------------ | --------- | 263| Uint8ClampedArray | 新创建的ArkTS Uint8ClampedArray对象。| 264 265**错误码:** 266 267以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 268 269| 错误码ID | 错误信息 | 270| -------- | ------------------------------------------------------- | 271| 401 | Parameter error. | 272 273**示例:** 274 275```ts 276// 例1 从一个对象创建 277let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from<number>( 278 { length: 5 }, (v: Object, k: number) => k); 279// Uint8ClampedArray [0, 1, 2, 3, 4] 280``` 281 282```ts 283// 例2 从一个字符数组创建 284let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from<string>( 285 ["1", "3", "5"], (v: string, k: number) => parseInt(v)); 286// Uint8ClampedArray [1, 3, 5] 287``` 288 289```ts 290// 例3 从一个字符串创建 291let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from<string>( 292 "12345", (v: string, k: number) => parseInt(v)); 293// Uint8ClampedArray [1, 2, 3, 4, 5] 294``` 295 296## from 297static from(arrayLike: Iterable\<number>, mapFn?: TypedArrayFromMapFn\<number, number>): Uint8ClampedArray 298 299从一个可迭代对象中创建一个ArkTS Uint8ClampedArray对象。 300 301**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 302 303**系统能力:** SystemCapability.Utils.Lang 304 305**参数:** 306| 参数名 | 类型 | 必填 | 说明 | 307| ------- | ------ | ---- | -----------------------------------| 308| arrayLike | Iterable\<number> | 是 | 用于构造的可迭代对象。 | 309| mapFn | [TypedArrayFromMapFn](arkts-apis-arkts-collections-Types.md#typedarrayfrommapfn)\<number, number> | 否 | 映射函数。如果省略,则不对元素进行加工处理。| 310 311**错误码:** 312 313以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 314 315| 错误码ID | 错误信息 | 316| -------- | ------------------------------------------------------- | 317| 401 | Parameter error. | 318 319**返回值:** 320 321| 类型 | 说明 | 322| ------------ | --------- | 323| Uint8ClampedArray | 新创建的ArkTS Uint8ClampedArray对象。| 324 325**示例:** 326 327```ts 328// 例1 不指定映射函数 329let set: Set<number> = new Set<number>([1, 2, 3]); 330let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from(set); 331// Uint8ClampedArray [1, 2, 3] 332``` 333 334```ts 335// 例2 指定映射函数 336let set: Set<number> = new Set<number>([1, 2, 3]); 337let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from( 338 set, (v: number, k: number) => v + k); 339// Uint8ClampedArray [1, 3, 5] 340``` 341 342## of<sup>18+</sup> 343 344static of(...items: number[]): Uint8ClampedArray 345 346通过可变数量的参数创建一个新的ArkTS Uint8ClampedArray对象,参数个数可以是0个、1个或者多个。 347 348**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 349 350**系统能力:** SystemCapability.Utils.Lang 351 352**参数:** 353 354| 参数名 | 类型 | 必填 | 说明 | 355| --------- | ------------- | ---- | ------------------------------- | 356| items | number[] | 否 | 用于创建数组的元素,参数个数可以是0个、1个或者多个。 | 357 358**返回值:** 359 360| 类型 | 说明 | 361| --------- | ----------------------- | 362| Uint8ClampedArray | 新的ArkTS Uint8ClampedArray实例。 | 363 364**错误码:** 365 366以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 367 368| 错误码ID | 错误信息 | 369| -------- | -------------------------------- | 370| 401 | Parameter error: Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed. | 371 372**示例:** 373 374```ts 375let arr: collections.Uint8ClampedArray = collections.Uint8ClampedArray.of(1, 2, 3, 4); 376console.info(arr.toString()); // 预期输出:1,2,3,4 377``` 378 379## toString<sup>18+</sup> 380 381toString(): string 382 383ArkTS Uint8ClampedArray转换为字符串。 384 385**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 386 387**系统能力:** SystemCapability.Utils.Lang 388 389**返回值:** 390 391| 类型 | 说明 | 392| ---------- | ------------- | 393| string | 一个包含数组所有元素的字符串。 | 394 395**错误码:** 396 397以下错误码详细介绍请参考[语言基础类库错误码](errorcode-utils.md)。 398 399| 错误码ID | 错误信息 | 400| -------- | ------------------------------------ | 401| 10200011 | The toString method cannot be bound. | 402| 10200201 | Concurrent modification error. | 403 404**示例:** 405 406```ts 407let array = new collections.Uint8ClampedArray([1, 2, 3, 4, 5]); 408let stringArray = array.toString(); 409console.info(stringArray); // 预期输出:1,2,3,4,5 410``` 411 412## toLocaleString<sup>18+</sup> 413 414toLocaleString(): string 415 416根据当前应用的系统地区获取符合当前文化习惯的数字表示形式,让每个元素调用自己的toLocaleString方法把数字转换为字符串,然后使用逗号将每个元素的结果字符串按照顺序拼接成字符串。 417 418**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 419 420**系统能力:** SystemCapability.Utils.Lang 421 422**返回值:** 423 424| 类型 | 说明 | 425| ---------- | ------------- | 426| string | 一个包含数组所有元素的字符串。 | 427 428**错误码:** 429 430以下错误码详细介绍请参考[语言基础类库错误码](errorcode-utils.md)。 431 432| 错误码ID | 错误信息 | 433| -------- | ------------------------------------------ | 434| 10200011 | The toLocaleString method cannot be bound. | 435| 10200201 | Concurrent modification error. | 436 437**示例:** 438 439```ts 440// 当前应用所在系统为法国地区 441let array = new collections.Uint8ClampedArray([100, 110, 120]); 442let stringArray = array.toLocaleString(); 443console.info(stringArray); // 预期输出:100,110,120 444``` 445 446## copyWithin 447copyWithin(target: number, start: number, end?: number): Uint8ClampedArray 448 449从ArkTS Uint8ClampedArray指定范围内的元素依次拷贝到目标位置。 450 451**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 452 453**系统能力:** SystemCapability.Utils.Lang 454 455**参数:** 456 457| 参数名 | 类型 | 必填 | 说明 | 458| ------- | ------ | ---- | ------------------------------------------------------------ | 459| target | number | 是 | 目标起始位置的下标,如果`target < 0`,则会从`target + array.length`位置开始。 | 460| start | number | 是 | 源起始位置下标,如果`start < 0`,则会从`start + Uint8ClampedArray.length`位置开始。 | 461| end | number | 否 | 源终止位置下标(不包含end位置的元素),如果`end < 0`,则会从`end + Uint8ClampedArray.length`位置终止。默认为ArkTS Uint8ClampedArray的长度。| 462 463**返回值:** 464 465| 类型 | 说明 | 466| ------------ | --------- | 467| Uint8ClampedArray | 修改后的Uint8ClampedArray。 | 468 469**错误码:** 470 471以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 472 473| 错误码ID | 错误信息 | 474| -------- | ------------------------------------------------ | 475| 401 | Parameter error. | 476| 10200011 | The copyWithin method cannot be bound. | 477| 10200201 | Concurrent modification error. | 478 479**示例:** 480 481```ts 482let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5, 6, 7, 8]); 483let copied: collections.Uint8ClampedArray = array.copyWithin(3, 1, 3); 484// Uint8ClampedArray [1, 2, 3, 2, 3, 6, 7, 8] 485``` 486 487## some 488some(predicate: TypedArrayPredicateFn\<number, Uint8ClampedArray>): boolean 489 490测试ArkTS Uint8ClampedArray中的是否存在元素满足指定条件。 491 492**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 493 494**系统能力:** SystemCapability.Utils.Lang 495 496**参数:** 497 498| 参数名 | 类型 | 必填 | 说明 | 499| ------- | ------ | ---- | ---------------------------------------------------- | 500| predicate | [TypedArrayPredicateFn](arkts-apis-arkts-collections-Types.md#typedarraypredicatefn)\<number, Uint8ClampedArray> | 是 | 用于测试的断言函数。| 501 502**返回值:** 503 504| 类型 | 说明 | 505| ------------ | --------- | 506| boolean | 如果存在元素满足指定条件返回true,否则返回false。| 507 508**错误码:** 509 510以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 511 512| 错误码ID | 错误信息 | 513| -------- | ---------------------------------- | 514| 401 | Parameter error. | 515| 10200011 | The some method cannot be bound. | 516| 10200201 | Concurrent modification error. | 517 518**示例:** 519 520```ts 521let arrayLike = [10, 20, 30, 40, 50]; 522let uint8ClampedArray: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(arrayLike); 523uint8ClampedArray.some((element: number) => element < 1); // false 524``` 525 526## every 527every(predicate: TypedArrayPredicateFn\<number, Uint8ClampedArray>): boolean 528 529测试ArkTS Uint8ClampedArray中的所有元素是否满足指定条件。 530 531**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 532 533**系统能力:** SystemCapability.Utils.Lang 534 535**参数:** 536 537| 参数名 | 类型 | 必填 | 说明 | 538| ------- | ------ | ---- | ----------------------------------------------------- | 539| predicate | [TypedArrayPredicateFn](arkts-apis-arkts-collections-Types.md#typedarraypredicatefn)\<number, Uint8ClampedArray> | 是 | 用于测试的断言函数。| 540 541**返回值:** 542 543| 类型 | 说明 | 544| ------------ | --------- | 545| boolean | 如果所有元素都满足指定条件则返回true,否则返回false。| 546 547**错误码:** 548 549以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 550 551| 错误码ID | 错误信息 | 552| -------- | ------------------------------------------------- | 553| 401 | Parameter error. | 554| 10200011 | The every method cannot be bound. | 555| 10200201 | Concurrent modification error. | 556 557**示例:** 558 559```ts 560let arrayLike = [10, 20, 30, 40, 50]; 561let uint8ClampedArray: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(arrayLike); 562uint8ClampedArray.every((element: number) => element > 10); // false 563``` 564 565## fill 566fill(value: number, start?: number, end?: number): Uint8ClampedArray 567 568使用特定值填充ArkTS Uint8ClampedArray指定范围的全部元素。 569 570**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 571 572**系统能力:** SystemCapability.Utils.Lang 573 574**参数:** 575 576| 参数名 | 类型 | 必填 | 说明 | 577| ------- | ------ | ---- | --------------------------------------------------------| 578| value | number | 是 | 待填充的值。| 579| start | number | 否 | 开始填充的索引,如果`start < 0`,则会从`start + Uint8ClampedArray.length`位置开始。默认值为0。| 580| end | number | 否 | 结束填充的索引(不包括该元素),如果`end < 0`,则会到`end + Uint8ClampedArray.length`位置结束。默认为ArkTS Uint8ClampedArray的长度。| 581 582**返回值:** 583 584| 类型 | 说明 | 585| ------------ | --------- | 586| Uint8ClampedArray | 填充后的Uint8ClampedArray。| 587 588**错误码:** 589 590以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 591 592| 错误码ID | 错误信息 | 593| -------- | ------------------------------------------------- | 594| 401 | Parameter error. | 595| 10200011 | The fill method cannot be bound. | 596| 10200201 | Concurrent modification error. | 597 598**示例:** 599 600```ts 601let arrayLike = [1, 2, 3]; 602new collections.Uint8ClampedArray(arrayLike).fill(4); // Uint8ClampedArray [4, 4, 4] 603new collections.Uint8ClampedArray(arrayLike).fill(4, 1); // Uint8ClampedArray [1, 4, 4] 604new collections.Uint8ClampedArray(arrayLike).fill(4, 1, 2); // Uint8ClampedArray [1, 4, 3] 605``` 606 607## filter 608filter(predicate: TypedArrayPredicateFn\<number, Uint8ClampedArray>): Uint8ClampedArray 609 610返回一个新ArkTS Uint8ClampedArray,其包含满足指定条件的所有元素。 611 612**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 613 614**系统能力:** SystemCapability.Utils.Lang 615 616**参数:** 617 618| 参数名 | 类型 | 必填 | 说明 | 619| ------- | ------ | ---- | ------------------------------------------------------ | 620| predicate | [TypedArrayPredicateFn](arkts-apis-arkts-collections-Types.md#typedarraypredicatefn)\<number, Uint8ClampedArray> | 是 | 用于元素过滤的断言函数。 | 621 622**返回值:** 623 624| 类型 | 说明 | 625| ------------ | --------- | 626| Uint8ClampedArray| 过滤后的ArkTS Uint8ClampedArray对象。| 627 628**错误码:** 629 630以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 631 632| 错误码ID | 错误信息 | 633| -------- | ------------------------------------------------- | 634| 401 | Parameter error. | 635| 10200011 | The filter method cannot be bound. | 636| 10200201 | Concurrent modification error. | 637 638**示例:** 639 640```ts 641let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([0, 1, 2, 3, 4]); 642let filtered: collections.Uint8ClampedArray = array.filter((element: number) => element % 2 == 0); 643// Uint8ClampedArray [0, 2, 4] 644``` 645 646## find 647find(predicate: TypedArrayPredicateFn\<number, Uint8ClampedArray>): number | undefined 648 649返回ArkTS Uint8ClampedArray中第一个满足指定条件的元素的值,如果所有元素都不满足,则返回undefined。 650 651**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 652 653**系统能力:** SystemCapability.Utils.Lang 654 655**参数:** 656 657| 参数名 | 类型 | 必填 | 说明 | 658| ------- | ------ | ---- | ------------------------------------------------------------ | 659| predicate | [TypedArrayPredicateFn](arkts-apis-arkts-collections-Types.md#typedarraypredicatefn)\<number, Uint8ClampedArray> | 是 | 用于元素查找的断言函数。| 660 661**返回值:** 662 663| 类型 | 说明 | 664| ------------ | --------- | 665| number \| undefined | 第一个满足条件的元素的值;如果所有元素都不满足条件,则返回undefined。| 666 667**错误码:** 668 669以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 670 671| 错误码ID | 错误信息 | 672| -------- | ------------------------------------------------- | 673| 401 | Parameter error. | 674| 10200011 | The find method cannot be bound. | 675| 10200201 | Concurrent modification error. | 676 677**示例:** 678 679```ts 680let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([0, 1, 2, 3, 4]); 681array.find((element: number) => element > 2); // 3 682array.find((element: number) => element > 4); // undefined 683``` 684 685## findIndex 686findIndex(predicate: TypedArrayPredicateFn\<number, Uint8ClampedArray>): number 687 688返回ArkTS Uint8ClampedArray中第一个满足指定条件的元素索引,如果所有元素都不满足,则返回-1。 689 690**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 691 692**系统能力:** SystemCapability.Utils.Lang 693 694**参数:** 695 696| 参数名 | 类型 | 必填 | 说明 | 697| ------- | ------ | ---- | ------------------------------------------------------------ | 698| predicate | [TypedArrayPredicateFn](arkts-apis-arkts-collections-Types.md#typedarraypredicatefn)\<number, Uint8ClampedArray> | 是 | 用于元素查找的断言函数。| 699 700**返回值:** 701 702| 类型 | 说明 | 703| ------------ | --------- | 704| number | 第一个满足条件的元素索引;如果所有元素都不满足条件,则返回-1。| 705 706**错误码:** 707 708以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 709 710| 错误码ID | 错误信息 | 711| -------- | ------------------------------------------------- | 712| 401 | Parameter error. | 713| 10200011 | The findIndex method cannot be bound. | 714| 10200201 | Concurrent modification error. | 715 716**示例:** 717 718```ts 719const array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 720let foundIndex: number = array.findIndex((element: number) => element % 2 === 0); // 1 721``` 722 723## forEach 724forEach(callbackFn: TypedArrayForEachCallback\<number, Uint8ClampedArray>): void 725 726对ArkTS Uint8ClampedArray中的每个元素执行提供的回调函数。 727 728**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 729 730**系统能力:** SystemCapability.Utils.Lang 731 732**参数:** 733 734| 参数名 | 类型 | 必填 | 说明 | 735| ------- | ------ | ---- | ------------------------------------------------------------ | 736| callbackFn | [TypedArrayForEachCallback](arkts-apis-arkts-collections-Types.md#typedarrayforeachcallback)\<number, Uint8ClampedArray> | 是 | 用于对每个元素执行的回调函数。| 737 738 739**错误码:** 740 741以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 742 743| 错误码ID | 错误信息 | 744| -------- | ------------------------------------------------- | 745| 401 | Parameter error. | 746| 10200011 | The forEach method cannot be bound. | 747| 10200201 | Concurrent modification error. | 748 749**示例:** 750 751```ts 752let uint8ClampedArray: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3]); 753uint8ClampedArray.forEach((value: number, index: number, array: collections.Uint8ClampedArray) => { 754 console.info(`Element ${value} at index ${index}`); 755}); 756``` 757 758## indexOf 759indexOf(searchElement: number, fromIndex?: number): number 760 761返回在ArkTS Uint8ClampedArray中给定元素的第一个索引,如果不存在,则返回-1。 762 763**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 764 765**系统能力:** SystemCapability.Utils.Lang 766 767**参数:** 768 769| 参数名 | 类型 | 必填 | 说明 | 770| ------------- | ------ | ---- | ---------------------------| 771| searchElement | number | 是 | 待索引的值。 | 772| fromIndex | number | 否 | 搜索的起始下标。默认值为0。如果下标大于等于ArkTS Uint8ClampedArray的长度,则返回-1。如果提供的下标值是负数,则被当做距离数组尾部的偏移,从前到后搜索。 | 773 774**返回值:** 775 776| 类型 | 说明 | 777| ------------ | --------- | 778| number | 数组中元素的第一个索引;没有找到,则返回-1。 | 779 780**错误码:** 781 782以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 783 784| 错误码ID | 错误信息 | 785| -------- | ------------------------------------------------- | 786| 401 | Parameter error. | 787| 10200011 | The indexOf method cannot be bound. | 788| 10200201 | Concurrent modification error. | 789 790**示例:** 791 792```ts 793let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([3, 5, 9]); 794array.indexOf(3); // 0 795array.indexOf(7); // -1 796array.indexOf(9, 2); // 2 797array.indexOf(9, -2); // 2 798``` 799 800## lastIndexOf<sup>18+</sup> 801 802lastIndexOf(searchElement: number, fromIndex?: number): number 803 804返回ArkTS Uint8ClampedArray实例中最后一次出现searchElement的索引,如果对象不包含,则为-1。 805 806**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 807 808**系统能力:** SystemCapability.Utils.Lang 809 810**参数:** 811 812| 参数名 | 类型 | 必填 | 说明 | 813| ------------- | ------ | --- | --------------------------------------------------------------------------------- | 814| searchElement | number | 是 | 待索引的值。 | 815| fromIndex | number | 否 | 搜索的起始下标。默认值为0。如果下标大于等于ArkTS Uint8ClampedArray的长度,则返回-1。如果提供的下标值是负数,则被当做距离数组尾部的偏移,从后到前搜索。 | 816 817**返回值:** 818 819| 类型 | 说明 | 820| ------ | ----------------------- | 821| number | 数组中给定元素的最后一个索引;没有找到,则返回-1。 | 822 823**错误码:** 824 825以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 826 827| 错误码ID | 错误信息 | 828| -------- | --------------------------------------- | 829| 10200011 | The lastIndexOf method cannot be bound. | 830| 10200201 | Concurrent modification error. | 831 832**示例:** 833 834```ts 835let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([3, 5, 9]); 836console.info(array.lastIndexOf(3) + ''); // 预期输出:0 837console.info(array.lastIndexOf(7) + ''); // 预期输出:-1 838console.info(array.lastIndexOf(9, 2) + ''); // 预期输出:2 839console.info(array.lastIndexOf(9, -2) + ''); // 预期输出:-1 840``` 841 842## join 843join(separator?: string): string 844 845将ArkTS Uint8ClampedArray的所有元素拼接成一个字符串,元素之间使用指定的分隔符分隔。 846 847**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 848 849**系统能力:** SystemCapability.Utils.Lang 850 851**参数:** 852 853| 参数名 | 类型 | 必填 | 说明 | 854| --------- | ------ | ---- | ---------------------------------------------------- | 855| separator | string | 否 | 分隔字符串。如果省略,则使用逗号分隔。 | 856 857**返回值:** 858 859| 类型 | 说明 | 860| ------------ | --------- | 861| string | 包含所有元素拼接成的字符串。如果ArkTS Uint8ClampedArray为空,则返回空字符串。| 862 863**错误码:** 864 865以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 866 867| 错误码ID | 错误信息 | 868| -------- | ------------------------------------------------- | 869| 401 | Parameter error. | 870| 10200011 | The join method cannot be bound. | 871| 10200201 | Concurrent modification error. | 872 873**示例:** 874 875```ts 876let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 877let joined: string = array.join('-'); // "1-2-3-4-5" 878``` 879 880## map 881map(callbackFn: TypedArrayMapCallback\<number, Uint8ClampedArray>): Uint8ClampedArray 882 883对ArkTS Uint8ClampedArray中的每个元素应用指定的回调函数,并使用结果创建一个新的ArkTS Uint8ClampedArray对象。 884 885**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 886 887**系统能力:** SystemCapability.Utils.Lang 888 889**参数:** 890| 参数名 | 类型 | 必填 | 说明 | 891| --------- | ------ | ---- | ---------------------------------------------------- | 892| callbackFn | [TypedArrayMapCallback](arkts-apis-arkts-collections-Types.md#typedarraymapcallback)\<number, Uint8ClampedArray> | 是 | 回调函数。 | 893 894 895**返回值:** 896 897| 类型 | 说明 | 898| ------------ | --------- | 899| Uint8ClampedArray | 新ArkTS Uint8ClampedArray对象。| 900 901**错误码:** 902 903以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 904 905| 错误码ID | 错误信息 | 906| -------- | ------------------------------------------------- | 907| 401 | Parameter error. | 908| 10200011 | The map method cannot be bound. | 909| 10200201 | Concurrent modification error. | 910 911**示例:** 912 913```ts 914let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([25, 36, 49]); 915const mapped: collections.Uint8ClampedArray = array.map(Math.sqrt); // Uint8ClampedArray [5, 6 ,7] 916``` 917 918## reduce 919reduce(callbackFn: TypedArrayReduceCallback\<number, number, Uint8ClampedArray>): number 920 921对ArkTS Uint8ClampedArray中的每个元素执行归约函数,并返回最终的归约结果。 922 923**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 924 925**系统能力:** SystemCapability.Utils.Lang 926 927**参数:** 928| 参数名 | 类型 | 必填 | 说明 | 929| ---------- | ---------------------- | ---- | ------------------------------------------------------------ | 930| callbackFn | [TypedArrayReduceCallback](arkts-apis-arkts-collections-Types.md#typedarrayreducecallback)\<number, number, Uint8ClampedArray> | 是 | 归约函数。 | 931 932**返回值:** 933 934| 类型 | 说明 | 935| ------------ | --------- | 936| number | 由归约函数返回的结果。| 937 938**错误码:** 939 940以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 941 942| 错误码ID | 错误信息 | 943| -------- | ------------------------------------------------ | 944| 401 | Parameter error. | 945| 10200011 | The reduce method cannot be bound. | 946| 10200201 | Concurrent modification error. | 947 948**示例:** 949 950```ts 951let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 952let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value); 953// reducedValue == 15 954``` 955 956## reduceRight<sup>18+</sup> 957 958reduceRight(callbackFn: TypedArrayReduceCallback\<number, number, Uint8ClampedArray>): number 959 960反向遍历ArkTS Uint8ClampedArray,对ArkTS Uint8ClampedArray中的每个元素执行归约函数,并返回最终的归约结果。 961 962**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 963 964**系统能力:** SystemCapability.Utils.Lang 965 966**参数:** 967| 参数名 | 类型 | 必填 | 说明 | 968| ---------- | ---------------------- | ---- | ------------------------------------------------------------ | 969| callbackFn | [TypedArrayReduceCallback](arkts-apis-arkts-collections-Types.md#typedarrayreducecallback)\<number, number, Uint8ClampedArray> | 是 | 归约函数。 | 970 971**返回值:** 972 973| 类型 | 说明 | 974| ------ | ----------- | 975| number | 由归约函数返回的结果。 | 976 977**错误码:** 978 979以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 980 981| 错误码ID | 错误信息 | 982| -------- | --------------------------------------- | 983| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 984| 10200011 | The reduceRight method cannot be bound. | 985| 10200201 | Concurrent modification error. | 986 987**示例:** 988 989```ts 990let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 991let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value); 992console.info(reducedValue + ''); // 预期输出: 15 993``` 994 995## reduce 996reduce\<U = number>(callbackFn: TypedArrayReduceCallback\<U, number, Uint8ClampedArray>, initialValue: U): U 997 998对ArkTS Uint8ClampedArray中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。 999 1000**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1001 1002**系统能力:** SystemCapability.Utils.Lang 1003 1004**参数:** 1005| 参数名 | 类型 | 必填 | 说明 | 1006| --------- | ------ | ---- | --------------------------------------------------- | 1007| callbackFn | [TypedArrayReduceCallback](arkts-apis-arkts-collections-Types.md#typedarrayreducecallback)\<U, number, Uint8ClampedArray> | 是 | 归约函数。 | 1008| initialValue | U | 是 | 初始值。 | 1009 1010 1011**返回值:** 1012 1013| 类型 | 说明 | 1014| ------------ | --------- | 1015| U | 由归约函数返回的结果。 | 1016 1017**错误码:** 1018 1019以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1020 1021| 错误码ID | 错误信息 | 1022| -------- | ------------------------------------------------- | 1023| 401 | Parameter error. | 1024| 10200011 | The reduce method cannot be bound. | 1025| 10200201 | Concurrent modification error. | 1026 1027**示例:** 1028 1029```ts 1030let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1031let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value, 1); 1032// reducedValue == 16 1033``` 1034 1035## reduceRight<sup>18+</sup> 1036 1037reduceRight\<U = number>(callbackFn: TypedArrayReduceCallback\<U, number, Uint8ClampedArray>, initialValue: U): U 1038 1039反向遍历ArkTS Uint8ClampedArray,对ArkTS Uint8ClampedArray中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。 1040 1041**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 1042 1043**系统能力:** SystemCapability.Utils.Lang 1044 1045**参数:** 1046| 参数名 | 类型 | 必填 | 说明 | 1047| --------- | ------ | ---- | --------------------------------------------------- | 1048| callbackFn | [TypedArrayReduceCallback](arkts-apis-arkts-collections-Types.md#typedarrayreducecallback)\<U, number, Uint8ClampedArray> | 是 | 归约函数。 | 1049| initialValue | U | 是 | 初始值。 | 1050 1051**返回值:** 1052 1053| 类型 | 说明 | 1054| ------ | ----------- | 1055| U | 由归约函数返回的结果。 | 1056 1057**错误码:** 1058 1059以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1060 1061| 错误码ID | 错误信息 | 1062| -------- | --------------------------------------- | 1063| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1064| 10200011 | The reduceRight method cannot be bound. | 1065| 10200201 | Concurrent modification error. | 1066 1067**示例:** 1068 1069```ts 1070let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1071let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value, 1); 1072console.info(reducedValue + ''); // 预期输出: 16 1073``` 1074 1075## reverse 1076reverse(): Uint8ClampedArray 1077 1078反转ArkTS Uint8ClampedArray。 1079 1080**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1081 1082**系统能力:** SystemCapability.Utils.Lang 1083 1084**返回值:** 1085 1086| 类型 | 说明 | 1087| ------------ | --------- | 1088| Uint8ClampedArray | 反转后的ArkTS Uint8ClampedArray对象。| 1089 1090**错误码:** 1091 1092以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1093 1094| 错误码ID | 错误信息 | 1095| -------- | ------------------------------------------------- | 1096| 10200011 | The reverse method cannot be bound. | 1097| 10200201 | Concurrent modification error. | 1098 1099**示例:** 1100 1101```ts 1102let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1103let reversed: collections.Uint8ClampedArray = array.reverse(); // Uint8ClampedArray [5, 4, 3, 2, 1] 1104``` 1105 1106## set 1107set(array: ArrayLike\<number>, offset?: number): void 1108 1109将传入的ArrayLike元素依次写入到指定的起始位置。 1110 1111**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1112 1113**系统能力:** SystemCapability.Utils.Lang 1114 1115**参数:** 1116| 参数名 | 类型 | 必填 | 说明 | 1117| --------- | ------ | ---- | ---------------------------------------------------- | 1118| array | ArrayLike\<number> | 是 | 用于设置的ArrayLike对象。| 1119| offset | number | 否 | 写入的起始位置。默认为0。| 1120 1121**错误码:** 1122 1123以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1124 1125| 错误码ID | 错误信息 | 1126| -------- | ------------------------------------------------- | 1127| 401 | Parameter error. | 1128| 10200011 | The set method cannot be bound. | 1129| 10200201 | Concurrent modification error. | 1130 1131**示例:** 1132 1133```ts 1134let buffer: collections.ArrayBuffer = new collections.ArrayBuffer(8); 1135let array: collections.Uint8ClampedArray = new collections.Uint8ClampedArray(buffer); 1136array.set([1, 2, 3], 3); // Uint8ClampedArray [0, 0, 0, 1, 2, 3, 0, 0] 1137``` 1138 1139## slice 1140slice(start?: number, end?: number): Uint8ClampedArray 1141 1142返回一个新的ArkTS Uint8ClampedArray对象,其包含原ArkTS Uint8ClampedArray指定范围的内容。 1143 1144**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1145 1146**系统能力:** SystemCapability.Utils.Lang 1147 1148**参数:** 1149 1150| 参数名 | 类型 | 必填 | 说明 | 1151| ------ | ------ | ---- | -----------------------------------------------------| 1152| start | number | 否 | 开始索引,如果`start < 0`,则会从`start + Uint8ClampedArray.length`位置开始。默认为0。 | 1153| end | number | 否 | 结束索引(不包括该元素),如果`end < 0`,则会到`end + Uint8ClampedArray.length`位置结束。默认为ArkTS Uint8ClampedArray的长度。| 1154 1155**返回值:** 1156 1157| 类型 | 说明 | 1158| ------------ | --------- | 1159| Uint8ClampedArray | 新的ArkTS Uint8ClampedArray对象。 | 1160 1161**错误码:** 1162 1163以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1164 1165| 错误码ID | 错误信息 | 1166| -------- | ------------------------------------------------- | 1167| 401 | Parameter error. | 1168| 10200011 | The slice method cannot be bound. | 1169| 10200201 | Concurrent modification error. | 1170 1171**示例:** 1172 1173```ts 1174let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1175array.slice(); // Uint8ClampedArray [1, 2, 3, 4, 5] 1176array.slice(1, 3); // Uint8ClampedArray [2, 3] 1177array.slice(-2); // Uint8ClampedArray [4, 5] 1178``` 1179 1180## sort 1181sort(compareFn?: TypedArrayCompareFn\<number>): Uint8ClampedArray 1182 1183对ArkTS Uint8ClampedArray进行排序,并返回排序后的ArkTS Uint8ClampedArray对象。 1184 1185**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1186 1187**系统能力:** SystemCapability.Utils.Lang 1188 1189**参数:** 1190 1191| 参数名 | 类型 | 必填 | 说明 | 1192| --------- | ---------------------- | ---- | ------------------------------------------ | 1193| compareFn | [TypedArrayCompareFn](arkts-apis-arkts-collections-Types.md#typedarraycomparefn)\<number> | 否 | 用于确定元素顺序的函数。默认使用升序排序。 | 1194 1195**返回值:** 1196 1197| 类型 | 说明 | 1198| ------------ | --------- | 1199| Uint8ClampedArray | 排序后的ArkTS Uint8ClampedArray对象。| 1200 1201**错误码:** 1202 1203以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1204 1205| 错误码ID | 错误信息 | 1206| -------- | ------------------------------------------ | 1207| 401 | Parameter error. | 1208| 10200011 | The sort method cannot be bound. | 1209| 10200201 | Concurrent modification error. | 1210 1211**示例:** 1212 1213```ts 1214let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 3, 5, 4, 2]); 1215array.sort(); // Uint8ClampedArray [1, 2, 3, 4, 5] 1216array.sort((a: number, b: number) => a - b); // Uint8ClampedArray [1, 2, 3, 4, 5] 1217array.sort((a: number, b: number) => b - a); // Uint8ClampedArray [5, 4, 3, 2, 1] 1218``` 1219 1220## subarray 1221subarray(begin?: number, end?: number): Uint8ClampedArray 1222 1223从指定的位置截取数组,返回一个新的、基于相同ArkTS ArrayBuffer的ArkTS Uint8ClampedArray对象。 1224 1225**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1226 1227**系统能力:** SystemCapability.Utils.Lang 1228 1229**参数:** 1230 1231| 参数名 | 类型 | 必填 | 说明 | 1232| ------ | ------ | ---- | ------------------------------------------------- | 1233| begin | number | 否 | 开始索引,如果`begin < 0`,则会从`begin + Uint8ClampedArray.length`位置开始。默认值为0。 | 1234| end | number | 否 | 结束索引(不包括该元素),如果`end < 0`,则会到`end + Uint8ClampedArray.length`位置结束。默认为ArkTS Uint8ClampedArray的长度。 | 1235 1236**返回值:** 1237 1238| 类型 | 说明 | 1239| ------------ | --------- | 1240| Uint8ClampedArray | 新的ArkTS Uint8ClampedArray对象。| 1241 1242**错误码:** 1243 1244以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1245 1246| 错误码ID | 错误信息 | 1247| -------- | -------------------------------------------------| 1248| 401 | Parameter error. | 1249| 10200011 | The subarray method cannot be bound. | 1250| 10200201 | Concurrent modification error. | 1251 1252**示例:** 1253 1254```ts 1255let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1256let subArray: collections.Uint8ClampedArray = array.subarray(); // Uint8ClampedArray [1, 2, 3, 4, 5] 1257subArray.set([10, 20, 30]); // Uint8ClampedArray [10, 20, 30, 4, 5] 1258``` 1259 1260## at 1261at(index: number): number | undefined 1262 1263返回指定下标的元素,如果不存在,则返回undefined。 1264 1265**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1266 1267**系统能力:** SystemCapability.Utils.Lang 1268 1269**参数:** 1270| 参数名 | 类型 | 必填 | 说明 | 1271| ------ | ------ | ---- | ------------------------------------------------------------ | 1272| index | number | 是 | 要返回的Array元素的索引(从零开始),取值为整数。如果`index < 0`,则会访问`index + Uint8ClampedArray.length`位置的元素。| 1273 1274**返回值:** 1275 1276| 类型 | 说明 | 1277| ------------ | --------- | 1278| number \| undefined| 指定下标的元素;如果不存在,则返回undefined。| 1279 1280**错误码:** 1281 1282以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1283 1284| 错误码ID | 错误信息 | 1285| -------- | ------------------------------------------------ | 1286| 401 | Parameter error. | 1287| 10200011 | The at method cannot be bound. | 1288| 10200201 | Concurrent modification error. | 1289 1290**示例:** 1291 1292```ts 1293let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1294console.info("element: " + array.at(2)); // element: 3 1295console.info("element: " + array.at(-1)); // element: 5 1296console.info("element: " + array.at(6)); // element: undefined 1297``` 1298 1299## includes 1300includes(searchElement: number, fromIndex?: number): boolean 1301 1302判断ArkTS Uint8ClampedArray是否包含特定元素。 1303 1304**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1305 1306**系统能力:** SystemCapability.Utils.Lang 1307 1308**参数:** 1309| 参数名 | 类型 | 必填 | 说明 | 1310| ------ | ------ | ---- | --------------------------------------- | 1311| searchElement | number | 是 | 待搜索的元素。 | 1312| fromIndex | number | 否 | 开始搜索的索引,如果`fromIndex < 0`,则会从`fromIndex + Uint8ClampedArray.length`位置开始。默认值为0。| 1313 1314**返回值:** 1315 1316| 类型 | 说明 | 1317| ------- | ---------------------------------------------------------- | 1318| boolean | 如果ArkTS Uint8ClampedArray包含指定的元素,则返回true;否则返回false。| 1319 1320 1321**错误码:** 1322 1323以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。 1324 1325| 错误码ID | 错误信息 | 1326| -------- | ------------------------------------------------- | 1327| 401 | Parameter error. | 1328| 10200011 | The includes method cannot be bound. | 1329| 10200201 | Concurrent modification error. | 1330 1331**示例:** 1332 1333```ts 1334let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3]); 1335console.info("includes: " + array.includes(2)); // includes: true 1336console.info("includes: " + array.includes(4)); // includes: false 1337console.info("includes: " + array.includes(3, 3)); // includes: false 1338``` 1339 1340## entries 1341entries(): IterableIterator\<[number, number]> 1342 1343返回一个新的迭代器对象,该对象包含ArkTS Uint8ClampedArray中每个元素的键值对。 1344 1345**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1346 1347**系统能力:** SystemCapability.Utils.Lang 1348 1349**返回值:** 1350 1351| 类型 | 说明 | 1352| ------------ | --------- | 1353| IterableIterator\<[number, number]>| 新的迭代器对象。 | 1354 1355**错误码:** 1356 1357以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1358 1359| 错误码ID | 错误信息 | 1360| -------- | ------------------------------------------------- | 1361| 10200011 | The entries method cannot be bound. | 1362| 10200201 | Concurrent modification error. | 1363 1364**示例:** 1365 1366```ts 1367let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([11, 22, 33]); 1368let iterator: IterableIterator<[number, number]> = array.entries(); 1369console.info("value: " + iterator.next().value); // value: 0,11 1370console.info("value: " + iterator.next().value); // value: 1,22 1371console.info("value: " + iterator.next().value); // value: 2,33 1372``` 1373 1374## keys 1375keys(): IterableIterator\<number> 1376 1377返回一个新的迭代器对象,该对象包含ArkTS Uint8ClampedArray中每个元素的键(下标)。 1378 1379**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1380 1381**系统能力:** SystemCapability.Utils.Lang 1382 1383**返回值:** 1384 1385| 类型 | 说明 | 1386| ------------ | --------- | 1387| IterableIterator\<number> | 新的迭代器对象。| 1388 1389**错误码:** 1390 1391以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1392 1393| 错误码ID | 错误信息 | 1394| -------- | ------------------------------------------------- | 1395| 10200011 | The keys method cannot be bound. | 1396| 10200201 | Concurrent modification error. | 1397 1398**示例:** 1399 1400```ts 1401let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1402let iterator: IterableIterator<number> = array.keys(); 1403for (const key of iterator) { 1404 console.info("" + key); // 依次输出 0,1,2,3,4 1405} 1406``` 1407 1408## values 1409values(): IterableIterator\<number> 1410 1411返回一个新的迭代器对象,该对象包含ArkTS Uint8ClampedArray中每个元素的值。 1412 1413**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1414 1415**系统能力:** SystemCapability.Utils.Lang 1416 1417**返回值:** 1418 1419| 类型 | 说明 | 1420| ------------ | --------- | 1421| IterableIterator\<number> | 新的迭代器对象。| 1422 1423**错误码:** 1424 1425以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1426 1427| 错误码ID | 错误信息 | 1428| -------- | ------------------------------------------------- | 1429| 10200011 | The values method cannot be bound. | 1430| 10200201 | Concurrent modification error. | 1431 1432**示例:** 1433 1434```ts 1435let array: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5]); 1436let iterator: IterableIterator<number> = array.values(); 1437for (const value of iterator) { 1438 console.info("" + value); // 依次输出 1,2,3,4,5 1439} 1440``` 1441 1442## [Symbol.iterator] 1443 1444[Symbol.iterator]\(): IterableIterator<number> 1445 1446返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。 1447 1448> **说明:** 1449> 1450> 本接口不支持在.ets文件中使用。 1451 1452**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1453 1454**系统能力:** SystemCapability.Utils.Lang 1455 1456**返回值:** 1457 1458| 类型 | 说明 | 1459| ------------------------- | ---------------- | 1460| IterableIterator<number> | 返回一个迭代器。 | 1461 1462**错误码:** 1463 1464以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。 1465 1466| 错误码ID | 错误信息 | 1467| -------- | ------------------------------------------- | 1468| 10200011 | The Symbol.iterator method cannot be bound. | 1469 1470**示例:** 1471 1472```ts 1473let uint8ClampedArray: collections.Uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 3, 4, 5, 6]); 1474 1475for (let item of uint8ClampedArray) { 1476 console.info(`value : ${item}`); 1477} 1478``` 1479 1480## [index: number] 1481 1482[index: number]: number 1483 1484返回Uint8ClampedArray指定索引位置的元素。 1485 1486**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1487 1488**系统能力:** SystemCapability.Utils.Lang 1489 1490**参数:** 1491 1492| 参数名 | 类型 | 必填 | 说明 | 1493| ----- | ------ | ---- | -------------------------- | 1494| index | number | 是 | 所需代码单元的从零开始的索引。| 1495 1496**返回值:** 1497 1498| 类型 | 说明 | 1499| ----- | ---------------------| 1500| number | 返回number数据类型。 | 1501 1502**示例:** 1503 1504```ts 1505let uint8ClampedArray = collections.Uint8ClampedArray.from([1, 2, 4]); 1506console.info("Element at index 1: ", uint8ClampedArray[1]); 1507``` 1508