harmony 鸿蒙@ohos.util.PlainArray (Nonlinear Container PlainArray)

2022-08-09 浏览 (782)

@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 '@ohos.util.PlainArray';  

PlainArray

Attributes

System capability: SystemCapability.Utils.Lang

NameTypeReadableWritableDescription
lengthnumberYesNoNumber of elements in a plain array (called container later).

constructor

constructor()

A constructor used to create a PlainArray instance.

System capability: SystemCapability.Utils.Lang

Error codes

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

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

Example

let plainArray: PlainArray<string> = new PlainArray();

isEmpty

isEmpty(): boolean

Checks whether this container is empty.

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

const plainArray: PlainArray<string> = new PlainArray();
let result = plainArray.isEmpty();

has

has(key: number): boolean

Checks whether this container contains the specified key.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
keynumberYesTarget key.

Return value

TypeDescription
booleanReturns true if the specified key 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 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
keynumberYesTarget key.

Return value

TypeDescription
TValue of the key.

Error codes

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

IDError Message
10200011The 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 first occurrence of an element with the specified key in this container.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
keynumberYesTarget key.

Return value

TypeDescription
numberReturns the position index if obtained; returns -1 otherwise.

Error codes

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

IDError Message
10200011The 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
valueTYesValue of the target element.

Return value

TypeDescription
numberReturns the position index if obtained; returns -1 otherwise.

Error codes

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

IDError Message
10200011The 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesPosition index of the target element.

Return value

TypeDescription
numberReturns the key of the element if obtained; returns -1 otherwise.

Error codes

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

IDError Message
10200011The 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesPosition index of the target element.

Return value

TypeDescription
TReturns the value of the element if obtained; returns undefined otherwise.

Error codes

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

IDError Message
10200011The getValueAt method cannot be bound.
10200001The value of index is out of range.

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.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
PlainArray<T>New PlainArray instance obtained.

Error codes

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

IDError Message
10200011The 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
keynumberYesKey of the target element.
valueTYesValue of the target element.

Error codes

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

IDError Message
10200011The add method cannot be bound.

Example

let plainArray: PlainArray<string> = new PlainArray();
plainArray.add(1, "squirrel");

remove

remove(key: number): T

Removes an element with the specified key from this container.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
keynumberYesTarget key.

Return value

TypeDescription
TValue of the element removed.

Error codes

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

IDError Message
10200011The 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesPosition index of the target element.

Return value

TypeDescription
TElement removed.

Error codes

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

IDError Message
10200011The 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesStart position of the elements to remove.
sizenumberYesNumber of elements to remove.

Return value

TypeDescription
numberNumber of elements removed.

Error codes

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

IDError Message
10200011The removeRangeFrom method cannot be bound.
10200001The value of index is out of range.

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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
indexnumberYesPosition index of the target element.
valueTYesValue of the target element.

Error codes

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

IDError Message
10200011The setValueAt method cannot be bound.
10200001The value of index is out of range.

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.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
StringString obtained.

Error codes

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

IDError Message
10200011The 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.

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 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.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnfunctionYesCallback invoked to traverse the elements in the container.
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.
PlainArrayPlainArray<T>NoInstance that calls the forEach API. The default value is this instance.

Error codes

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

IDError Message
10200011The 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);
});

[Symbol.iterator]

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

Obtains an iterator object that contains key-value pairs, where the key is of the number type.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<[number, 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 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();
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

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