【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.

示例:

TypeScript 复制代码
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.

示例:

TypeScript 复制代码
// 以长度参数构造对象
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.

示例:

TypeScript 复制代码
// 例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.

示例:

TypeScript 复制代码
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对象。

示例:

TypeScript 复制代码
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对象。

示例:

TypeScript 复制代码
// 例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", "5"], (v: string, k: number) => parseInt(v));
// Uint32Array [1, 3, 5]

// 例3 从一个字符串创建
let array: collections.Uint32Array = collections.Uint32Array.from<string>(
"12345", (v: string, k: number) => parseInt(v));
// Uint32Array [1, 2, 3, 4, 5]

from

static from(iterable: Iterable<number>, mapFn?: TypedArrayFromMapFn<number, number>): TypedArray

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

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
iterable Iterable<number> 用于构造的可迭代对象。
mapFn TypedArrayFromMapFn<number, number> 映射函数。如果省略,则不对元素进行加工处理。

返回值:

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

示例:

TypeScript 复制代码
// 例1 不指定映射函数
let set: Set<number> = new Set<number>([1, 2, 3]);
let array: collections.Uint32Array = collections.Uint32Array.from(set);
// Uint32Array [1, 2, 3]

// 例2 指定映射函数
let set: Set<number> = new Set<number>([1, 2, 3]);
let array: collections.Uint32Array = collections.Uint32Array.from(
set, (v: number, k: number) => v + k);
// Uint32Array [1, 3, 5]

copyWithin

copyWithin(target: number, start: number, end?: number): TypedArray

从ArkTS TypedArray指定范围内的元素依次拷贝到目标位置。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
target number 目标起始位置的下标。
start number 源起始位置下标,如果start < 0,则会从start + typedarray.length位置开始。
end number 源终止位置下标,如果end < 0,则会从end + typedarray.length位置终止。默认为ArkTS TypedArray的长度。

返回值:

类型 说明
TypedArray 修改后的TypedArray。

错误码:

错误码ID 错误信息
10200011 The copyWithin method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5, 6, 7, 8]);
let copied: collections.Uint32Array = array.copyWithin(3, 1, 3);
// Uint32Array [1, 2, 3, 2, 3, 6, 7, 8]

some

some(predicate: TypedArrayPredicateFn<number, TypedArray>): boolean

测试ArkTS TypedArray中的是否存在元素满足指定条件。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
predicate TypedArrayPredicateFn<number, TypedArray> 用于测试的断言函数。

返回值:

类型 说明
boolean 如果存在元素满足指定条件返回true,否则返回false。

错误码:

以下错误码的详细介绍请参见语言基础类库错误码

错误码ID 错误信息
10200011 The some method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let arrayLike = [-10, 20, -30, 40, -50];
let uint32Array: collections.Uint32Array = new collections.Uint32Array(arrayLike);
uint32Array.some((element: number) => element < 0); // false

let int32Array: collections.Int32Array = new collections.Int32Array(arrayLike);
int32Array.some((element: number) => element < 0); // true

every

every(predicate: TypedArrayPredicateFn<number, TypedArray>): boolean

测试ArkTS TypedArray中的所有元素是否满足指定条件。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
predicate TypedArrayPredicateFn<number, TypedArray> 用于测试的断言函数。

返回值:

类型 说明
boolean 如果所有元素都满足指定条件则返回true,否则返回false。

错误码:

错误码ID 错误信息
10200011 The every method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let arrayLike = [-10, 20, -30, 40, -50];
let uint32Array: collections.Uint32Array = new collections.Uint32Array(arrayLike);
uint32Array.every((element: number) => element > 0); // true

let int32Array: collections.Int32Array = new collections.Int32Array(arrayLike);
int32Array.every((element: number) => element > 0); // false

fill

fill(value: number, start?: number, end?: number): TypedArray

使用特定值填充ArkTS TypedArray指定范围的全部元素。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
value number 待填充的值。
start number 开始填充的索引,如果start < 0,则会从start + typedarray.length位置开始。默认值为0。
end number 结束填充的索引,如果end < 0,则会到end + typedarray.length位置结束。默认为ArkTS TypedArray的长度。

