openharmony 鸿蒙 arkts-apis-arkts-collections-Int8Array

2026-08-25 浏览 (1)

Class (Int8Array)

A linear data structure that is implemented on ArkTS ArrayBuffer.

NOTE

  • The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • This module can be imported only to ArkTS files (with the file name extension .ets).

Decorator: @Sendable

Modules to Import

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

Properties

System capability: SystemCapability.Utils.Lang

Atomic service API: This API can be used in atomic services since API version 12.

NameTypeRead OnlyOptionalDescription
bufferArrayBufferYesNoBottom-layer buffer used by an ArkTS Int8Array.
byteLengthnumberYesNoNumber of bytes occupied by an ArkTS Int8Array.
byteOffsetnumberYesNoOffset between the ArkTS Int8Array and the start position of the ArrayBuffer.
lengthnumberYesNoNumber of elements in an ArkTS Int8Array.
BYTES_PER_ELEMENTnumberYesNoNumber of bytes occupied by each element in the ArkTS Int8Array.

constructor

constructor()

A constructor used to create an empty ArkTS Int8Array.

System capability: SystemCapability.Utils.Lang

Atomic service API: This API can be used in atomic services since API version 12.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200012The Int8Array's constructor cannot be directly invoked.

Example

let int8Array: collections.Int8Array = new collections.Int8Array();

constructor

constructor(length: number)

A constructor used to create an ArkTS Int8Array of a given length.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
lengthnumberYesLength of the ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200012The Int8Array's constructor cannot be directly invoked.

Example

// Construct an object with the length parameter.
let int8Array: collections.Int8Array = new collections.Int8Array(12);

constructor

constructor(array: ArrayLike<number>|ArrayBuffer)

A constructor that creates an ArkTS Int8Array from an array-like object or ArkTS ArrayBuffer.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
arrayArrayLike<number> |ArrayBufferYesObject used to construct the ArkTS Int8Array. When the parameter type is ArrayBuffer, the number of bytes occupied by the buffer must be an integer multiple of 4.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200012The Int8Array's constructor cannot be directly invoked.

Example

// Example 1: Construct an object from an array-like object.
let arrayLike = [1, 3, 5];
let array: collections.Int8Array = new collections.Int8Array(arrayLike);
// Example 2: Construct an object from an ArrayBuffer.
let arrayBuffer: collections.ArrayBuffer = new collections.ArrayBuffer(12);
let array: collections.Int8Array = new collections.Int8Array(arrayBuffer);
// Example 3: Construct an object from another ArkTS Int8Array.
let arrayLike = [1, 3, 5];
// int8Array1 [1, 3, 5]
let int8Array1: collections.Int8Array = new collections.Int8Array(arrayLike);
// int8Array2 [1, 3, 5]
let int8Array2: collections.Int8Array = new collections.Int8Array(int8Array1);

constructor

constructor(elements: Iterable<number>)

A constructor that creates an ArkTS Int8Array from an iterable object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementsIterable<number>YesAn iterable collection of numbers used to construct an ArkTS Int8Array object.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200012The Int8Array's constructor cannot be directly invoked.

Example

// Construct objects from an iterable object.
let set: Set<number> = new Set<number>([1, 2, 3]);
let array: collections.Int8Array = new collections.Int8Array(set);
// Int8Array [1, 2, 3]

constructor

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

A constructor that creates an ArkTS Int8Array from an ArrayBuffer.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
bufferArrayBufferYesArrayBuffer object used to construct the ArkTS Int8Array. The number of bytes occupied by the buffer must be an integer multiple of 4.
byteOffsetnumberNoByte offset of the buffer, beginning at 0. The default value is 0.
lengthnumberNoLength of the ArkTS Int8Array. The default value is 0.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200012The Int8Array's constructor cannot be directly invoked.

Example

let int8Array: collections.Int8Array = collections.Int8Array.from([1, 2, 3, 4, 5, 6]);
console.info("byteLength: " + int8Array.buffer.byteLength); // byteLength: 6
// Start from the first byte of the buffer corresponding to int8Array. The length is 5.
let int8Array1: collections.Int8Array = new collections.Int8Array(int8Array.buffer, 1, 5);
console.info("[" + int8Array1 + "]"); // [2, 3, 4, 5, 6]

