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

名称类型只读可选说明
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); // falselet 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); // truelet 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
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/43763.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Laravel Excel导出功能:高效实现数据导出

Laravel是一个功能丰富的PHP Web开发框架&#xff0c;它提供了许多内置功能来简化开发过程。其中&#xff0c;Laravel Excel导出功能是处理数据导出任务的强大工具。通过使用Maatwebsite的Laravel Excel包&#xff0c;开发者可以轻松地将数据集导出为Excel文件&#xff0c;这对…

软件代码漏洞风险等级

代码漏洞的风险等级通常根据漏洞的潜在影响、利用难易程度以及可能造成的损害程度来划分。不同的组织或机构可能会采用不同的标准或评分系统来评估漏洞的风险等级。以下是一些常见的代码漏洞风险等级划分标准和考虑因素: 通用漏洞评分系统(CVSS) CVSS是一种广泛使用的漏洞…

DMA方式的知识点笔记

苏泽 “弃工从研”的路上很孤独&#xff0c;于是我记下了些许笔记相伴&#xff0c;希望能够帮助到大家 目录 1. DMA基本概念 2. DMA传送过程 易错点 DMA控制器操作流程 3. DMA传送方式 这是单总线的结果 &#xff08;CPU说了算 所以不会产生于CPU的冲突&#xff09; 这…

新浪API系列:支付API打造无缝支付体验,畅享便利生活(3)

在当今数字化时代&#xff0c;支付功能已经成为各类应用和平台的必备要素之一。作为开发者&#xff0c;要构建出安全、便捷的支付解决方案&#xff0c;新浪支付API是你不可或缺的利器。新浪支付API提供了全面而强大的接口和功能&#xff0c;帮助开发者轻松实现在线支付的集成和…

软件开发面试题(C#语言,.NET框架)

1. 解释什么是委托&#xff08;Delegate&#xff09;&#xff0c;并举例说明它在C#中的用法。 委托是一种引用类型&#xff0c;它可以用于封装一个或多个方法。委托对象可以像方法一样调用&#xff0c;甚至可以用于创建事件处理程序。委托是C#中实现事件和回调函数的重要机制。…

【PyTorch][chapter 26][李宏毅深度学习][attention-1]

前言&#xff1a; attention 在自然语言处理&#xff0c;声音处理里面是一个很重要的技巧. attention 要解决的是输入的向量长度不定. 根据输入输出的不同,分为三种场景&#xff1a; 输入N个向量&#xff0c;输出N个向量,这是本章的重点 输入N个向量&#xff0c;输出向量不定 输…

人工智能与技术失业:历史教训与未来趋势

在1938年大萧条时期&#xff0c;MIT校长卡尔康普顿讨论了“技术失业”&#xff0c;即因技术进步导致的失业现象。他认为&#xff0c;尽管技术进步会对个别工人和社区产生负面影响&#xff0c;但从整体上看&#xff0c;技术进步创造了更多的就业机会。这一观点在今天依然具有重要…

施罗德数列SQL实现

在组合数学中,施罗德数用来描述从(0,0)到(n,n)的格路中,只能使用(1,0)、(0,1)、(1,1)三种移动方式,始终位于对角线下方且不越过对角线的路径数 DECLARE n INT 10 DECLARE i INT DECLARE rst INT DECLARE old INT1CREATE TABLE #rst (i INT ,rst int )INSERT INTO #rst values(…

react使用markdown进行展示

有一些文档非常长&#xff0c;但是又要挨个设置样式&#xff0c;直接用 组件库 - marked 注意文档要放在public下才能读取。但非常方便 import { marked, Renderer } from "marked".....const [html, setHtml] useState<any>("")const renderer:…

3-7 使用深度学习解决温度即示数问题

3-7 使用深度学习解决温度即示数问题 直接上代码 %matplotlib inline import matplotlib.pyplot as plt import numpy as np import torch torch.set_printoptions(edgeitems2, linewidth75)设置Jupyter Notebook在单元格中内嵌显示图像&#xff0c;导入所需库并设置PyTorch的…

代码随想三刷动态规划篇10

