目录

Class (Float32Array)

一种线性数据结构,底层基于ArrayBuffer实现。

本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

此模块仅支持在ArkTS文件(文件后缀为.ets)中导入使用。

装饰器类型:@Sendable

导入模块

import { collections } from '@kit.ArkTS';

属性

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

系统能力: SystemCapability.Utils.Lang

名称 类型 只读 可选 说明
buffer ArrayBuffer 是 否 ArkTS Float32Array底层使用的buffer。
byteLength number 是 否 ArkTS Float32Array所占的字节数。
byteOffset number 是 否 ArkTS Float32Array距离其ArrayBuffer起始位置的字节偏移。
length number 是 否 ArkTS Float32Array元素个数。
BYTES_PER_ELEMENT number 是 否 ArkTS Float32Array中每个元素所占的字节数。

constructor

constructor()

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

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

系统能力: SystemCapability.Utils.Lang

错误码:

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

错误码ID 错误信息
10200012 The Float32Array's constructor cannot be directly invoked.

示例:

// 创建空的Float32Array对象
let float32Array: collections.Float32Array = new collections.Float32Array();

constructor

constructor(length: number)

构造函数,用于创建一个指定长度的ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
length number 是 用于指定ArkTS Float32Array的长度,取值需为非负整数。

错误码:

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

错误码ID 错误信息
10200012 The Float32Array's constructor cannot be directly invoked.

示例:

// 以长度参数构造对象
let float32Array: collections.Float32Array = new collections.Float32Array(12);

constructor

constructor(elements: Iterable<number>)

构造函数,以Iterable创建一个ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
elements Iterable<number> 是 可迭代数字集合,用于构造ArkTS Float32Array对象。

错误码:

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

错误码ID 错误信息
10200012 The Float32Array's constructor cannot be directly invoked.

示例:

// 从一个Iterable构造对象,Float32Array [1, 2, 3]
let set: Set<number> = new Set<number>([1, 2, 3]);
let float32Array: collections.Float32Array = new collections.Float32Array(set);

constructor

constructor(array: ArrayLike<number> | ArrayBuffer)

构造函数,以ArrayLike或ArkTS ArrayBuffer创建一个ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

错误码:

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

错误码ID 错误信息
10200012 The Float32Array's constructor cannot be directly invoked.

示例:

// 例1 从一个ArrayLike构造对象
let arrayLike = [1, 3, 5];
let float32Array: collections.Float32Array = new collections.Float32Array(arrayLike);
// 例2 从一个ArrayBuffer构造对象
let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12);
let float32Array: collections.Float32Array = new collections.Float32Array(arrayBuffer);
// 例3 从另一ArkTS Float32Array构造对象
let arrayLike = [1, 3, 5];
// float32Array1 [1, 3, 5]
let float32Array1: collections.Float32Array = new collections.Float32Array(arrayLike);
// float32Array2 [1, 3, 5]
let float32Array2: collections.Float32Array = new collections.Float32Array(float32Array1);

constructor

constructor(buffer: ArrayBuffer, byteOffset?: number, length?: number)

构造函数,以ArkTS ArrayBuffer创建一个ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
buffer ArrayBuffer 是 用于构造ArkTS Float32Array的ArrayBuffer对象。buffer所占的字节数需是4的整数倍。
byteOffset number 否 指定buffer的字节偏移,从0开始,默认值为0。byteOffset需为4的整数倍。
length number 否 指定ArkTS Float32Array的长度,默认值为0。取值需为非负整数,且需满足byteOffset + length * 4 <= buffer.byteLength。

错误码:

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

错误码ID 错误信息
10200012 The Float32Array's constructor cannot be directly invoked.

示例:

let float32Array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5, 6]);
console.info("byteLength: " + float32Array.buffer.byteLength); // byteLength: 24
// 从float32Array对应buffer第4个字节开始,长度为5
let float32Array1: collections.Float32Array = new collections.Float32Array(float32Array.buffer, 4, 5);
console.info("[" + float32Array1 + "]"); // [2, 3, 4, 5, 6]

from

static from(arrayLike: ArrayLike<number>): Float32Array