from

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

Creates an ArkTS Int8Array from an array-like or iterator object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
arrayLikeArrayLike<number>YesArray-like object used to construct the ArkTS Int8Array.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array generated.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

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

from

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

Creates an ArkTS Int8Array from an array-like object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
arrayLikeArrayLike<T>YesArray-like object used to construct the ArkTS Int8Array.
mapFnTypedArrayFromMapFn<T, number>YesMapping function.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array generated.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

// Example 1: Create an ArkTS Int8Array from an object.
let array: collections.Int8Array = collections.Int8Array.from<number>(
  { length: 5 }, (v: Object, k: number) => k);
// Int8Array [0, 1, 2, 3, 4]
// Example 2: Create an ArkTS Int8Array from a string array.
let array: collections.Int8Array = collections.Int8Array.from<string>(
  ["1", "3", "5"], (v: string, k: number) => parseInt(v));
// Int8Array [1, 3, 5]
// Example 3: Create an ArkTS Int8Array from a string.
let array: collections.Int8Array = collections.Int8Array.from<string>(
  "12345", (v: string, k: number) => parseInt(v));
// Int8Array [1, 2, 3, 4, 5]

from

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

Creates an ArkTS Int8Array from an iterator object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
arrayLikeIterable<number>YesIterator object used to construct the ArkTS Int8Array.
mapFnTypedArrayFromMapFn<number, number>NoMapping function. If no value is passed in, no special processing is conducted on the elements.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array generated.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.

Example

// Example 1: No mapping function is specified.
let set: Set<number> = new Set<number>([1, 2, 3]);
let array: collections.Int8Array = collections.Int8Array.from(set);
// Int8Array [1, 2, 3]
// Example 2: A mapping function is specified.
let set: Set<number> = new Set<number>([1, 2, 3]);
let array: collections.Int8Array = collections.Int8Array.from(
  set, (v: number, k: number) => v + k);
// Int8Array [1, 3, 5]

of18+

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

Creates an ArkTS Int8Array with a variable number of parameters.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
itemsnumber[]NoArray of elements used to create the array. The number of elements can be zero, one, or more.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array instance.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error: Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types;3. Parameter verification failed.

Example

let arr: collections.Int8Array = collections.Int8Array.of(1, 2, 3, 4);
console.info(arr.toString()); // Expected output: 1,2,3,4

toString18+

toString(): string

Converts an ArkTS Int8Array into a string.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
stringA string that contains all elements of the array.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The toString method cannot be bound.
10200201Concurrent modification error.

Example

let array = new collections.Int8Array([1, 2, 3, 4, 5]);
let stringArray = array.toString();
console.info(stringArray); // Expected output: 1,2,3,4,5

toLocaleString18+

toLocaleString(): string

Generates a string of digits that matches the cultural conventions of the current system locale. Each element converts its digits to a string via its toLocaleString API, and these strings are then joined in sequence with commas (,).

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
stringA string that contains all elements of the array.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The toLocaleString method cannot be bound.
10200201Concurrent modification error.

Example

// The system where the application is running is set to the French locale.
let array = new collections.Int8Array([100, 110, 120]);
let stringArray = array.toLocaleString();
console.info(stringArray); // Expected output: 100,110,120

copyWithin

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

Copies elements within a given range from this ArkTS Int8Array to another position in sequence.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
targetnumberYesStart index of the range. If a negative number is passed in, it refers to the index of target + array.length.
startnumberYesStart index of the range. If a negative number is passed in, it refers to the index of start + Int8Array.length.
endnumberNoEnd index of the range (exclusive). If a negative number is passed in, it refers to the index of end + Int8Array.length. The default value is the length of the ArkTS Int8Array.

Return value

TypeDescription
Int8ArrayArkTS Int8Array after being modified.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The copyWithin method cannot be bound.
10200201Concurrent modification error.

Example

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

some

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

Checks whether any element in this ArkTS Int8Array meets a given condition.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
predicateTypedArrayPredicateFn<number, Int8Array>YesAssertion function used for the test.

Return value

TypeDescription
booleanCheck result. The value true is returned if an element meeting the given condition exists; otherwise, false is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The some method cannot be bound.
10200201Concurrent modification error.

