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"