collections.TypedArray

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

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

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

属性

系统能力: SystemCapability.Utils.Lang

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

名称类型只读可选说明
bufferArrayBufferArkTS TypedArray底层使用的buffer。
byteLengthnumberArkTS TypedArray的所占的字节数。
byteOffsetnumberArkTS TypedArray距离其ArrayBuffer起始位置的偏移。
lengthnumberArkTS TypedArray元素个数。
BYTES_PER_ELEMENTnumberArkTS TypedArray中每个元素所占用的字节数。

constructor

constructor()

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

系统能力: SystemCapability.Utils.Lang

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

错误码:

错误码ID错误信息
10200012The 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开始,该接口支持在元服务中使用。

参数:

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

错误码:

错误码ID错误信息
10200012The 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开始,该接口支持在元服务中使用。

参数:

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

错误码:

错误码ID错误信息
10200012The 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开始,该接口支持在元服务中使用。

参数:

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

错误码:

错误码ID错误信息
10200012The 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开始,该接口支持在元服务中使用。

参数:

参数名类型必填说明
arrayLikeArrayLike<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开始,该接口支持在元服务中使用。

参数:

参数名类型必填说明
arrayLikeArrayLike<T>用于构造ArrayLike对象。
mapFnTypedArrayFromMapFn<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", "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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

示例:

// 例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开始,该接口支持在元服务中使用。

参数:

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

返回值:

类型说明
TypedArray修改后的TypedArray。

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

类型说明
TypedArray填充后的TypedArray。

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

类型说明
TypedArray新ArkTS TypedArray对象。

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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错误信息
10200011The reverse method cannot be bound.
10200201Concurrent modification exception.

示例:


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开始,该接口支持在元服务中使用。

参数:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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开始,该接口支持在元服务中使用。

参数:

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

返回值:

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

错误码:

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

示例:


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错误信息
10200011The entries method cannot be bound.
10200201Concurrent modification exception.

示例:


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错误信息
10200011The keys method cannot be bound.
10200201Concurrent modification exception.

示例:


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错误信息
10200011The values method cannot be bound.
10200201Concurrent modification exception.

示例:


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
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