从一个ArrayLike中创建一个ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
arrayLike ArrayLike<number> 是 用于构造ArkTS Float32Array的ArrayLike对象。

返回值:

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

示例:

let arrayLike = [1, 3, 5];
let array: collections.Float32Array = collections.Float32Array.from(arrayLike); // array [1, 3, 5]

from

static from<T>(arrayLike: ArrayLike<T>, mapFn: TypedArrayFromMapFn<T, number>): Float32Array

从一个ArrayLike中创建一个ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
arrayLike ArrayLike<T> 是 用于构造ArkTS Float32Array的ArrayLike对象。
mapFn TypedArrayFromMapFn<T, number> 是 映射函数。

返回值:

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

示例:

// 例1 从一个对象创建,Float32Array [0, 1, 2, 3, 4]
let array: collections.Float32Array = collections.Float32Array.from<number>(
  { length: 5 }, (v: Object, k: number) => k);
// 例2 从一个字符数组创建,Float32Array [1, 3, 5]
let array: collections.Float32Array = collections.Float32Array.from<string>(
  ["1", "3", "5"], (v: string, k: number) => parseInt(v));
// 例3 从一个字符串创建,Float32Array [1, 2, 3, 4, 5]
let array: collections.Float32Array = collections.Float32Array.from<string>(
  "12345", (v: string, k: number) => parseInt(v));

from

static from(arrayLike: Iterable<number>, mapFn?: TypedArrayFromMapFn<number, number>): Float32Array

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

示例:

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

of18+

static of(...items: number[]): Float32Array

通过可变数量的参数创建一个新的ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
items number[] 否 用于创建数组的元素,参数个数可变。默认值为空数组。

返回值:

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

示例:

// 通过可变参数创建Float32Array对象
let float32Array: collections.Float32Array = collections.Float32Array.of(1, 2, 3, 4);
console.info(float32Array.toString()); // 预期输出:1,2,3,4

toString18+

toString(): string

将ArkTS Float32Array转换为字符串,各元素以逗号分隔拼接。

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
string 一个包含数组所有元素的字符串,各元素以逗号分隔。

错误码:

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

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

示例:

let float32Array = new collections.Float32Array([1, 2, 3, 4, 5]);
let stringArray = float32Array.toString(); // 将Float32Array转换为字符串
console.info(stringArray); // 预期输出:1,2,3,4,5

toLocaleString18+

toLocaleString(): string

根据当前应用的系统地区获取符合当前文化习惯的数字表示形式。每个元素调用自己的toLocaleString方法将数字转换为字符串,然后使用逗号按顺序将各元素的结果拼接为字符串。

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
string 一个包含数组所有元素的字符串,各元素以本地化格式表示并以逗号分隔。

错误码:

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

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

示例:

// 当前应用所在系统为法国地区
let array = new collections.Float32Array([1000, 2000, 3000]);
let stringArray = array.toLocaleString();
console.info(stringArray); // 预期输出:1 000,2 000,3 000

copyWithin

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
target number 是 目标起始位置的索引,如果target < 0,则会从target + Float32Array.length位置开始。
start number 是 源起始位置索引,如果start < 0,则会从start + Float32Array.length位置开始。
end number 否 源终止位置索引(不包含end位置的元素),如果end < 0,则会从end + Float32Array.length位置终止。默认为ArkTS Float32Array的长度。

返回值:

类型 说明
Float32Array 修改后的ArkTS Float32Array。

错误码:

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

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

示例:

let float32Array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5, 6, 7, 8]);
// Float32Array [1, 2, 3, 2, 3, 6, 7, 8]
let copied: collections.Float32Array = float32Array.copyWithin(3, 1, 3);

some

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let arrayLike = [-10, 20, -30, 40, -50];
let float32Array: collections.Float32Array = new collections.Float32Array(arrayLike);
// 测试是否存在小于0的元素
float32Array.some((element: number) => element < 0); // true

every

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let arrayLike = [-10, 20, -30, 40, -50];
let float32Array: collections.Float32Array = new collections.Float32Array(arrayLike);
// 判断是否所有元素都大于0
float32Array.every((element: number) => element > 0);  // false

fill

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

类型 说明
Float32Array 填充后的ArkTS Float32Array。