Example

let arrayLike = [-10, 20, -30, 40, -50];
let int8Array: collections.Int8Array = new collections.Int8Array(arrayLike);
int8Array.some((element: number) => element < 0); // true

every

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

Checks whether all elements in this ArkTS Int8Array meet a given condition.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
predicateTypedArrayPredicateFn<number, Int8Array>YesAssertion function used for the test.

Return value

TypeDescription
booleanCheck result. The value true is returned if all elements meet the given condition; otherwise, false is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The every method cannot be bound.
10200201Concurrent modification error.

Example

let arrayLike = [-10, 20, -30, 40, -50];
let int8Array: collections.Int8Array = new collections.Int8Array(arrayLike);
int8Array.every((element: number) => element > 0);  // false

fill

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

Fills all elements in a given range in this ArkTS Int8Array with a value.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
valuenumberYesValue to fill in.
startnumberNoStart index of the range. If a negative number is passed in, it refers to the index of start + Int8Array.length. The default value is 0.
endnumberNoEnd index of the range (exclusive). If a negative number is passed in, it refers to the index of end + Int8Array.length. The default value is the length of the ArkTS Int8Array.

Return value

TypeDescription
Int8ArrayFilled ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The fill method cannot be bound.
10200201Concurrent modification error.

Example

let arrayLike = [1, 2, 3];
new collections.Int8Array(arrayLike).fill(4); // Int8Array [4, 4, 4]
new collections.Int8Array(arrayLike).fill(4, 1); // Int8Array [1, 4, 4]
new collections.Int8Array(arrayLike).fill(4, 1, 2); // Int8Array [1, 4, 3]

filter

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

Returns a new ArkTS Int8Array that contains all elements that meet the given condition.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
predicateTypedArrayPredicateFn<number, Int8Array>YesAssertion function used for the test.

Return value

TypeDescription
Int8ArrayFiltered ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The filter method cannot be bound.
10200201Concurrent modification error.

Example

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

find

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

Returns the value of the first element that passes a test provided by a callback function. If none of the elements pass the test, undefined is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
predicateTypedArrayPredicateFn<number, Int8Array>YesAssertion function used for the test.

Return value

TypeDescription
number |undefinedValue of the first element that passes the test. If none of the elements pass the test, undefined is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The find method cannot be bound.
10200201Concurrent modification error.

Example

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

Returns the index of the first element that passes a test provided by a callback function. If none of the elements pass the test, -1 is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
predicateTypedArrayPredicateFn<number, Int8Array>YesAssertion function used for the test.

Return value

TypeDescription
numberIndex of the first element that passes the test. If none of the elements pass the test, -1 is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The findIndex method cannot be bound.
10200201Concurrent modification error.

Example

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

forEach

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

Calls a callback function for each element in this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayForEachCallback<number, Int8Array>YesCallback function to run for each element.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The forEach method cannot be bound.
10200201Concurrent modification error.

Example

let int8Array: collections.Int8Array = collections.Int8Array.from([1, 2, 3]);
int8Array.forEach((value: number, index: number, array: collections.Int8Array) => {
  console.info(`Element ${value} at index ${index}`);
});

indexOf

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

Returns the index of the first occurrence of a value in this ArkTS Int8Array. If the value is not found, -1 is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
searchElementnumberYesValue to search for.
fromIndexnumberNoIndex from which the search starts. The default value is 0. If the index is greater than or equal to the length of the ArkTS Int8Array, -1 is returned. If a negative number is passed in, the search starts from the end of the ArkTS Int8Array.

Return value

TypeDescription
numberIndex of the first occurrence of the value. If the value is not found, -1 is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The indexOf method cannot be bound.
10200201Concurrent modification error.

Example

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

Obtains the index of the last occurrence of the specified value in this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
searchElementnumberYesValue to search for.
fromIndexnumberNoIndex from which the search starts. The default value is 0. If the index is greater than or equal to the length of the ArkTS Int8Array, -1 is returned. If a negative number is passed in, the search starts from the end of the ArkTS Int8Array.

Return value

