openharmony 鸿蒙 js-apis-arraylist

2025-06-12 浏览 (1)

@ohos.util.ArrayList (Linear Container ArrayList)

ArrayList is a linear data structure that is implemented based on arrays. ArrayList can dynamically adjust the capacity based on project requirements. It increases the capacity by 50% each time.

Similar to ArrayList, Vector is also implemented based on arrays and can dynamically adjust the capacity. It increases the capability by 100% each time.

When compared with LinkedList, ArrayList is more efficient in random access but less efficient in the addition or removal operation, because its addition or removal operation affects the position of other elements in the container.

Recommended use case: Use ArrayList when elements in a container need to be frequently read.

This topic uses the following to identify the use of generics:

  • T: Type

NOTE

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

Modules to Import

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

ArrayList

Attributes

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

System capability: SystemCapability.Utils.Lang

NameTypeReadableWritableDescription
lengthnumberYesNoNumber of elements in an array list (called container later).

constructor

constructor()

A constructor used to create an ArrayList instance.

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

System capability: SystemCapability.Utils.Lang

Error codes

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

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

Example

let arrayList: ArrayList<string|number> = new ArrayList();

add

add(element: T): boolean

Adds an element at the end of this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
booleanReturns true if the element is added successfully; returns false otherwise.

Error codes

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

IDError Message
10200011The add method cannot be bound.

Example

class C1 {
  name: string = ""
  age: string = ""
}
let arrayList: ArrayList<string|number|boolean|Array<number>|C1> = new ArrayList();
let result1 = arrayList.add("a");
let result2 = arrayList.add(1);
let b = [1, 2, 3];
let result3 = arrayList.add(b);
let c : C1 = {name: "Dylan", age: "13"}
let result4 = arrayList.add(c);
let result5 = arrayList.add(false);

insert

insert(element: T, index: number): void

Inserts an element at the specified position in this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.
indexnumberYesIndex of the position where the element is to be inserted. The value must be less than or equal to int32_max, that is, 2147483647.

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; 3. Parameter verification failed.
10200001The value of index is out of range.
10200011The insert method cannot be bound.

Example

let arrayList: ArrayList<number|string|boolean> = new ArrayList();
arrayList.insert("A", 0);
arrayList.insert(0, 1);
arrayList.insert(true, 2);

has

has(element: T): boolean

Checks whether this container has the specified element.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
booleanReturns true if the specified element is contained; returns false otherwise.

Error codes

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

IDError Message
10200011The has method cannot be bound.

Example

let arrayList: ArrayList<string> = new ArrayList();
arrayList.add("squirrel");
let result: boolean = arrayList.has("squirrel");

getIndexOf

getIndexOf(element: T): number

Obtains the index of the first occurrence of the specified element in this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
numberReturns the position index if obtained; returns -1 if the specified element is not found.

Error codes

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

IDError Message
10200011The getIndexOf method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(1);
arrayList.add(2);
arrayList.add(4);
let result: number = arrayList.getIndexOf(2);

getLastIndexOf

getLastIndexOf(element: T): number

Obtains the index of the last occurrence of the specified element in this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
numberReturns the position index if obtained; returns -1 if the specified element is not found.

Error codes

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

IDError Message
10200011The getLastIndexOf method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(1);
arrayList.add(2);
arrayList.add(4);
let result: number = arrayList.getLastIndexOf(2);

removeByIndex

removeByIndex(index: number): T

Removes an element with the specified position from this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesPosition index of the target element. The value must be less than or equal to int32_max, that is, 2147483647.

Return value

TypeDescription
TElement removed.

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; 3. Parameter verification failed.
10200001The value of index is out of range.
10200011The removeByIndex method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(4);
let result: number = arrayList.removeByIndex(2);

remove

remove(element: T): boolean

Removes the first occurrence of the specified element from this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
booleanReturns true if the element is removed successfully; returns false otherwise.

Error codes

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

IDError Message
10200011The remove method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result: boolean = arrayList.remove(2);

removeByRange

removeByRange(fromIndex: number, toIndex: number): void

Removes from this container all of the elements within a range, including the element at the start position but not that at the end position.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
fromIndexnumberYesIndex of the start position.
toIndexnumberYesIndex of the end position.

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; 3. Parameter verification failed.
10200001The value of fromIndex or toIndex is out of range.
10200011The removeByRange method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.removeByRange(2, 4);

replaceAllElements

replaceAllElements(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => T, thisArg?: Object): void

Replaces all elements in this container with new elements, and returns the new ones.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnfunctionYesCallback invoked for the replacement.
thisArgObjectNoValue of this to use when callbackFn is invoked. The default value is this instance.

callbackFn

NameTypeMandatoryDescription
valueTYesValue of the element that is currently traversed.
indexnumberNoPosition index of the element that is currently traversed. The default value is 0.
arrlistArrayList<T>NoInstance that calls the replaceAllElements API. The default value is this instance.

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 replaceAllElements method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.replaceAllElements((value: number): number => {
  // Add the user operation logic based on the actual scenario.
  return value;
});