错误码:

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

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

示例:

let arrayLike = [1, 2, 3];
// 使用4填充所有元素
new collections.Float32Array(arrayLike).fill(4); // Float32Array [4, 4, 4]
// 从索引1开始填充4
new collections.Float32Array(arrayLike).fill(4, 1); // Float32Array [1, 4, 4]
// 从索引1到2(不含2)填充4
new collections.Float32Array(arrayLike).fill(4, 1, 2); // Float32Array [1, 4, 3]

filter

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([0, 1, 2, 3, 4]);
// Float32Array [0, 2, 4]
let filtered: collections.Float32Array = array.filter((element: number) => element % 2 === 0);

find

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.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, Float32Array>): number

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

const array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let foundIndex: number = array.findIndex((element: number) => element % 2 === 0); // 1

forEach

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

错误码:

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

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

示例:

let float32Array: collections.Float32Array = collections.Float32Array.from([1, 2, 3]);
// 遍历Float32Array中的每个元素
float32Array.forEach((value: number, index: number, array: collections.Float32Array) => {
  console.info("Element " + value + " at index " + index);
});

indexOf

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
searchElement number 是 待查找的元素值。
fromIndex number 否 搜索的起始索引。默认值为0。如果索引大于等于ArkTS Float32Array的长度,则返回-1。如果索引为负数,则被视为距离数组尾部的偏移,从前到后搜索。

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([3, 5, 9]);
array.indexOf(3); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(9, -2); // 2

lastIndexOf18+

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

返回ArkTS Float32Array实例中最后一次出现searchElement的索引,如果不存在,则返回-1。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
searchElement number 是 待查找的元素值。
fromIndex number 否 搜索的起始索引,从该位置开始往前查找。默认值为ArkTS Float32Array长度减1(即从末尾开始)。如果索引大于等于ArkTS Float32Array的长度,则从末尾开始往前查找。如果提供的索引值是负数,则被当作距离数组尾部的偏移,从后到前搜索。

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([3, 5, 9]);
console.info(array.lastIndexOf(3) + ''); // 预期输出:0
console.info(array.lastIndexOf(7) + ''); // 预期输出:-1
console.info(array.lastIndexOf(9, 2) + ''); // 预期输出:2
console.info(array.lastIndexOf(9, -2) + ''); // 预期输出:-1

join

join(separator?: string): string

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let joined: string = array.join('-'); // "1-2-3-4-5"

map

map(callbackFn: TypedArrayMapCallback<number, Float32Array>): Float32Array

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

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
callbackFn TypedArrayMapCallback<number, Float32Array> 是 回调函数。

返回值:

类型 说明
Float32Array 包含回调函数处理后结果的新ArkTS Float32Array对象。

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([25, 36, 49]);
const mapped: collections.Float32Array = array.map(Math.sqrt); // Float32Array [5, 6, 7]

reduce

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value);
// reducedValue == 15

reduceRight18+

reduceRight(callbackFn: TypedArrayReduceCallback<number, number, Float32Array>): number

反向遍历ArkTS Float32Array,对每个元素执行归约函数,并返回最终的归约结果。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value);
console.info(reducedValue + ''); // 预期输出:15

reduce

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
callbackFn TypedArrayReduceCallback<U, number, Float32Array> 是 归约函数。
initialValue U 是 归约函数首次调用时的初始累加值。

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
// reducedValue == 16
let reducedValue: number = array.reduce((accumulator: number, value: number) => accumulator + value, 1);

reduceRight18+

reduceRight<U = number>(callbackFn: TypedArrayReduceCallback<U, number, Float32Array>, initialValue: U): U

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

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
callbackFn TypedArrayReduceCallback<U, number, Float32Array> 是 归约函数。
initialValue U 是 归约函数首次调用时的初始累加值。

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value, 1);
console.info(reducedValue + ''); // 预期输出:16

reverse

reverse(): Float32Array

反转ArkTS Float32Array中元素的顺序。

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

系统能力: SystemCapability.Utils.Lang

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let reversed: collections.Float32Array = array.reverse(); // Float32Array [5, 4, 3, 2, 1]