TypeDescription
numberIndex of the last occurrence of the value. If the value is not found, -1 is returned.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The lastIndexOf method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.from([3, 5, 9]);
console.info(array.lastIndexOf(3) + ''); // Expected output: 0
console.info(array.lastIndexOf(7) + ''); // Expected output: -1
console.info(array.lastIndexOf(9, 2) + ''); // Expected output: 2
console.info(array.lastIndexOf(9, -2) + ''); // Expected output: -1

join

join(separator?: string): string

Concatenates all elements in this ArkTS Int8Array into a string, with a given separator.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
separatorstringNoSeparator to be used. If no value is passed in, a comma (,) is used as the separator.

Return value

TypeDescription
stringString obtained. If the array is empty, an empty string is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The join method cannot be bound.
10200201Concurrent modification error.

Example

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

map

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

Applies a callback function to each element in this ArkTS Int8Array and uses the result to create an ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayMapCallback<number, Int8Array>YesCallback function to run for each element.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The map method cannot be bound.
10200201Concurrent modification error.

Example

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

reduce

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

Applies a reduce function on each element in this ArkTS Int8Array and returns the final reduction result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayReduceCallback<number, number, Int8Array>YesReduce function.

Return value

TypeDescription
numberFinal result obtained from the last call of the reduce function.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The reduce method cannot be bound.
10200201Concurrent modification error.

Example

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

Reversely traverses this ArkTS Int8Array, applies a reduce function on each element in the array, and returns the final reduction result.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayReduceCallback<number, number, Int8Array>YesReduce function.

Return value

TypeDescription
numberFinal result obtained from the last call of the reduce function.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
10200011The reduceRight method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value);
console.info(reducedValue + ''); // Expected output: 15

reduce

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

Applies a reduce function for each element in this ArkTS Int8Array, receives an initial value as the parameter called by the reduce function for the first time, and returns the final reduction result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayReduceCallback<number, number, Int8Array>YesReduce function.
initialValuenumberYesInitial value.

Return value

TypeDescription
numberFinal result obtained from the last call of the reduce function.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The reduce method cannot be bound.
10200201Concurrent modification error.

Example

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

reduceRight18+

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

Reversely traverses this ArkTS Int8Array, applies a reduce function for each element in the array, receives an initial value as the parameter called by the reduce function for the first time, and returns the final reduction result.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayReduceCallback<U, number, Int8Array>YesReduce function.
initialValueUYesInitial value.

Return value

TypeDescription
UFinal result obtained from the last call of the reduce function.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types.
10200011The reduceRight method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.from([1, 2, 3, 4, 5]);
let reducedValue: number = array.reduceRight((accumulator: number, value: number) => accumulator + value, 1);
console.info(reducedValue + ''); // Expected output: 16

reduce

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

Applies a reduce function for each element in this ArkTS Int8Array, receives an initial value as the parameter called by the reduce function for the first time, and returns the final reduction result.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnTypedArrayReduceCallback<U, number, Int8Array>YesReduce function.
initialValueUYesInitial value.

Return value

TypeDescription
UFinal result obtained from the last call of the reduce function.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The reduce method cannot be bound.
10200201Concurrent modification error.

Example

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

reverse

reverse(): Int8Array

Reverses this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
Int8ArrayReversed ArkTS Int8Array.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The reverse method cannot be bound.
10200201Concurrent modification error.

Example

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

set

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

Writes the elements in an array-like object to the given start position in sequence.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
arrayArrayLike<number>YesArray-like object whose elements will be written.
offsetnumberNoStart position for writing data. The default value is 0.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The set method cannot be bound.
10200201Concurrent modification error.

Example

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

slice

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

Selects a range of elements in this ArkTS Int8Array to create an ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
startnumberNoStart index of the range. If a negative number is passed in, it refers to the index of start + Int8Array.length. The default value is 0.
endnumberNoEnd index of the range (exclusive). If a negative number is passed in, it refers to the index of end + Int8Array.length. The default value is the length of the ArkTS Int8Array.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The slice method cannot be bound.
10200201Concurrent modification error.

Example

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

sort

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

Sorts elements in this ArkTS Int8Array and returns the sorted ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
compareFnTypedArrayCompareFn<number>NoFunction that determines the sort order. By default, elements are sorted in ascending order.