forEach

forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, thisArg?: Object): void

Uses a callback to traverse the elements in this container and obtain their position indexes.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnfunctionYesCallback invoked for the replacement.
thisArgObjectNoValue of this to use when callbackFn is invoked. The default value is this instance.

callbackFn

NameTypeMandatoryDescription
valueTYesValue of the element that is currently traversed.
indexnumberNoPosition index of the element that is currently traversed. The default value is 0.
arrlistArrayList<T>NoInstance that calls the forEach API. The default value is this instance.

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 forEach method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.forEach((value: number, index?: number) => {
  console.log("value:" + value, "index:" + index);
});

sort

sort(comparator?: (firstValue: T, secondValue: T) => number): void

Sorts elements in this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
comparatorfunctionNoCallback invoked for sorting. The default value is the callback function for sorting elements in ascending order.

comparator

NameTypeMandatoryDescription
firstValueTYesPrevious element.
secondValueTYesNext element.

Error codes

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

IDError Message
401Parameter error. Possible causes: 1. Incorrect parameter types; 2. Parameter verification failed.
10200011The sort method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.sort((a: number, b: number) => a - b);
arrayList.sort((a: number, b: number) => b - a);
arrayList.sort();

subArrayList

subArrayList(fromIndex: number, toIndex: number): ArrayList<T>

Obtains elements within a range in this container, including the element at the start position but not that at the end position, and returns these elements as a new ArrayList instance.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
fromIndexnumberYesIndex of the start position.
toIndexnumberYesIndex of the end position.

Return value

TypeDescription
ArrayList<T>New ArrayList instance obtained.

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; 3. Parameter verification failed.
10200001The value of fromIndex or toIndex is out of range.
10200011The subArrayList method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result: ArrayList<number> = arrayList.subArrayList(2, 4);

clear

clear(): void

Clears this container and sets its length to 0.

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

System capability: SystemCapability.Utils.Lang

Error codes

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

IDError Message
10200011The clear method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.clear();

clone

clone(): ArrayList<T>

Clones this container and returns a copy. The modification to the copy does not affect the original instance.

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

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
ArrayList<T>New ArrayList instance obtained.

Error codes

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

IDError Message
10200011The clone method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result:  ArrayList<number> = arrayList.clone();

getCapacity

getCapacity(): number

Obtains the capacity of this container.

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

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
numberCapacity obtained.

Error codes

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

IDError Message
10200011The getCapacity method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result: number = arrayList.getCapacity();

convertToArray

convertToArray(): Array<T>

Converts this container into an array.

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

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
Array<T>Array obtained.

Error codes

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

IDError Message
10200011The convertToArray method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result: Array<number> = arrayList.convertToArray();

isEmpty

isEmpty(): boolean

Checks whether this container is empty (contains no element).

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

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
booleanReturns true if the container is empty; returns false otherwise.

Error codes

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

IDError Message
10200011The isEmpty method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result: boolean = arrayList.isEmpty();

[index: number]12+

[index: number]: T

Returns the element at the given index.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesIndex. The value must be less than or equal to int32_max, that is, 2147483647.

Return value

TypeDescription
TElement obtained.

Error codes

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

IDError Message
401Parameter error.
10200001The value of index is out of range.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result: number = arrayList[2];

increaseCapacityTo

increaseCapacityTo(newCapacity: number): void

Increases the capacity of this container.

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

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
newCapacitynumberYesNew capacity.

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 increaseCapacityTo method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.increaseCapacityTo(2);
arrayList.increaseCapacityTo(8);

trimToCurrentLength

trimToCurrentLength(): void

Releases the reserved space in this container by adjusting the container capacity to the actual number of elements in this container.

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

System capability: SystemCapability.Utils.Lang

Error codes

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

IDError Message
10200011The trimToCurrentLength method cannot be bound.

Example

let arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.trimToCurrentLength();

[Symbol.iterator]

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

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

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

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<T>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 arrayList: ArrayList<number> = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);

// Method 1:
let numbers: Array<number> = arrayList.convertToArray()
for (let item of numbers) {
  console.log(`value : ${item}`);
}

// Method 2:
let iter = arrayList[Symbol.iterator]();
let temp: IteratorResult<number> = iter.next();
while(!temp.done) {
    console.log(`value:${temp.value}`);
    temp = iter.next();
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS

harmony 鸿蒙Compilation Toolchain Error Codes

harmony 鸿蒙TypeScript Compiler Error Codes

harmony 鸿蒙Utils Error Codes

harmony 鸿蒙js-apis-arkts-collections

harmony 鸿蒙@arkts.math.Decimal (High-Precision Math Library Decimal)

harmony 鸿蒙@arkts.lang (ArkTS Base Capability)

harmony 鸿蒙@arkts.utils (ArkTS Utils)

harmony 鸿蒙@ohos.buffer (Buffer)

harmony 鸿蒙@ohos.convertxml (XML-to-JavaScript Conversion)

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