返回值:

类型 说明
TypedArray 填充后的TypedArray。

错误码:

错误码ID 错误信息
10200011 The fill method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let arrayLike = [1, 2, 3];
new collections.Uint32Array(arrayLike).fill(4); // Uint32Array [4, 4, 4]
new collections.Uint32Array(arrayLike).fill(4, 1); // Uint32Array [1, 4, 4]
new collections.Uint32Array(arrayLike).fill(4, 1, 2); // Uint32Array [1, 4, 3]

filter

filter(predicate: TypedArrayPredicateFn<number, TypedArray>): TypedArray

返回一个新ArkTS TypedArray,其包含满足指定条件的所有元素。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
predicate TypedArrayPredicateFn<number, TypedArray> 用于元素过滤的断言函数。

返回值:

类型 说明
TypedArray 过滤后的ArkTS TypedArray对象。

错误码:

错误码ID 错误信息
10200011 The filter method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([0, 1, 2, 3, 4]);
let filtered: collections.Uint32Array = array.filter((element: number) => element % 2 == 0);
// Uint32Array [0, 2, 4]

find

find(predicate: TypedArrayPredicateFn<number, TypedArray>): number | undefined

返回ArkTS TypedArray中第一个满足指定条件的元素的值,如果所有元素都不满足,则返回undefined。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
predicate TypedArrayPredicateFn<number, TypedArray> 用于元素查找的断言函数。

返回值:

类型 说明
number | undefined 第一个满足条件的元素的值;如果所有元素都不满足条件,则返回undefined。

错误码:

错误码ID 错误信息
10200011 The find method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([0, 1, 2, 3, 4]);
array.find((element: number) => element > 2); // 3
array.find((element: number) => element > 4); // undefined

findIndex

findIndex(predicate: TypedArrayPredicateFn<number, TypedArray>): number

返回ArkTS TypedArray中第一个满足指定条件的元素索引,如果所有元素都不满足,则返回-1。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
predicate TypedArrayPredicateFn<number, TypedArray> 用于元素查找的断言函数。

返回值:

类型 说明
number 第一个满足条件的元素索引;如果所有元素都不满足条件,否返回-1。

错误码:

错误码ID 错误信息
10200011 The findIndex method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
const array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let foundIndex: number = array.findIndex((element: number) => element % 2 === 0); // 1

forEach

forEach(callbackFn: TypedArrayForEachCallback<number, TypedArray>): void

对ArkTS TypedArray中的每个元素执行提供的回调函数。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
callbackFn TypedArrayForEachCallback<number, TypedArray> 用于对每个元素执行的回调函数。

错误码:

错误码ID 错误信息
10200011 The forEach method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let uint32Array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3]);
uint32Array.forEach((value: number, index: number, array: collections.Uint32Array) => {
console.info(`Element ${value} at index ${index}`);
});

indexOf

indexOf(searchElement: number, fromIndex?: number): number

返回在ArkTS TypedArray中给定元素的第一个索引,如果不存在,则返回-1。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
searchElement number 待索引的值。
fromIndex number 搜索的起始下标。默认值为0。如果下标大于等于ArkTS TypedArray的长度,则返回-1。如果提供的下标值是负数,则被当做距离数组尾部的偏移,从前到后搜索。

返回值:

类型 说明
number 数组中元素的第一个索引;没有找到,则返回-1。

错误码:

错误码ID 错误信息
10200011 The indexOf method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([3, 5, 9]);
array.indexOf(3); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(9, -2); // 2

join

join(separator?: string): string

将ArkTS TypedArray的所有元素拼接成一个字符串,元素之间使用指定的分隔符分隔。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
separator string 分隔字符串。如果省略,则使用逗号分隔。

返回值:

类型 说明
string 包含所有元素拼接成的字符串。如果ArkTS TypedArray为空,则返回空字符串。

错误码:

错误码ID 错误信息
10200011 The join method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let joined: string = array.join('-'); // "1-2-3-4-5"