代码随想三刷动态规划篇10 300. 最长递增子序列题目代码 674. 最长连续递增序列题目代码 718. 最长重复子数组题目代码 1143. 最长公共子序列题目代码 1035. 不相交的线题目代码 300. 最长递增子序列 题目 链接 代码 class Solution {public int lengthOfLIS(int[] nums) {…

裸金属服务器与物理服务器之间的区别

裸金属服务器与物理服务器之间主要的区别就在于虚拟化技术、资源共享和灵活性等多种方面。 裸金属服务器主要是通过虚拟化技术将物理服务器划分成多个独立的虚拟机&#xff0c;致使每个虚拟机都具有独立的操作系统与资源&#xff1b;物理服务器则是指有着独立的服务器硬件&…

阿里发布大模型发布图结构长文本处理智能体,超越GPT-4-128k

随着大语言模型的发展&#xff0c;处理长文本的能力成为了一个重要挑战。虽然有许多方法试图解决这个问题&#xff0c;但都存在不同程度的局限性。最近&#xff0c;阿里巴巴的研究团队提出了一个名为GraphReader的新方法&#xff0c;通过将长文本组织成图结构&#xff0c;并利用…

0. python面试常见问题

这里写目录标题 1.python迭代器和生成器1.1 迭代器1.2 生成器1.3 总结一下迭代器和生成器的应用场景 什么是装饰器&#xff08;decorator&#xff09;Python中如何实现多线程-GIL&#xff08;全局解释器锁&#xff09; 知乎&#xff1a;python面试170题 知乎&#xff1a;30道py…

2,区块链、数字货币及其应用场景(react+区块链实战)

2&#xff0c;区块链、数字货币及其应用场景&#xff08;react区块链实战&#xff09; 一、什么是区块链&#xff1f;1 ibloackchain&#xff08;1&#xff09;安装ibloackchain&#xff08;2&#xff09;Blance查询余额&#xff08;3&#xff09;Mine挖矿&#xff08;4&#x…

JavaScript中的拷贝技术探秘:浅拷贝与深拷贝的奥秘

最新技术资源&#xff08;建议收藏&#xff09; https://www.grapecity.com.cn/resources/ 前言 JavaScript中的浅拷贝和深拷贝是非常重要的概念&#xff0c;它们在处理对象和数组时具有不同的作用。在编程中&#xff0c;经常需要复制数据以便进行各种操作&#xff0c;但必须注…

小波与傅里叶变换的对比(Python)

直接上代码&#xff0c;理论可以去知乎看。 #Import necessary libraries %matplotlib inline import numpy as np import matplotlib.pyplot as plt import seaborn as snsimport pywt from scipy.ndimage import gaussian_filter1d from scipy.signal import chirp import m…

基于 sftp 的 NAS (局域网文件存储服务器)

局域网 NAS (文件存储服务器) 的基本功能有: 能够存储文件, 同时能够通过多个设备访问 (上传/下载) 文件. 这些功能通过 sftp 可以实现. sftp 是基于 SSH 的文件传输协议, SSH 全程加密传输, 使用 公钥 认证 (不使用密码/口令), 能够提供很高的安全性. 上文说到, 在 LVM 和 bt…

谷粒商城-个人笔记(集群部署篇三)

前言 ​学习视频&#xff1a;​Java项目《谷粒商城》架构师级Java项目实战&#xff0c;对标阿里P6-P7&#xff0c;全网最强​学习文档&#xff1a; 谷粒商城-个人笔记(基础篇一)谷粒商城-个人笔记(基础篇二)谷粒商城-个人笔记(基础篇三)谷粒商城-个人笔记(高级篇一)谷粒商城-个…

古建筑倾斜在线监测系统:科技守护历史的创新实践

​ ​​在文化遗产保护的广阔领域中&#xff0c;古建筑的健康监测占据着举足轻重的地位。然而&#xff0c;传统的监测方法往往受限于布线复杂、安装难度大以及对古建筑本体可能造成的伤害等问题。近年来&#xff0c;一种新型的古建筑倾斜在线监测系统应运而生&#xff0c;它…