一种线性数据结构,底层基于ArrayBuffer实现。
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块仅支持在ArkTS文件(文件后缀为.ets)中导入使用。
装饰器类型:@Sendable
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| buffer | ArrayBuffer | 是 | 否 | ArkTS Uint8Array底层使用的buffer。 |
| byteLength | number | 是 | 否 | ArkTS Uint8Array所占的字节数。 |
| byteOffset | number | 是 | 否 | ArkTS Uint8Array距离其ArrayBuffer起始位置的字节偏移。 |
| length | number | 是 | 否 | ArkTS Uint8Array元素个数。 |
| BYTES_PER_ELEMENT | number | 是 | 否 | ArkTS Uint8Array中每个元素所占的字节数。 |
constructor()
构造函数,用于创建一个空ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint8Array's constructor cannot be directly invoked. |
示例:
let uint8Array: collections.Uint8Array = new collections.Uint8Array();
constructor(length: number)
构造函数,用于创建一个指定长度的ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| length | number | 是 | 用于指定ArkTS Uint8Array的长度,取值为非负整数。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint8Array's constructor cannot be directly invoked. |
示例:
// 以长度参数构造对象 let uint8Array: collections.Uint8Array = new collections.Uint8Array(12);
constructor(elements: Iterable<number>)
构造函数,以Iterable创建一个ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| elements | Iterable<number> | 是 | 可迭代数字集合,用于构造ArkTS Uint8Array对象。集合中的数值取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint8Array's constructor cannot be directly invoked. |
示例:
// 从一个Iterable构造对象 let set: Set<number> = new Set<number>([1, 2, 3, 256]); let uint8Array: collections.Uint8Array = new collections.Uint8Array(set); // Uint8Array [1, 2, 3, 0]
constructor(array: ArrayLike<number> | ArrayBuffer)
构造函数,以ArrayLike或ArkTS ArrayBuffer创建一个ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| array | ArrayLike<number> | ArrayBuffer | 是 | 用于构造ArkTS Uint8Array的对象。传入ArrayLike<number>时,数值取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint8Array's constructor cannot be directly invoked. |
示例:
// 例1 从一个ArrayLike构造对象 let arrayLike = [1, 3, 5]; let uint8Array: collections.Uint8Array = new collections.Uint8Array(arrayLike);
// 例2 从一个ArrayBuffer构造对象 let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(10); let uint8Array: collections.Uint8Array = new collections.Uint8Array(arrayBuffer);
// 例3 从另一ArkTS Uint8Array构造对象 let arrayLike = [1, 3, 5]; // uint8Array1 [1, 3, 5] let uint8Array1: collections.Uint8Array = new collections.Uint8Array(arrayLike); // uint8Array2 [1, 3, 5] let uint8Array2: collections.Uint8Array = new collections.Uint8Array(uint8Array1);
constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number)
构造函数,以ArrayBuffer创建一个ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| buffer | ArrayBuffer | 是 | 用于构造ArkTS Uint8Array的ArrayBuffer对象。且ArkTS Uint8Array和该ArrayBuffer共享内存,修改其中的数据会相互影响。 |
| byteOffset | number | 否 | 指定buffer的字节偏移,取值为非负整数且须小于buffer.byteLength,超出范围时将抛出异常。默认值为0。 |
| length | number | 否 | 指定ArkTS Uint8Array的长度,byteOffset和length的总和不能超过buffer.byteLength,超出范围时将抛出异常。默认值为buffer.byteLength - byteOffset。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200012 | The Uint8Array's constructor cannot be directly invoked. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5, 6]);
console.info(`byteLength: ${uint8Array.buffer.byteLength}`); // byteLength: 6
// 从uint8Array的buffer中下标为1的成员开始获取,长度为5
let uint8Array1: collections.Uint8Array = new collections.Uint8Array(uint8Array.buffer, 1, 5);
console.info(`[${uint8Array1}]`); // [2, 3, 4, 5, 6] static from(arrayLike: ArrayLike<number>): Uint8Array
从一个ArrayLike中创建一个ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| arrayLike | ArrayLike<number> | 是 | 用于构造ArkTS Uint8Array的ArrayLike对象。其中数值元素的取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 新创建的ArkTS Uint8Array对象。 |
示例:
let arrayLike = [1, 3, 5]; let uint8Array: collections.Uint8Array = collections.Uint8Array.from(arrayLike); // Uint8Array [1, 3, 5]
static from<T>(arrayLike: ArrayLike<T>, mapFn: TypedArrayFromMapFn<T, number>): Uint8Array
从一个ArrayLike中创建一个ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| arrayLike | ArrayLike<T> | 是 | 用于构造ArkTS Uint8Array的ArrayLike对象。其中数值元素的取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
| mapFn | TypedArrayFromMapFn<T, number> | 是 | 映射函数将ArrayLike中的每个元素从类型T映射为number类型,用于创建ArkTS Uint8Array。其中数值元素的取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 新创建的ArkTS Uint8Array对象。 |
示例:
// 例1 从一个对象创建
let uint8Array: collections.Uint8Array = collections.Uint8Array.from<number>(
{ length: 5 }, (v: Object, k: number) => k);
// Uint8Array [0, 1, 2, 3, 4] // 例2 从一个字符数组创建 let uint8Array: collections.Uint8Array = collections.Uint8Array.from<string>( ["1", "3", "5"], (v: string, k: number) => parseInt(v)); // Uint8Array [1, 3, 5]
// 例3 从一个字符串创建 let uint8Array: collections.Uint8Array = collections.Uint8Array.from<string>( "12345", (v: string, k: number) => parseInt(v)); // Uint8Array [1, 2, 3, 4, 5]
static from(arrayLike: Iterable<number>, mapFn?: TypedArrayFromMapFn<number, number>): Uint8Array
从一个可迭代对象中创建一个ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| arrayLike | Iterable<number> | 是 | 用于构造ArkTS Uint8Array的可迭代对象。其中数值元素的取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
| mapFn | TypedArrayFromMapFn<number, number> | 否 | 映射函数。如果省略,则不对元素进行加工处理,直接使用原始数据创建Uint8Array。其中数值元素的取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 新创建的ArkTS Uint8Array对象。 |
示例:
// 例1 不指定映射函数 let set: Set<number> = new Set<number>([1, 2, 3]); let uint8Array: collections.Uint8Array = collections.Uint8Array.from(set); // Uint8Array [1, 2, 3]
// 例2 指定映射函数 let set: Set<number> = new Set<number>([1, 2, 3]); let uint8Array: collections.Uint8Array = collections.Uint8Array.from( set, (v: number, k: number) => v + k); // Uint8Array [1, 3, 5]
static of(...items: number[]): Uint8Array
通过可变数量的参数创建一个新的ArkTS Uint8Array对象。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| items | number[] | 否 | 用于创建Uint8Array的元素,参数个数可以是0个、1个或者多个,参数数值取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。默认值为空数组。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 新创建的ArkTS Uint8Array对象。 |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.of(1, 2, 3, 4); console.info(uint8Array.toString()); // 预期输出:1,2,3,4
toString(): string
ArkTS Uint8Array转换为字符串,返回一个由所有元素以逗号分隔拼接而成的字符串。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| string | 一个包含数组所有元素的字符串。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The toString method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array = new collections.Uint8Array([1, 2, 3, 4, 5]); let resultString = uint8Array.toString(); console.info(resultString); // 预期输出:1,2,3,4,5
toLocaleString(): string
根据当前应用的系统地区,将每个元素转换为本地化字符串,并使用逗号按顺序拼接成结果字符串。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| string | 一个包含数组所有元素的、根据系统地区格式化的字符串。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The toLocaleString method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
// 当前应用所在系统为法国地区 let uint8Array = new collections.Uint8Array([100, 110, 120]); let resultString = uint8Array.toLocaleString(); console.info(resultString); // 预期输出:100,110,120
copyWithin(target: number, start: number, end?: number): Uint8Array
将ArkTS Uint8Array指定范围内的元素依次拷贝到其自身buffer内目标位置,覆盖目标范围内原有数据,并返回修改后的ArkTS Uint8Array。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| target | number | 是 | 目标起始位置的下标,如果target < 0,则会从target + Uint8Array.length位置开始。 |
| start | number | 是 | 源起始位置下标,如果start < 0,则会从start + Uint8Array.length位置开始。 |
| end | number | 否 | 源终止位置下标(不包含end位置的元素),如果end < 0,则会从end + Uint8Array.length位置终止。默认为ArkTS Uint8Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 修改后的ArkTS Uint8Array。(原地修改,返回原对象引用) |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The copyWithin method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5, 6, 7, 8]); let copied: collections.Uint8Array = uint8Array.copyWithin(3, 1, 3); // Uint8Array [1, 2, 3, 2, 3, 6, 7, 8]
some(predicate: TypedArrayPredicateFn<number, Uint8Array>): boolean
测试ArkTS Uint8Array中是否存在元素满足指定条件,存在则返回true,否则返回false。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint8Array> | 是 | 用于测试的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 如果存在元素满足指定条件返回true,否则返回false。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The some method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let arrayLike = [10, 20, 30, 40, 50]; let uint8Array: collections.Uint8Array = new collections.Uint8Array(arrayLike); uint8Array.some((element: number) => element < 1); // false
every(predicate: TypedArrayPredicateFn<number, Uint8Array>): boolean
测试ArkTS Uint8Array中的所有元素是否满足指定条件,全部满足则返回true,否则返回false。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint8Array> | 是 | 用于测试的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 如果所有元素都满足指定条件则返回true,否则返回false。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The every method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let arrayLike = [10, 20, 30, 40, 50]; let uint8Array: collections.Uint8Array = new collections.Uint8Array(arrayLike); uint8Array.every((element: number) => element > 1); // true
fill(value: number, start?: number, end?: number): Uint8Array
使用特定值填充替换ArkTS Uint8Array指定范围的全部元素,并返回修改后的ArkTS Uint8Array。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| value | number | 是 | 待填充的值,取值范围为[0, 255],超出范围的值会被截断。 |
| start | number | 否 | 开始填充的索引,如果start < 0,则会从start + Uint8Array.length位置开始。默认值为0。 |
| end | number | 否 | 结束填充的索引(不包括该元素),如果end < 0,则会到end + Uint8Array.length位置结束。默认值为ArkTS Uint8Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 填充后的ArkTS Uint8Array(原地修改,返回原对象引用)。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The fill method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let arrayLike = [1, 2, 3]; new collections.Uint8Array(arrayLike).fill(4); // Uint8Array [4, 4, 4] new collections.Uint8Array(arrayLike).fill(4, 1); // Uint8Array [1, 4, 4] new collections.Uint8Array(arrayLike).fill(4, 1, 2); // Uint8Array [1, 4, 3]
filter(predicate: TypedArrayPredicateFn<number, Uint8Array>): Uint8Array
返回一个新ArkTS Uint8Array,其包含满足指定条件的所有元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint8Array> | 是 | 用于元素过滤的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 过滤后的ArkTS Uint8Array对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The filter method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([0, 1, 2, 3, 4]); let filtered: collections.Uint8Array = uint8Array.filter((element: number) => element % 2 == 0); // Uint8Array [0, 2, 4]
find(predicate: TypedArrayPredicateFn<number, Uint8Array>): number | undefined
返回ArkTS Uint8Array中第一个满足指定条件的元素的值,如果所有元素都不满足,则返回undefined。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint8Array> | 是 | 用于元素查找的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | undefined | 第一个满足条件的元素的值;如果所有元素都不满足条件,则返回undefined。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The find method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([0, 1, 2, 3, 4]); uint8Array.find((element: number) => element > 2); // 3 uint8Array.find((element: number) => element > 4); // undefined
findIndex(predicate: TypedArrayPredicateFn<number, Uint8Array>): number
返回ArkTS Uint8Array中第一个满足指定条件的元素索引,如果所有元素都不满足,则返回-1。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| predicate | TypedArrayPredicateFn<number, Uint8Array> | 是 | 用于元素查找的断言函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 第一个满足条件的元素索引;如果所有元素都不满足条件,则返回-1。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The findIndex method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
const uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); let foundIndex: number = uint8Array.findIndex((element: number) => element % 2 === 0); // 1
forEach(callbackFn: TypedArrayForEachCallback<number, Uint8Array>): void
对ArkTS Uint8Array中的每个元素按顺序执行提供的回调函数,该方法无返回值。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayForEachCallback<number, Uint8Array> | 是 | 用于对每个元素执行的回调函数。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The forEach method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3]);
uint8Array.forEach((value: number, index: number, array: collections.Uint8Array) => {
console.info(`Element ${value} at index ${index}`);
}); indexOf(searchElement: number, fromIndex?: number): number
返回在ArkTS Uint8Array中给定元素的第一个索引,如果该元素未找到,则返回-1。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| searchElement | number | 是 | 待查找索引的元素。 |
| fromIndex | number | 否 | 从前到后搜索的起始下标。默认值为0。如果fromIndex < 0,则会从fromIndex + Uint8Array.length位置开始从前到后搜索。如果fromIndex的值导致搜索区间和数组范围没有重叠,则返回-1。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 指定元素在数组指定范围中的第一个索引;没有找到,则返回-1。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The indexOf method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([3, 5, 9]); uint8Array.indexOf(3); // 0 uint8Array.indexOf(7); // -1 uint8Array.indexOf(9, 2); // 2 uint8Array.indexOf(9, -2); // 2
lastIndexOf(searchElement: number, fromIndex?: number): number
返回ArkTS Uint8Array实例中最后一次出现给定元素的索引,如果该元素未找到,则返回-1。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| searchElement | number | 是 | 待查找索引的元素。 |
| fromIndex | number | 否 | 从后到前搜索的起始下标。默认值为ArkTS Uint8Array的长度减1。如果fromIndex < 0,则会从fromIndex + Uint8Array.length位置开始从后向前搜索。如果fromIndex的值导致搜索区间和数组范围没有重叠,则返回-1。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 指定元素在数组指定范围中的最后一个索引;没有找到,则返回-1。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The lastIndexOf method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([3, 5, 9]);
console.info(`${uint8Array.lastIndexOf(3)}`); // 预期输出:0
console.info(`${uint8Array.lastIndexOf(7)}`); // 预期输出:-1
console.info(`${uint8Array.lastIndexOf(9, 2)}`); // 预期输出:2
console.info(`${uint8Array.lastIndexOf(9, -2)}`); // 预期输出:-1 join(separator?: string): string
将ArkTS Uint8Array的所有元素拼接成一个字符串,元素之间使用指定的分隔符分隔。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| separator | string | 否 | 分隔字符串。如果省略,则使用逗号分隔。 |
返回值:
| 类型 | 说明 |
|---|---|
| string | 包含所有元素拼接成的字符串。如果ArkTS Uint8Array为空,则返回空字符串。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The join method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]);
let joined: string = uint8Array.join('-'); // "1-2-3-4-5" map(callbackFn: TypedArrayMapCallback<number, Uint8Array>): Uint8Array
对ArkTS Uint8Array中的每个元素应用指定的回调函数,并使用结果创建一个新的ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayMapCallback<number, Uint8Array> | 是 | 回调函数,对ArkTS Uint8Array中的每个元素执行转换操作,返回值作为对应位置的新元素,返回值若超出取值范围[0, 255],会被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 新创建的包含回调函数转换结果的ArkTS Uint8Array对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The map method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([25, 36, 49]); const mapped: collections.Uint8Array = uint8Array.map(Math.sqrt); // Uint8Array [5, 6, 7]
reduce(callbackFn: TypedArrayReduceCallback<number, number, Uint8Array>): number
对ArkTS Uint8Array中的每个元素执行归约函数,并返回最终的归约结果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<number, number, Uint8Array> | 是 | 归约函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 由归约函数返回的结果。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduce method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); let reducedValue: number = uint8Array.reduce((accumulator: number, value: number) => accumulator + value); // reducedValue == 15
reduceRight(callbackFn: TypedArrayReduceCallback<number, number, Uint8Array>): number
反向遍历ArkTS Uint8Array,对ArkTS Uint8Array中的每个元素执行归约函数,并返回最终的归约结果。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<number, number, Uint8Array> | 是 | 归约函数。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 由归约函数返回的结果。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduceRight method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = uint8Array.reduceRight((accumulator: number, value: number) => accumulator + value);
console.info(`${reducedValue}`); // 预期输出:15 reduce(callbackFn: TypedArrayReduceCallback<number, number, Uint8Array>, initialValue: number): number
对ArkTS Uint8Array中的每个元素执行归约函数,接收初始值作为首次调用参数,并返回最终的归约结果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<number, number, Uint8Array> | 是 | 归约函数。 |
| initialValue | number | 是 | 初始值。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 由归约函数返回的结果。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduce method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); let reducedValue: number = uint8Array.reduce((accumulator: number, value: number) => accumulator + value, 1); // reducedValue == 16
reduceRight<U = number>(callbackFn: TypedArrayReduceCallback<U, number, Uint8Array>, initialValue: U): U
反向遍历ArkTS Uint8Array,对其中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。
元服务API: 从API version 18开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<U, number, Uint8Array> | 是 | 归约函数。 |
| initialValue | U | 是 | 初始值。 |
返回值:
| 类型 | 说明 |
|---|---|
| U | 由归约函数返回的结果。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduceRight method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = uint8Array.reduceRight((accumulator: number, value: number) => accumulator + value, 1);
console.info(`${reducedValue}`); // 预期输出:16 reduce<U>(callbackFn: TypedArrayReduceCallback<U, number, Uint8Array>, initialValue: U): U
对ArkTS Uint8Array中的每个元素执行归约函数,且接收一个初始值作为归约函数首次调用的参数,并返回最终的归约结果。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callbackFn | TypedArrayReduceCallback<U, number, Uint8Array> | 是 | 归约函数。 |
| initialValue | U | 是 | 初始值。 |
返回值:
| 类型 | 说明 |
|---|---|
| U | 由归约函数返回的结果。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reduce method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); let reducedValue: string = uint8Array.reduce<string>((accumulator: string, value: number) => accumulator + value, "initialValue"); // reducedValue == initialValue12345
reverse(): Uint8Array
反转ArkTS Uint8Array中元素的顺序,并返回反转后的ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 反转后的ArkTS Uint8Array对象(原地修改,返回原对象引用)。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The reverse method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); let reversed: collections.Uint8Array = uint8Array.reverse(); // Uint8Array [5, 4, 3, 2, 1]
set(array: ArrayLike<number>, offset?: number): void
将传入的ArrayLike元素依次写入到ArkTS Uint8Array中,从起始位置开始依次替换原有元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| array | ArrayLike<number> | 是 | 用于设置的ArrayLike对象。数值元素的取值范围为[0, 255],超出值被截断为低8位(例如256的二进制是100000000,截断后是00000000)。 |
| offset | number | 否 | 写入的起始位置,取值为非负整数,且offset与array.length之和不能超过ArkTS Uint8Array的长度,超出范围时将抛出异常。默认为0。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The set method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let buffer: collections.ArrayBuffer = new collections.ArrayBuffer(8); let uint8Array: collections.Uint8Array = new collections.Uint8Array(buffer); uint8Array.set([1, 2, 3], 3); // Uint8Array [0, 0, 0, 1, 2, 3, 0, 0]
slice(start?: number, end?: number): Uint8Array
返回一个新的ArkTS Uint8Array对象,其拷贝了原ArkTS Uint8Array指定范围的内容。和subarray不同,本方法生成的是独立副本,不会影响原ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| start | number | 否 | 开始索引,如果start < 0,则会从start + Uint8Array.length位置开始。默认为0。 |
| end | number | 否 | 结束索引(不包括该元素),如果end < 0,则会到end + Uint8Array.length位置结束。默认为ArkTS Uint8Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 包含原ArkTS Uint8Array指定范围元素的新的ArkTS Uint8Array对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The slice method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); uint8Array.slice(); // Uint8Array [1, 2, 3, 4, 5] uint8Array.slice(1, 3); // Uint8Array [2, 3] uint8Array.slice(-2); // Uint8Array [4, 5]
sort(compareFn?: TypedArrayCompareFn<number>): Uint8Array
对ArkTS Uint8Array进行排序,并返回排序后的ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| compareFn | TypedArrayCompareFn<number> | 否 | 用于确定元素顺序的函数。默认使用升序排序。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 排序后的ArkTS Uint8Array对象(原地排序,返回原对象引用)。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The sort method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 3, 5, 4, 2]); uint8Array.sort(); // Uint8Array [1, 2, 3, 4, 5] uint8Array.sort((a: number, b: number) => a - b); // Uint8Array [1, 2, 3, 4, 5] uint8Array.sort((a: number, b: number) => b - a); // Uint8Array [5, 4, 3, 2, 1]
subarray(begin?: number, end?: number): Uint8Array
从指定的位置截取数组,返回一个新的、基于相同ArkTS ArrayBuffer的ArkTS Uint8Array对象。和slice不同,本方法生成的不是独立副本,会影响原ArkTS Uint8Array对象。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| begin | number | 否 | 开始索引,如果begin < 0,则会从begin + Uint8Array.length位置开始。默认值为0。 |
| end | number | 否 | 结束索引(不包括该元素),如果end < 0,则会到end + Uint8Array.length位置结束。默认为ArkTS Uint8Array的长度。 |
返回值:
| 类型 | 说明 |
|---|---|
| Uint8Array | 基于原ArkTS Uint8Array的、从指定位置截取的新ArkTS Uint8Array对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The subarray method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]); let subArray: collections.Uint8Array = uint8Array.subarray(); // Uint8Array [1, 2, 3, 4, 5] subArray.set([10, 20, 30]); // Uint8Array [10, 20, 30, 4, 5]
at(index: number): number | undefined
返回指定下标的元素,如果不存在,则返回undefined。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| index | number | 是 | 要返回的Array元素的索引(从零开始),取值为整数。如果index < 0,则会访问index + Uint8Array.length位置的元素。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | undefined | 指定下标的元素;如果不存在,则返回undefined。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The at method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]);
console.info(`element: ${uint8Array.at(2)}`); // element: 3
console.info(`element: ${uint8Array.at(-1)}`); // element: 5
console.info(`element: ${uint8Array.at(6)}`); // element: undefined includes(searchElement: number, fromIndex?: number): boolean
判断ArkTS Uint8Array是否包含特定元素,包含则返回true,否则返回false。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| searchElement | number | 是 | 待搜索的元素。 |
| fromIndex | number | 否 | 开始搜索的索引,默认值为0。如果fromIndex < 0,则会从fromIndex + Uint8Array.length位置开始从前向后搜索。如果fromIndex的值导致搜索区间和数组范围没有重叠,则返回false。 |
返回值:
| 类型 | 说明 |
|---|---|
| boolean | 如果ArkTS Uint8Array包含指定的元素,则返回true;否则返回false。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The includes method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3]);
console.info(`includes: ${uint8Array.includes(2)}`); // includes: true
console.info(`includes: ${uint8Array.includes(4)}`); // includes: false
console.info(`includes: ${uint8Array.includes(3, 3)}`); // includes: false entries(): IterableIterator<[number, number]>
返回一个新的迭代器对象,该对象包含ArkTS Uint8Array中每个元素的键值对。迭代器遍历期间不能使用会改变ArkTS Uint8Array数组内容的方法。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<[number, number]> | 包含ArkTS Uint8Array中每个元素键值对的迭代器对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The entries method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([11, 22, 33]);
let iterator: IterableIterator<[number, number]> = uint8Array.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(): IterableIterator<number>
返回一个新的迭代器对象,该对象包含ArkTS Uint8Array中每个元素的键(下标)。迭代器遍历期间不能使用会改变ArkTS Uint8Array数组内容的方法。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<number> | 包含ArkTS Uint8Array中每个元素的键(下标)的迭代器对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The keys method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]);
let iterator: IterableIterator<number> = uint8Array.keys();
for (const key of iterator) {
console.info(`${key}`); // 依次输出 0,1,2,3,4
} values(): IterableIterator<number>
返回一个新的迭代器对象,该对象包含ArkTS Uint8Array中每个元素的值。迭代器遍历期间不能使用会改变ArkTS Uint8Array数组内容的方法。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<number> | 包含ArkTS Uint8Array中每个元素的值的迭代器对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The values method cannot be bound. |
| 10200201 | Concurrent modification error. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5]);
let iterator: IterableIterator<number> = uint8Array.values();
for (const value of iterator) {
console.info(`${value}`); // 依次输出 1,2,3,4,5
} [Symbol.iterator](): IterableIterator<number>
返回一个迭代器对象,用于遍历ArkTS Uint8Array中的每个元素值。迭代器遍历期间不能使用会改变ArkTS Uint8Array数组内容的方法。
本接口不支持在.ets文件中使用(和本文中其他迭代器方法不同,其他迭代器方法没有此限制)。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
返回值:
| 类型 | 说明 |
|---|---|
| IterableIterator<number> | 包含每个元素值的迭代器对象。 |
错误码:
以下错误码详细介绍请参考语言基础类库错误码。
| 错误码ID | 错误信息 |
|---|---|
| 10200011 | The Symbol.iterator method cannot be bound. |
示例:
let uint8Array: collections.Uint8Array = collections.Uint8Array.from([1, 2, 3, 4, 5, 6]);
for (let item of uint8Array) {
console.info(`value : ${item}`);
} [index: number]: number
返回ArkTS Uint8Array指定索引位置的元素。
元服务API: 从API version 12开始,该接口支持在元服务中使用。
系统能力: SystemCapability.Utils.Lang
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| index | number | 是 | 所需元素的从零开始的索引,取值范围为0到Uint8Array.length-1的整数。超出范围时或不为整数时返回undefined。 |
返回值:
| 类型 | 说明 |
|---|---|
| number | 指定索引位置的元素。索引超过数组范围则返回undefined。 |
示例:
let uint8Array = collections.Uint8Array.from([1, 2, 4]);
console.info(`Element at index 1: ${uint8Array[1]}`);