一种线性数据结构,底层基于ArrayBuffer实现。
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
此模块仅支持在ArkTS文件(文件后缀为.ets)中导入使用。
装饰器类型:@Sendable
系统能力: SystemCapability.Utils.Lang
元服务API: 从API version 12开始,该接口支持在元服务中使用。
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| buffer | ArrayBuffer | 是 | 否 | ArkTS Uint16Array底层使用的ArrayBuffer对象。 |
| byteLength | number | 是 | 否 | ArkTS Uint16Array所占的字节数。 |
| byteOffset | number | 是 | 否 | ArkTS Uint16Array距离其ArrayBuffer起始位置的字节偏移。 |
| length | number | 是 | 否 | ArkTS Uint16Array元素个数。 |
| BYTES_PER_ELEMENT | number | 是 | 否 | ArkTS Uint16Array中每个元素所占的字节数。 |
constructor()
构造函数,用于创建一个空ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint16Array's constructor cannot be directly invoked. |
示例:
let uint16Array: collections.Uint16Array = new collections.Uint16Array();
constructor(length: number)
构造函数,用于创建一个指定长度的ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| length | number | 是 | 用于指定ArkTS Uint16Array的长度,需为非负整数。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint16Array's constructor cannot be directly invoked. |
示例:
// 以长度参数构造对象 let uint16Array: collections.Uint16Array = new collections.Uint16Array(12);
constructor(elements: Iterable<number>)
构造函数,以Iterable创建一个ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| elements | Iterable<number> | 是 | 可迭代数字集合,用于构造ArkTS Uint16Array对象,每个元素的取值范围为0到2^16-1,即0到65535。如果元素超出此范围,该元素会通过取模运算转换为0~65535范围内的值(即执行 value & 0xFFFF 操作)。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint16Array's constructor cannot be directly invoked. |
示例:
// 从一个Iterable构造对象 let set: Set<number> = new Set<number>([1, 2, 3]); let array: collections.Uint16Array = new collections.Uint16Array(set); // Uint16Array [1, 2, 3]
constructor(array: ArrayLike<number> | ArrayBuffer)
构造函数,以ArrayLike或ArkTS ArrayBuffer创建一个ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| array | ArrayLike<number> | ArrayBuffer | 是 | 用于构造ArkTS Uint16Array的对象。当参数类型是ArrayBuffer时buffer所占的字节数需是2的整数倍。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint16Array's constructor cannot be directly invoked. |
示例:
// 例1 从一个ArrayLike构造对象 let arrayLike = [1, 3, 5]; let array: collections.Uint16Array = new collections.Uint16Array(arrayLike);
// 例2 从一个ArrayBuffer构造对象 let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12); let array: collections.Uint16Array = new collections.Uint16Array(arrayBuffer);
// 例3 从另一ArkTS Uint16Array构造对象 let arrayLike = [1, 3, 5]; // uint16Array1 [1, 3, 5] let uint16Array1: collections.Uint16Array = new collections.Uint16Array(arrayLike); // uint16Array2 [1, 3, 5] let uint16Array2: collections.Uint16Array = new collections.Uint16Array(uint16Array1);
constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number)
构造函数,以ArrayBuffer创建一个ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| buffer | ArrayBuffer | 是 | 用于构造ArkTS Uint16Array的ArrayBuffer对象。buffer所占的字节数需是2的整数倍。 |
| byteOffset | number | 否 | 指定buffer的字节偏移,需为非负整数。默认值为0。 |
| length | number | 否 | 指定ArkTS Uint16Array的长度,需为非负整数。默认值为0,此时表示从byteOffset开始到buffer末尾的全部元素。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint16Array's constructor cannot be directly invoked. |
示例:
let uint16Array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5, 6]);
console.info(`byteLength: ${uint16Array.buffer.byteLength}`); // byteLength: 12
// 从uint16Array对应buffer第3个字节开始,长度为5
let uint16Array1: collections.Uint16Array = new collections.Uint16Array(uint16Array.buffer, 2, 5);
console.info(`[${uint16Array1}]`); // [2, 3, 4, 5, 6] static from(arrayLike: ArrayLike<number>): Uint16Array
从一个ArrayLike中创建一个ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| arrayLike | ArrayLike<number> | 是 | 用于构造ArkTS Uint16Array的ArrayLike对象。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 新创建的ArkTS Uint16Array对象。 |
示例:
let arrayLike = [1, 3, 5]; let array: collections.Uint16Array = collections.Uint16Array.from(arrayLike); // array [1, 3, 5]
static from<T>(arrayLike: ArrayLike<T>, mapFn: TypedArrayFromMapFn<T, number>): Uint16Array
从一个ArrayLike中创建一个ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| arrayLike | ArrayLike<T> | 是 | 用于构造ArkTS Uint16Array的ArrayLike对象。 |
| mapFn | TypedArrayFromMapFn<T, number> | 是 | 映射函数,将ArrayLike中类型为T的每个元素映射为number类型,映射结果用于创建ArkTS Uint16Array的对应元素。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 新创建的ArkTS Uint16Array对象。 |
示例:
// 例1 从一个对象创建
let array: collections.Uint16Array = collections.Uint16Array.from<number>(
{ length: 5 }, (v: Object, k: number) => k);
// Uint16Array [0, 1, 2, 3, 4] // 例2 从一个字符数组创建 let array: collections.Uint16Array = collections.Uint16Array.from<string>( ["1", "3", "5"], (v: string, k: number) => parseInt(v)); // Uint16Array [1, 3, 5]
// 例3 从一个字符串创建 let array: collections.Uint16Array = collections.Uint16Array.from<string>( "12345", (v: string, k: number) => parseInt(v)); // Uint16Array [1, 2, 3, 4, 5]
static from(arrayLike: Iterable<number>, mapFn?: TypedArrayFromMapFn<number, number>): Uint16Array
从一个可迭代对象中创建一个ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| arrayLike | Iterable<number> | 是 | 用于构造ArkTS Uint16Array的可迭代对象。 |
| mapFn | TypedArrayFromMapFn<number, number> | 否 | 映射函数。如果省略,则不对元素进行映射转换,直接使用原始元素值。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 新创建的ArkTS Uint16Array对象。 |
示例:
// 例1 不指定映射函数 let set: Set<number> = new Set<number>([1, 2, 3]); let array: collections.Uint16Array = collections.Uint16Array.from(set); // Uint16Array [1, 2, 3]
// 例2 指定映射函数 let set: Set<number> = new Set<number>([1, 2, 3]); let array: collections.Uint16Array = collections.Uint16Array.from( set, (v: number, k: number) => v + k); // Uint16Array [1, 3, 5]
static of(...items: number[]): Uint16Array
通过可变数量的参数创建一个新的ArkTS Uint16Array对象。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| items | number[] | 否 | 用于创建数组的元素,参数个数可以是0个、1个或者多个。默认值为空数组。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 新的ArkTS Uint16Array对象。 |
示例:
let arr: collections.Uint16Array = collections.Uint16Array.of(1, 2, 3, 4); console.info(arr.toString()); // 预期输出:1,2,3,4
toString(): string
ArkTS Uint16Array转换为字符串。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| string | 一个包含数组所有元素的字符串。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The toString method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array = new collections.Uint16Array([1, 2, 3, 4, 5]); let stringArray = array.toString(); console.info(stringArray); // 预期输出:1,2,3,4,5
toLocaleString(): string
根据当前应用的系统地区获取符合当前文化习惯的数字表示形式,对每个元素调用toLocaleString方法,把数字转换为字符串,然后使用逗号将每个元素的结果字符串按照顺序拼接成字符串。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| string | 一个包含数组所有元素的字符串。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The toLocaleString method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
// 当前应用所在系统为法国地区 let array = new collections.Uint16Array([1000, 2000, 3000]); let stringArray = array.toLocaleString(); console.info(stringArray); // 预期输出:1,000,2,000,3,000
copyWithin(target: number, start: number, end?: number): Uint16Array
从ArkTS Uint16Array指定范围内的元素依次拷贝到目标位置。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| target | number | 是 | 目标起始位置的下标,如果target < 0,则会从target + Uint16Array.length位置开始。 |
| start | number | 是 | 源起始位置下标,如果start < 0,则会从start + Uint16Array.length位置开始。 |
| end | number | 否 | 源终止位置下标(不包含end位置的元素),如果end < 0,则会到end + Uint16Array.length位置结束。默认为ArkTS Uint16Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 修改后的ArkTS Uint16Array。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The copyWithin method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5, 6, 7, 8]); let copied: collections.Uint16Array = array.copyWithin(3, 1, 3); // Uint16Array [1, 2, 3, 2, 3, 6, 7, 8]
some(predicate: TypedArrayPredicateFn<number, Uint16Array>): boolean
测试ArkTS Uint16Array中是否存在满足指定条件的元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint16Array> | 是 | 用于测试的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 如果存在元素满足指定条件返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The some method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let arrayLike = [10, 20, 30, 40, 50]; let uint16Array: collections.Uint16Array = new collections.Uint16Array(arrayLike); uint16Array.some((element: number) => element < 1); // false
every(predicate: TypedArrayPredicateFn<number, Uint16Array>): boolean
测试ArkTS Uint16Array中的所有元素是否满足指定条件。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint16Array> | 是 | 用于测试的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 如果所有元素都满足指定条件则返回true,否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The every method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let arrayLike = [10, 20, 30, 40, 50]; let uint16Array: collections.Uint16Array = new collections.Uint16Array(arrayLike); uint16Array.every((element: number) => element > 1); // true
fill(value: number, start?: number, end?: number): Uint16Array
使用特定值填充ArkTS Uint16Array指定范围的全部元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| value | number | 是 | 待填充的值,取值范围为0到2^16-1,即0到65535。如果元素超出此范围,该元素会通过取模运算转换为0~65535范围内的值(即执行 value & 0xFFFF 操作)。 |
| start | number | 否 | 开始填充的索引,如果start < 0,则会从start + Uint16Array.length位置开始。默认值为0。 |
| end | number | 否 | 结束填充的索引(不包括该元素),如果end < 0,则会到end + Uint16Array.length位置结束。默认为ArkTS Uint16Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 填充后的ArkTS Uint16Array。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The fill method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let arrayLike = [1, 2, 3]; new collections.Uint16Array(arrayLike).fill(4); // Uint16Array [4, 4, 4] new collections.Uint16Array(arrayLike).fill(4, 1); // Uint16Array [1, 4, 4] new collections.Uint16Array(arrayLike).fill(4, 1, 2); // Uint16Array [1, 4, 3]
filter(predicate: TypedArrayPredicateFn<number, Uint16Array>): Uint16Array
返回一个新ArkTS Uint16Array,其包含满足指定条件的所有元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint16Array> | 是 | 用于元素过滤的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 过滤后的ArkTS Uint16Array对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The filter method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([0, 1, 2, 3, 4]); let filtered: collections.Uint16Array = array.filter((element: number) => element % 2 == 0); // Uint16Array [0, 2, 4]
find(predicate: TypedArrayPredicateFn<number, Uint16Array>): number | undefined
返回ArkTS Uint16Array中第一个满足指定条件的元素的值,如果所有元素都不满足,则返回undefined。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint16Array> | 是 | 用于元素查找的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | undefined | 第一个满足条件的元素的值;如果所有元素都不满足条件,则返回undefined。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The find method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([0, 1, 2, 3, 4]); array.find((element: number) => element > 2); // 3 array.find((element: number) => element > 4); // undefined
findIndex(predicate: TypedArrayPredicateFn<number, Uint16Array>): number
返回ArkTS Uint16Array中第一个满足指定条件的元素索引,如果所有元素都不满足,则返回-1。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint16Array> | 是 | 用于元素查找的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 第一个满足条件的元素索引;如果所有元素都不满足条件,则返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The findIndex method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
const array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let foundIndex: number = array.findIndex((element: number) => element % 2 === 0); // 1
forEach(callbackFn: TypedArrayForEachCallback<number, Uint16Array>): void
对ArkTS Uint16Array中的每个元素执行提供的回调函数。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayForEachCallback<number, Uint16Array> | 是 | 用于对每个元素执行的回调函数。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The forEach method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint16Array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3]);
uint16Array.forEach((value: number, index: number, array: collections.Uint16Array) => {
console.info(`Element ${value} at index ${index}`);
}); indexOf(searchElement: number, fromIndex?: number): number
返回在ArkTS Uint16Array中给定元素的第一个索引,如果不存在,则返回-1。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| searchElement | number | 是 | 待索引的值。 |
| fromIndex | number | 否 | 搜索的起始下标。默认值为0。如果下标大于等于ArkTS Uint16Array的长度,则返回-1。如果下标为负数,则被视为距离数组尾部的偏移,从前到后搜索。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | ArkTS Uint16Array中给定元素的第一个索引;没有找到,则返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The indexOf method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([3, 5, 9]); array.indexOf(3); // 0 array.indexOf(7); // -1 array.indexOf(9, 2); // 2 array.indexOf(9, -2); // 2
lastIndexOf(searchElement: number, fromIndex?: number): number
返回ArkTS Uint16Array实例中最后一次出现searchElement的索引,如果不包含该元素,则返回-1。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| searchElement | number | 是 | 待索引的值。 |
| fromIndex | number | 否 | 搜索的起始下标,从该位置开始往前查找。默认值为ArkTS Uint16Array长度减1(即从末尾开始)。如果提供的下标值是负数,则被当作距离数组尾部的偏移,从后到前搜索。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | ArkTS Uint16Array中给定元素的最后一个索引;没有找到,则返回-1。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The lastIndexOf method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([3, 5, 9]); console.info(array.lastIndexOf(3) + ''); // 预期输出:0 console.info(array.lastIndexOf(7) + ''); // 预期输出:-1 console.info(array.lastIndexOf(9, 2) + ''); // 预期输出:2 console.info(array.lastIndexOf(9, -2) + ''); // 预期输出:-1
join(separator?: string): string
将ArkTS Uint16Array的所有元素拼接成一个字符串,元素之间使用指定的分隔符分隔。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| separator | string | 否 | 分隔字符串。如果省略,则使用逗号分隔。 |
返回值:
| 类型 | 说明 |
|---|---|
| string | 包含所有元素拼接成的字符串。如果ArkTS Uint16Array为空,则返回空字符串。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The join method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]);
let joined: string = array.join('-'); // "1-2-3-4-5" map(callbackFn: TypedArrayMapCallback<number, Uint16Array>): Uint16Array
对ArkTS Uint16Array中的每个元素应用指定的回调函数,并使用结果创建一个新的ArkTS Uint16Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayMapCallback<number, Uint16Array> | 是 | 对每个元素进行变换的回调函数,其返回值依次构成新ArkTS Uint16Array的对应元素。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 新ArkTS Uint16Array对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The map method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([25, 36, 49]); const mapped: collections.Uint16Array = array.map(Math.sqrt); // Uint16Array [5, 6, 7]
reduce(callbackFn: TypedArrayReduceCallback<number, number, Uint16Array>): number
对ArkTS Uint16Array中的每个元素执行归约函数,并返回最终的归约结果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<number, number, Uint16Array> | 是 | 归约函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 由归约函数返回的结果。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduce method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value); // reducedValue == 15
reduceRight(callbackFn: TypedArrayReduceCallback<number, number, Uint16Array>): number
反向遍历ArkTS Uint16Array,对每个元素执行归约函数,并返回最终的归约结果。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<number, number, Uint16Array> | 是 | 归约函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 由归约函数返回的结果。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduceRight method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value); console.info(reducedValue + ''); // 预期输出:15
reduce(callbackFn: TypedArrayReduceCallback<number, number, Uint16Array>, initialValue: number): number
对ArkTS Uint16Array中的每个元素执行归约函数,接收初始值作为首次调用参数,并返回最终的归约结果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<number, number, Uint16Array> | 是 | 归约函数。 |
| initialValue | number | 是 | 初始值。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 由归约函数返回的结果。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduce method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value, 1); // reducedValue == 16
reduceRight<U = number>(callbackFn: TypedArrayReduceCallback<U, number, Uint16Array>, initialValue: U): U
反向遍历ArkTS Uint16Array,对每个元素执行归约函数,接收初始值作为首次调用参数,并返回最终的归约结果。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<U, number, Uint16Array> | 是 | 归约函数。 |
| initialValue | U | 是 | 初始值。 |
返回值:
| 类型 | 说明 |
|---|---|
| U | 由归约函数返回的结果。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduceRight method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value, 1); console.info(reducedValue + ''); // 预期输出: 16
reduce<U>(callbackFn: TypedArrayReduceCallback<U, number, Uint16Array>, initialValue: U): U
对ArkTS Uint16Array中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<U, number, Uint16Array> | 是 | 归约函数。 |
| initialValue | U | 是 | 初始值。 |
返回值:
| 类型 | 说明 |
|---|---|
| U | 由归约函数返回的结果。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduce method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let reducedValue: string = array.reduce<string>((accumulator: string, value: number) => accumulator + value, "initialValue"); // reducedValue == initialValue12345
reverse(): Uint16Array
原地反转ArkTS Uint16Array的元素顺序(修改原数组),并返回修改后的原数组引用。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 反转后的ArkTS Uint16Array对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reverse method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let reversed: collections.Uint16Array = array.reverse(); // Uint16Array [5, 4, 3, 2, 1]
set(array: ArrayLike<number>, offset?: number): void
将传入的ArrayLike元素依次写入到指定的起始位置。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| array | ArrayLike<number> | 是 | 用于设置的ArrayLike对象。 |
| offset | number | 否 | 写入的起始位置,需为非负整数。默认值为0。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The set method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let buffer: collections.ArrayBuffer = new collections.ArrayBuffer(16); let array: collections.Uint16Array = new collections.Uint16Array(buffer); array.set([1, 2, 3], 3); // Uint16Array [0, 0, 0, 1, 2, 3, 0, 0]
slice(start?: number, end?: number): Uint16Array
返回一个新的ArkTS Uint16Array对象,其包含原ArkTS Uint16Array指定范围的内容。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| start | number | 否 | 开始索引,取值范围为[0, Uint16Array.length - 1]。如果start < 0,则会从start + Uint16Array.length位置开始。超出范围时自动修正为边界值。默认值为0。 |
| end | number | 否 | 结束索引(不包括该元素),取值范围为[0, Uint16Array.length]。如果end < 0,则会到end + Uint16Array.length位置结束。超出范围时自动修正为边界值。默认为ArkTS Uint16Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 包含原ArkTS Uint16Array指定范围内容的新ArkTS Uint16Array对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The slice method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); array.slice(); // Uint16Array [1, 2, 3, 4, 5] array.slice(1, 3); // Uint16Array [2, 3] array.slice(-2); // Uint16Array [4, 5]
sort(compareFn?: TypedArrayCompareFn<number>): Uint16Array
原地对ArkTS Uint16Array进行排序(修改原数组),并返回排序后的原数组引用。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| compareFn | TypedArrayCompareFn<number> | 否 | 用于确定元素顺序的函数。默认使用升序排序。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 排序后的ArkTS Uint16Array对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The sort method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 3, 5, 4, 2]); array.sort(); // Uint16Array [1, 2, 3, 4, 5] array.sort((first: number, second: number) => first - second); // Uint16Array [1, 2, 3, 4, 5] array.sort((first: number, second: number) => second - first); // Uint16Array [5, 4, 3, 2, 1]
subarray(begin?: number, end?: number): Uint16Array
从指定的位置截取数组,返回一个新的、基于相同ArkTS ArrayBuffer的ArkTS Uint16Array对象。修改返回的subarray或原始Uint16Array会互相影响,因为它们共享同一份底层ArrayBuffer数据。如需创建独立副本,请使用slice()方法。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| begin | number | 否 | 开始索引,取值范围为[0, Uint16Array.length - 1]。如果begin < 0,则会从begin + Uint16Array.length位置开始。超出范围时自动修正为边界值。默认值为0。 |
| end | number | 否 | 结束索引(不包括该元素),如果end < 0,则会到end + Uint16Array.length位置结束。默认为ArkTS Uint16Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint16Array | 新的ArkTS Uint16Array对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The subarray method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]); let subArray: collections.Uint16Array = array.subarray(); // Uint16Array [1, 2, 3, 4, 5] subArray.set([10, 20, 30]); // Uint16Array [10, 20, 30, 4, 5]
at(index: number): number | undefined
返回指定下标的元素,如果不存在,则返回undefined。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| index | number | 是 | 要返回的元素的索引(从0开始),取值为整数。如果index < 0,则会访问index + Uint16Array.length位置的元素。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | undefined | 指定下标的元素;如果不存在,则返回undefined。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The at method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]);
console.info(`element: ${array.at(2)}`); // element: 3
console.info(`element: ${array.at(-1)}`); // element: 5
console.info(`element: ${array.at(6)}`); // element: undefined includes(searchElement: number, fromIndex?: number): boolean
判断ArkTS Uint16Array是否包含特定元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| searchElement | number | 是 | 待搜索的元素。 |
| fromIndex | number | 否 | 开始搜索的索引,如果fromIndex < 0,则会从fromIndex + Uint16Array.length位置开始。默认值为0。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 如果ArkTS Uint16Array包含指定的元素,则返回true;否则返回false。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The includes method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3]);
console.info(`includes: ${array.includes(2)}`); // includes: true
console.info(`includes: ${array.includes(4)}`); // includes: false
console.info(`includes: ${array.includes(3, 3)}`); // includes: false entries(): IterableIterator<[number, number]>
返回一个新的迭代器对象,该对象包含ArkTS Uint16Array中每个元素的键值对。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<[number, number]> | 新的迭代器对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The entries method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([11, 22, 33]);
let iterator: IterableIterator<[number, number]> = array.entries();
console.info(`value: ${iterator.next().value}`); // value: 0,11
console.info(`value: ${iterator.next().value}`); // value: 1,22
console.info(`value: ${iterator.next().value}`); // value: 2,33 keys(): IterableIterator<number>
返回一个新的迭代器对象,该对象包含ArkTS Uint16Array中每个元素的键(下标)。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<number> | 新的迭代器对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The keys method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]);
let iterator: IterableIterator<number> = array.keys();
for (const key of iterator) {
console.info(`${key}`); // 依次输出 0,1,2,3,4
} values(): IterableIterator<number>
返回一个新的迭代器对象,该对象包含ArkTS Uint16Array中每个元素的值。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<number> | 新的迭代器对象。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The values method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5]);
let iterator: IterableIterator<number> = array.values();
for (const value of iterator) {
console.info(`${value}`); // 依次输出 1,2,3,4,5
} [Symbol.iterator](): IterableIterator<number>
返回一个迭代器,迭代器的每一项是Uint16Array中对应位置的元素值。
本接口不支持在.ets文件中使用。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<number> | 返回一个迭代器。 |
错误码:
以下错误码的详细介绍请参见语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The Symbol.iterator method cannot be bound. |
示例:
let uint16Array: collections.Uint16Array = collections.Uint16Array.from([1, 2, 3, 4, 5, 6]);
for (let item of uint16Array) {
console.info(`value : ${item}`);
} [index: number]: number
返回Uint16Array指定索引位置的元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| index | number | 是 | 所需元素的从零开始的索引。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 返回指定索引位置的元素值。 |
示例:
let uint16Array = collections.Uint16Array.from([1, 2, 4]);
console.info(`Element at index 1: ${uint16Array[1]}`);