Return value

TypeDescription
Int8ArraySorted ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The sort method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.from([1, 3, 5, 4, 2]);
array.sort(); // Int8Array [1, 2, 3, 4, 5]
array.sort((a: number, b: number) => a - b); // Int8Array [1, 2, 3, 4, 5]
array.sort((a: number, b: number) => b - a); // Int8Array [5, 4, 3, 2, 1]

subarray

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

Truncates an array from a specified position and returns a new ArkTS Int8Array based on the same ArkTS ArrayBuffer.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
beginnumberNoStart index of the range. If a negative number is passed in, it refers to the index of begin + Int8Array.length. The default value is 0.
endnumberNoEnd index of the range (exclusive). If a negative number is passed in, it refers to the index of end + Int8Array.length. The default value is the length of the ArkTS Int8Array.

Return value

TypeDescription
Int8ArrayNew ArkTS Int8Array.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The subarray method cannot be bound.
10200201Concurrent modification error.

Example

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

at

at(index: number): number|undefined

Returns the element at the given index. If no element is found, undefined is returned.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex of the element. The index in an array always starts from 0 and is an integer. If a negative number is passed in, it refers to the index of index + Int8Array.length.

Return value

TypeDescription
number |undefinedElement obtained. If no element is found, undefined is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The at method cannot be bound.
10200201Concurrent modification error.

Example

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

Checks whether elements are contained in this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
searchElementnumberYesElement to search for.
fromIndexnumberNoIndex from which the search starts. If a negative number is passed in, it refers to the index of fromIndex + Int8Array.length. The default value is 0.

Return value

TypeDescription
booleanCheck result. The value true is returned if the element exists; otherwise, false is returned.

Error codes

For details about the error codes, see Universal Error Codes and Utils Error Codes.

IDError Message
401Parameter error.
10200011The includes method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.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]>

Returns an iterator object that contains the key-value pair of each element in this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<[number, number]>Iterator object.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The entries method cannot be bound.
10200201Concurrent modification error.

Example

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

Returns an iterator object that contains the key (index) of each element in this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<number>Iterator object.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The keys method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.from([1, 2, 3, 4, 5]);
let iterator: IterableIterator<number> = array.keys();
for (const key of iterator) {
  console.info("" + key); // 0, 1, 2, 3, and 4 are returned in sequence.
}

values

values(): IterableIterator<number>

Returns an iterator object that contains the value of each element in this ArkTS Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<number>Iterator object.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The values method cannot be bound.
10200201Concurrent modification error.

Example

let array: collections.Int8Array = collections.Int8Array.from([1, 2, 3, 4, 5]);
let iterator: IterableIterator<number> = array.values();
for (const value of iterator) {
  console.info("" + value); // 1, 2, 3, 4, and 5 are returned in sequence.
}

[Symbol.iterator]

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

Returns an iterator, each item of which is a JavaScript object.

NOTE

This API cannot be used in .ets files.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<number>Iterator obtained.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The Symbol.iterator method cannot be bound.

Example

let int8Array: collections.Int8Array = collections.Int8Array.from([1, 2, 3, 4, 5, 6]);

for (let item of int8Array) {
  console.info(`value : ${item}`);
}

[index: number]

[index: number]: number

Returns the element at a given index in this Int8Array.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex of the element. The index starts from zero.

Return value

TypeDescription
numberNumber data type.

Example

let int8Array = collections.Int8Array.from([1, 2, 4]);
console.info("Element at index 1: ", int8Array[1]);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-apis-arkts-collections-Float32Array

openharmony 鸿蒙 arkts-apis-arkts-collections-ArrayBuffer

openharmony 鸿蒙 js-apis-lightweightset

openharmony 鸿蒙 errorcode-source-obfuscation

openharmony 鸿蒙 arkts-apis-arkts-utils-ASON

openharmony 鸿蒙 arkts-apis-arkts-collections-Uint8ClampedArray

openharmony 鸿蒙 errorcode-tsc

openharmony 鸿蒙 js-apis-deque

openharmony 鸿蒙 arkts-apis-arkts-collections-BitVector

openharmony 鸿蒙 js-apis-worker

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/sg2WGfao