set

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

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

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
array ArrayLike<number> 是 用于设置的ArrayLike对象。
offset number 否 写入的起始位置。默认值为0。取值为非负整数,且offset + array.length不超过当前Float32Array的长度。

错误码:

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

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

示例:

let buffer: collections.ArrayBuffer = new collections.ArrayBuffer(32);
let array: collections.Float32Array = new collections.Float32Array(buffer);
array.set([1, 2, 3], 3); // Float32Array [0, 0, 0, 1, 2, 3, 0, 0]

slice

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

返回一个新的ArkTS Float32Array对象,其包含原ArkTS Float32Array指定范围的内容。与subarray不同,slice返回的对象不会影响原数组。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

类型 说明
Float32Array 包含原ArkTS Float32Array指定范围元素的新的ArkTS Float32Array对象。

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
array.slice(); // Float32Array [1, 2, 3, 4, 5]
array.slice(1, 3); // Float32Array [2, 3]
array.slice(-2); // Float32Array [4, 5]

sort

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

对ArkTS Float32Array进行原地排序,并返回排序后的ArkTS Float32Array对象。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 3, 5, 4, 2]);
// 默认升序排序
array.sort(); // Float32Array [1, 2, 3, 4, 5]
// 使用比较函数升序排序
array.sort((first: number, second: number) => first - second); // Float32Array [1, 2, 3, 4, 5]
// 使用比较函数降序排序
array.sort((first: number, second: number) => second - first); // Float32Array [5, 4, 3, 2, 1]

subarray

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

从指定的位置截取数组,返回一个基于相同ArkTS ArrayBuffer的新ArkTS Float32Array对象。由于返回的对象与原始对象共享同一个ArrayBuffer,修改其中一个会影响另一个。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

类型 说明
Float32Array 基于相同ArrayBuffer的新ArkTS Float32Array对象。

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
let subArray: collections.Float32Array = array.subarray(); // Float32Array [1, 2, 3, 4, 5]
subArray.set([10, 20, 30]); // Float32Array [10, 20, 30, 4, 5]

at

at(index: number): number | undefined

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

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let float32Array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5]);
console.info("element: " + float32Array.at(2));  // element: 3
console.info("element: " + float32Array.at(-1)); // element: 5
console.info("element: " + float32Array.at(6));  // element: undefined

includes

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

判断ArkTS Float32Array是否包含指定元素。

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

系统能力: SystemCapability.Utils.Lang

参数:

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

返回值:

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

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.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 Float32Array中每个元素的键值对。

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<[number, number]> 包含ArkTS Float32Array中每个元素键值对的迭代器对象。

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.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 Float32Array中每个元素的键(索引)。

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<number> 包含ArkTS Float32Array中每个元素的键(索引)的迭代器对象。

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.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 Float32Array中每个元素的值。

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<number> 包含ArkTS Float32Array中每个元素值的迭代器对象。

错误码:

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

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

示例:

let array: collections.Float32Array = collections.Float32Array.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
}

[Symbol.iterator]

[Symbol.iterator](): IterableIterator<number>

返回包含ArkTS Float32Array中每个元素的迭代器。

本接口不支持在.ets文件中使用。

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

系统能力: SystemCapability.Utils.Lang

返回值:

类型 说明
IterableIterator<number> 包含ArkTS Float32Array中每个元素值的迭代器对象。

错误码:

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

错误码ID 错误信息
10200011 The Symbol.iterator method cannot be bound.

示例:

let float32Array: collections.Float32Array = collections.Float32Array.from([1, 2, 3, 4, 5, 6]);
// 通过迭代器遍历Float32Array
for (let item of float32Array) {
  console.info("value : " + item);
}

[index: number]

[index: number]: number

返回ArkTS Float32Array指定索引位置的元素。

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

系统能力: SystemCapability.Utils.Lang

参数:

参数名 类型 必填 说明
index number 是 从0开始的元素索引。取值范围为[0, Float32Array.length - 1]。

返回值:

类型 说明
number 返回指定索引位置的元素值。

示例:

let float32Array = collections.Float32Array.from([1, 2, 4]);
// 通过索引访问Float32Array中的元素
console.info("Element at index 1: ", float32Array[1]);