【HarmonyOS NEXT】鸿蒙线程安全容器集collections.TypedArray

collections.TypedArray

一种线性数据结构,底层基于ArkTS ArrayBuffer实现。目前支持包括Int8Array、Uint8Array、Int16Array、Uint16Array、Int32Array以及Uint32Array。

文档中存在泛型的使用,涉及以下泛型标记符:

  • TypedArray: 指上述6种具体的ArkTS TypedArray。

属性

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

名称 类型 只读 可选 说明
buffer ArrayBuffer ArkTS TypedArray底层使用的buffer。
byteLength number ArkTS TypedArray的所占的字节数。
byteOffset number ArkTS TypedArray距离其ArrayBuffer起始位置的偏移。
length number ArkTS TypedArray元素个数。
BYTES_PER_ELEMENT number ArkTS TypedArray中每个元素所占用的字节数。

constructor

constructor()

构造函数,用于创建一个空ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

错误码:

错误码ID 错误信息
10200012 The TypedArray's constructor cannot be directly invoked.

示例:


let int8Array: collections.Int8Array = new collections.Int8Array();
let uint8Array: collections.Uint8Array = new collections.Uint8Array();
let int16Array: collections.Int16Array = new collections.Int16Array();
let uint16Array: collections.Uint16Array = new collections.Uint16Array();
let int32Array: collections.Int32Array = new collections.Int32Array();
let uint32Array: collections.Uint32Array = new collections.Uint32Array();

constructor

constructor(length: number)

构造函数,用于创建一个指定长度的ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

参数:

参数名 类型 必填 说明
length number 用于指定ArkTS TypedArray的长度。

错误码:

错误码ID 错误信息
10200012 The TypedArray's constructor cannot be directly invoked.

示例:


// 以长度参数构造对象
let int8Array: collections.Int8Array = new collections.Int8Array(12);
let uint8Array: collections.Uint8Array = new collections.Uint8Array(12);
let int16Array: collections.Int16Array = new collections.Int16Array(12);
let uint16Array: collections.Uint16Array = new collections.Uint16Array(12);
let int32Array: collections.Int32Array = new collections.Int32Array(12);
let uint32Array: collections.Uint32Array = new collections.Uint32Array(12);

constructor

constructor(array: ArrayLike<number> | ArrayBuffer)

构造函数,以ArrayLike或ArkTS ArrayBuffer创建一个ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

参数:

参数名 类型 必填 说明
array ArrayLike<number> | ArrayBuffer 用于构造ArkTS TypedArray的对象。当参数类型是ArrayBuffer时buffer所占的字节数须是4的整数倍。

错误码:

错误码ID 错误信息
10200012 The TypedArray's constructor cannot be directly invoked.

示例:


// 例1 从一个ArrayLike构造对象
let arrayLike = [1, 3, 5];
let array: collections.Uint32Array = new collections.Uint32Array(arrayLike);

// 例2 从一个ArrayBuffer构造对象
let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12);
let array: collections.Uint32Array = new collections.Uint32Array(arrayBuffer);

// 例3 从另一ArkTS TypedArray构造对象
let arrayLike = [1, 3, 5];
let uint8Array: collections.Uint8Array = new collections.Uint8Array(arrayLike);
// Uint8Array [1, 3, 5]
let uint32Array: collections.Uint32Array = new collections.Uint32Array(uint8Array);
// Uint32Array [1, 3, 5]

constructor

constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number)

构造函数,以ArrayBuffer创建一个ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

参数:

参数名 类型 必填 说明
buffer ArrayBuffer 用于构造ArkTS TypedArray的ArrayBuffer对象。buffer所占的字节数须是4的整数倍。
byteOffset number 指定buffer的字节偏移,默认为0。
length number 指定ArkTS TypedArray的长度,默认为0。

错误码:

错误码ID 错误信息
10200012 The TypedArray's constructor cannot be directly invoked.

示例:


let int32Array: collections.Int32Array = collections.Int32Array.from([1, 2, 3, 4, 5, 6]);
console.info("byteLength: " + int32Array.buffer.byteLength) // byteLength: 24
// 从int32Array对应buffer第4个字节开始,长度为5
let uint32Array: collections.Uint32Array = new collections.Uint32Array(int32Array.buffer, 4, 5);
console.info("[" + uint32Array + "]"); // [2, 3, 4, 5, 6]

from

static from(arrayLike: ArrayLike<number>): TypedArray

从一个ArrayLike或者可迭代对象中创建一个ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

参数:

参数名 类型 必填 说明
arrayLike ArrayLike<number> 用于构造ArkTS TypedArray的ArrayLike对象。

返回值:

类型 说明
TypedArray 新创建的ArkTS TypedArray对象。

示例:


let arrayLike = [1, 3, 5];
let array: collections.Uint32Array = collections.Uint32Array.from(arrayLike);
// Uint32Array [1, 3, 5]

from

static from<T>(arrayLike: ArrayLike<T>, mapFn: TypedArrayFromMapFn<T, number>): TypedArray

从一个ArrayLike中创建一个ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

元服务API: 从API version 12开始,该接口支持在元服务中使用。

参数:

参数名 类型 必填 说明
arrayLike ArrayLike<T> 用于构造ArrayLike对象。
mapFn TypedArrayFromMapFn<T, number> 映射函数。

返回值:

类型 说明
TypedArray 新创建的ArkTS TypedArray对象。

示例:


// 例1 从一个对象创建
let array: collections.Uint32Array = collections.Uint32Array.from<number>(
{ length: 5 }, (v: Object, k: number) => k);
// Uint32Array [0, 1, 2, 3, 4]

// 例2 从一个字符数组创建
let array: collections.Uint32Array = collections.Uint32Array.from<string>(
["1", "3"
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ConneyWu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值