harmony 鸿蒙@ohos.util.PlainArray (Nonlinear Container PlainArray)
@ohos.util.PlainArray (Nonlinear Container PlainArray)
PlainArray stores key-value (KV) pairs. Each key must be unique, be of the number type, and have only one value.
PlainArray is based on generics and uses a lightweight structure. Keys in the array are searched using binary search and are mapped to values in other arrays.
Both PlainArray and LightWeightMap are used to store KV pairs in the lightweight structure. However, the key type of PlainArray can only be number.
Recommended use case: Use PlainArray when you need to store KV pairs whose keys are of the number type.
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 { PlainArray } from '@kit.ArkTS';
PlainArray
Attributes
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
length | number | Yes | No | Number of elements in a plain array (called container later). |
constructor
constructor()
A constructor used to create a PlainArray 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.
ID | Error Message |
---|---|
10200012 | The PlainArray’s constructor cannot be directly invoked. |
Example
let plainArray: PlainArray<string> = new PlainArray();
isEmpty
isEmpty(): boolean
Checks whether this container is empty.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
Type | Description |
---|---|
boolean | Returns true if the container is empty; returns false otherwise. |
Error codes
For details about the error codes, see Utils Error Codes.
ID | Error Message |
---|---|
10200011 | The isEmpty method cannot be bound. |
Example
const plainArray: PlainArray<string> = new PlainArray();
let result = plainArray.isEmpty();
has
has(key: number): boolean
Checks whether this container contains the specified key.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | number | Yes | Target key. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
boolean | Returns true if the specified key is contained; returns false otherwise. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The has method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
let result = plainArray.has(1);
get
get(key: number): T
Obtains the value of the specified key in this container.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | number | Yes | Target key. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
T | Value of the key. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The get method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.get(1);
getIndexOfKey
getIndexOfKey(key: number): number
Obtains the index of the element with the specified key in this container.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | number | Yes | Target key. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
number | Returns the position index if obtained; returns -1 otherwise. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The getIndexOfKey method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getIndexOfKey(2);
getIndexOfValue
getIndexOfValue(value: T): number
Obtains the index of the first occurrence of an element with the specified value in this container.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
value | T | Yes | Value of the target element. |
Return value
Type | Description |
---|---|
number | Returns the position index if obtained; returns -1 otherwise. |
Error codes
For details about the error codes, see Utils Error Codes.
ID | Error Message |
---|---|
10200011 | The getIndexOfValue method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getIndexOfValue("squirrel");
getKeyAt
getKeyAt(index: number): number
Obtains the key of the 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
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | Yes | Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
number | Returns the key of the element if obtained; returns -1 otherwise. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The getKeyAt method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getKeyAt(1);
getValueAt
getValueAt(index: number): T
Obtains the value of 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
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | Yes | Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
T | Returns the value of the element if obtained; returns undefined otherwise. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200001 | The value of index is out of range. |
10200011 | The getValueAt method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getValueAt(1);
clone
clone(): PlainArray<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
Type | Description |
---|---|
PlainArray<T> | New PlainArray instance obtained. |
Error codes
For details about the error codes, see Utils Error Codes.
ID | Error Message |
---|---|
10200011 | The clone method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let newPlainArray = plainArray.clone();
add
add(key: number, value: T): void
Adds an element to this container.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | number | Yes | Key of the target element. The value must be less than or equal to int32_max, that is, 2147483647. |
value | T | Yes | Value of the target element. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The add method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
remove
remove(key: number): T
Removes a key-value pair with the specified key.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
key | number | Yes | Target key. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
T | Value in the key-value pair removed. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The remove method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.remove(2);
removeAt
removeAt(index: number): T
Removes an element at 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
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | Yes | Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
T | Element removed. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The removeAt method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.removeAt(1);
removeRangeFrom
removeRangeFrom(index: number, size: number): number
Removes elements in a specified range from this container.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | Yes | Start position of the elements to remove. The value must be less than or equal to int32_max, that is, 2147483647. |
size | number | Yes | Number of elements to remove. The value must be less than or equal to int32_max, that is, 2147483647. |
Return value
Type | Description |
---|---|
number | Number of elements removed. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200001 | The value of index is out of range. |
10200011 | The removeRangeFrom method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.removeRangeFrom(1, 3);
setValueAt
setValueAt(index: number, value: T): void
Sets a value for 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
Name | Type | Mandatory | Description |
---|---|---|---|
index | number | Yes | Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647. |
value | T | Yes | Value of the target element. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200001 | The value of index is out of range. |
10200011 | The setValueAt method cannot be bound. |
Example
let plainArray: PlainArray<string|number> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
plainArray.setValueAt(1, 3546);
toString
toString(): String
Obtains a string that contains all elements in this container.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
Type | Description |
---|---|
String | String obtained. |
Error codes
For details about the error codes, see Utils Error Codes.
ID | Error Message |
---|---|
10200011 | The toString method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.toString();
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.
ID | Error Message |
---|---|
10200011 | The clear method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
plainArray.clear();
forEach
forEach(callbackFn: (value: T, index?: number, PlainArray?: PlainArray<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
Name | Type | Mandatory | Description |
---|---|---|---|
callbackFn | function | Yes | Callback invoked to traverse the elements in the container. |
thisArg | Object | No | Value of this to use when callbackFn is invoked. The default value is this instance. |
callbackFn |Name|Type|Mandatory|Description| |——–|——–|——–|——–| |value|T|Yes|Value of the element that is currently traversed.| |index|number|No|Position index of the element that is currently traversed. The default value is 0.| |PlainArray|PlainArray<T>|No|Instance 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.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
10200011 | The forEach method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
plainArray.forEach((value: string, index?: number) => {
console.log("value:" + value, "index:" + index);
});
// You are not advised to use the add, remove, or removeAt APIs in forEach because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
let plainArray: PlainArray<string> = new PlainArray();
for(let i = 0;i < 10;i++) {
plainArray.add(i,"123");
}
for(let i = 0;i < 10;i++) {
plainArray.remove(i);
}
[Symbol.iterator]
[Symbol.iterator](): IterableIterator<[number, T]>
Obtains an iterator object that contains key-value pairs, where the key is of the number type.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
Type | Description |
---|---|
IterableIterator<[number, T]> | Iterator obtained. |
Error codes
For details about the error codes, see Utils Error Codes.
ID | Error Message |
---|---|
10200011 | The Symbol.iterator method cannot be bound. |
Example
let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let iter = plainArray[Symbol.iterator]();
let temp: IteratorResult<Object[]> = iter.next();
while(!temp.done) {
console.log("key:" + temp.value[0]);
console.log("value:" + temp.value[1]);
temp = iter.next();
}
// You are not advised to use the add, remove, or removeAt APIs in Symbol.iterator because they may cause unpredictable risks such as infinite loops. You can use the for loop when inserting or deleting data.
let plainArray: PlainArray<string> = new PlainArray();
for(let i = 0;i < 10;i++) {
plainArray.add(i,"123");
}
for(let i = 0;i < 10;i++) {
plainArray.remove(i);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Compilation Toolchain Error Codes
harmony 鸿蒙TypeScript Compiler 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)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