map

map(callbackFn: TypedArrayForEachCallback<number, TypedArray>): TypedArray

对ArkTS TypedArray中的每个元素应用指定的回调函数,并使用结果创建一个新的ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
callbackFn TypedArrayForEachCallback<number, TypedArray> 回调函数。

返回值:

类型 说明
TypedArray 新ArkTS TypedArray对象。

错误码:

错误码ID 错误信息
10200011 The map method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([25, 36, 49]);
const mapped: collections.Uint32Array = array.map(Math.sqrt); // Uint32Array [5, 6 ,7]

reduce

reduce(callbackFn: TypedArrayReduceCallback<number, number, TypedArray>): number

对ArkTS TypedArray中的每个元素执行归约函数,并返回最终的归约结果。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
callbackFn TypedArrayReduceCallback<number, number, TypedArray> 归约函数。

返回值:

类型 说明
number 由归约函数返回的结果。

错误码:

错误码ID 错误信息
10200011 The reduce method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value);
// reducedValue == 15

reduce

reduce(callbackFn: TypedArrayReduceCallback<number, number, TypedArray>, initialValue: number): number

对ArkTS TypedArray中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
callbackFn TypedArrayReduceCallback<number, number, TypedArray> 归约函数。
initialValue number 初始值。

返回值:

类型 说明
number 由归约函数返回的结果。

错误码:

错误码ID 错误信息
10200011 The reduce method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value, 1);
// reducedValue == 16

reduce

reduce<U>(callbackFn: TypedArrayReduceCallback<U, number, TypedArray>, initialValue: U): U

对ArkTS TypedArray中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
callbackFn TypedArrayReduceCallback<U, number, TypedArray> 归约函数。
initialValue U 初始值。

返回值:

类型 说明
U 由归约函数返回的结果。

错误码:

错误码ID 错误信息
10200011 The reduce method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let reducedValue: string = array.reduce<string>((accumulator: string, value: number) => accumulator + value, "initialValue");
// reducedValue == initialValue12345

reverse

reverse(): TypedArray

反转ArkTS TypedArray。

系统能力: SystemCapability.Utils.Lang

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

返回值:

类型 说明
TypedArray 反转后的ArkTS TypedArray对象。

错误码:

错误码ID 错误信息
10200011 The reverse method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let reversed: collections.Uint32Array = array.reverse(); // Uint32Array [5, 4, 3, 2, 1]

set

set(array: ArrayLike<number>, offset?: number): void

将传入的ArrayLike元素依次写入到指定的起始位置。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
array ArrayLike<number> 用于设置的ArrayLike对象。
offset number 写入的起始位置。默认为0。

错误码:

错误码ID 错误信息
10200011 The set method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let buffer: collections.ArrayBuffer = new collections.ArrayBuffer(8);
let array: collections.Uint8Array = new collections.Uint8Array(buffer);
array.set([1, 2, 3], 3); // Uint8Array [0, 0, 0, 1, 2, 3, 0, 0]

slice

slice(start?: number, end?: number): TypedArray

返回一个新的ArkTS TypedArray对象,其包含原ArkTS TypedArray指定范围的内容。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
start number 开始索引,如果start < 0,则会从start + typedarray.length位置开始。默认为0。
end number 结束索引(不包括该元素),如果end < 0,则会到end + typedarray.length位置结束。默认为ArkTS TypedArray的长度。

返回值:

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

错误码:

错误码ID 错误信息
10200011 The slice method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
array.slice(); // Uint32Array [1, 2, 3, 4, 5]
array.slice(1, 3); // Uint32Array [2, 3]
array.slice(-2); // Uint32Array [4, 5]

sort

sort(compareFn?: TypedArrayCompareFn<number>): TypedArray

对ArkTS TypedArray进行排序,并返回排序后的ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
compareFn TypedArrayCompareFn<number> 用于确定元素顺序的函数。默认使用升序排序。

返回值:

类型 说明
TypedArray 排序后的ArkTS TypedArray对象。

错误码:

错误码ID 错误信息
10200011 The sort method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 3, 5, 4, 2]);
array.sort(); // Uint32Array [1, 2, 3, 4, 5]
array.sort((a: number, b: number) => a - b); // Uint32Array [1, 2, 3, 4, 5]
array.sort((a: number, b: number) => b - a); // Uint32Array [5, 4, 3, 2, 1]

subarray

subarray(begin?: number, end?: number): TypedArray

返回一个新的、基于相同ArkTS ArrayBuffer的ArkTS TypedArray对象。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
begin number 开始索引,如果begin < 0,则会从begin + typedarray.length位置开始。默认值为0。
end number 结束索引(不包括该元素),如果end < 0,则会到end + typedarray.length位置结束。默认为ArkTS TypedArray的长度。

返回值:

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

错误码:

错误码ID 错误信息
10200011 The subarray method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.from([1, 2, 3, 4, 5]);
let subArray: collections.Uint32Array = array.subarray(); // Uint32Array [1, 2, 3, 4, 5]
subArray.set([10, 20, 30]); // Uint32Array [10, 20, 30, 4, 5]

at

at(index: number): number | undefined

返回指定下标的元素,如果不存在,则返回undefined。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
index number 要返回的Array元素的索引(从零开始),取值为整数。如果index < 0,则会访问index + typedarray.length位置的元素。

返回值:

类型 说明
number | undefined 指定下标的元素;如果不存在,则返回undefined。

错误码:

错误码ID 错误信息
10200011 The at method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.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

includes(searchElement: number, fromIndex?: number): boolean

判断ArkTS TypedArray是否包含特定元素。

系统能力: SystemCapability.Utils.Lang

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

参数:

参数名 类型 必填 说明
searchElement number 待搜索的元素。
fromIndex number 开始搜索的索引,如果fromIndex < 0,则会从fromIndex + typedarray.length位置开始。默认值为0。

返回值:

类型 说明
boolean 如果ArkTS TypedArray包含指定的元素,则返回true;否则返回false。

错误码:

错误码ID 错误信息
10200011 The includes method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.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

entries(): IterableIterator<[number, number]>

返回一个新的迭代器对象,该对象包含ArkTS TypedArray中每个元素的键值对。

系统能力: SystemCapability.Utils.Lang

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

返回值:

类型 说明
IterableIterator<[number, number]> 新的迭代器对象。

错误码:

错误码ID 错误信息
10200011 The entries method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.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

keys(): IterableIterator<number>

返回一个新的迭代器对象,该对象包含ArkTS TypedArray中每个元素的键(下标)。

系统能力: SystemCapability.Utils.Lang

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

返回值:

类型 说明
IterableIterator<number> 新的迭代器对象。

错误码:

错误码ID 错误信息
10200011 The keys method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.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

values(): IterableIterator<number>

返回一个新的迭代器对象,该对象包含ArkTS TypedArray中每个元素的值。

系统能力: SystemCapability.Utils.Lang

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

返回值:

类型 说明
IterableIterator<number> 新的迭代器对象。

错误码:

错误码ID 错误信息
10200011 The values method cannot be bound.
10200201 Concurrent modification exception.

示例:

TypeScript 复制代码
let array: collections.Uint32Array = collections.Uint32Array.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
}
相关推荐
万少7 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
用户9623779544812 小时前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机15 小时前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent
数据智能老司机15 小时前
用于进攻性网络安全的智能体 AI——智能体 AI 入门
人工智能·安全·agent
用户9623779544817 小时前
DVWA 靶场实验报告 (Medium Level)
安全
red1giant_star17 小时前
S2-067 漏洞复现:Struts2 S2-067 文件上传路径穿越漏洞
安全
用户9623779544820 小时前
DVWA Weak Session IDs High 的 Cookie dvwaSession 为什么刷新不出来?
安全
Huang兄1 天前
鸿蒙-List和Grid拖拽排序:仿微信小程序删除效果
harmonyos·arkts·arkui
anyup2 天前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
Ranger09292 天前
鸿蒙开发新范式:Gpui
rust·harmonyos