非線性容器PlainArray

2024-01-23 18:16 更新
說明

本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

PlainArray可用于存儲具有關聯關系的key-value鍵值對集合,存儲元素中key值唯一,key值類型為number類型,每個key對應一個value。

PlainArray依據泛型定義,采用輕量級結構,集合中key值的查找依賴于二分查找算法,然后映射到其他數組中的value值。

PlainArray和LightWeightMap都是用來存儲鍵值對,且均采用輕量級結構,但PlainArray的key值類型只能為number類型。

推薦使用場景: 當需要存儲key值為number類型的鍵值對時,可以使用PlainArray。

文檔中存在泛型的使用,涉及以下泛型標記符:

  • T: Type, 類

導入模塊

  1. import PlainArray from '@ohos.util.PlainArray';

PlainArray

屬性

系統(tǒng)能力: SystemCapability.Utils.Lang

名稱

類型

可讀

可寫

說明

length

number

PlainArray的元素個數。

constructor

constructor()

PlainArray的構造函數。

系統(tǒng)能力: SystemCapability.Utils.Lang

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200012

The PlainArray's constructor cannot be directly invoked.

示例:

  1. let plainArray = new PlainArray();

isEmpty

isEmpty(): boolean

判斷該容器是否為空。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型

說明

boolean

為空返回true, 不為空返回false。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The isEmpty method cannot be bound.

示例:

  1. const plainArray = new PlainArray();
  2. let result = plainArray.isEmpty();

has

has(key: number): boolean

判斷此容器中是否含有該指定key。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

key

number

指定key。

返回值:

類型

說明

boolean

包含指定key返回true,否則返回false。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The has method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.has(1);
  3. plainArray.add(1, "squirrel");
  4. let result1 = plainArray.has(1);

get

get(key: number): T

獲取指定key所對應的value。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

key

number

查找的指定key。

返回值:

類型

說明

T

返回key映射的value值。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The get method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.get(1);

getIndexOfKey

getIndexOfKey(key: number): number

查找指定key對應的下標值,如果沒有找到該key返回-1。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

key

number

指定key。

返回值:

類型

說明

number

返回指定key對應的下標值,查找失敗返回-1。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The getIndexOfKey method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.getIndexOfKey(2);

getIndexOfValue

getIndexOfValue(value: T): number

查找指定value元素第一次出現的下標值,如果沒有找到該value元素返回-1。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

value

T

指定value元素。

返回值:

類型

說明

number

返回指定value元素第一次出現時的下標值,查找失敗返回-1。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The getIndexOfValue method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.getIndexOfValue("squirrel");

getKeyAt

getKeyAt(index: number): number

查找指定下標元素鍵值對中的key值。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

index

number

指定下標。

返回值:

類型

說明

number

返回該下標元素鍵值對中的key值,失敗返回-1。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The getKeyAt method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.getKeyAt(1);

getValueAt

getValueAt(index: number): T

查找指定下標元素鍵值對中的Value值,失敗返回undefined。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

index

number

指定下標。

返回值:

類型

說明

T

返回該下標元素鍵值對中的value值,失敗返回undefined。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The getValueAt method cannot be bound.

10200001

The value of index is out of range.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.getValueAt(1);

clone

clone(): PlainArray<T>

克隆一個實例,并返回克隆后的實例。修改克隆后的實例并不會影響原實例。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型

說明

PlainArray<T>

返回新的對象實例。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The clone method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let newPlainArray = plainArray.clone();

add

add(key: number, value: T): void

向容器中添加一組數據。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

key

number

添加成員數據的鍵名。

value

T

添加成員數據的值。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The add method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");

remove

remove(key: number): T

刪除指定key對應的鍵值對。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

key

number

指定key。

返回值:

類型

說明

T

返回所刪除的鍵值對中的Value值。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The remove method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.remove(2);

removeAt

removeAt(index: number): T

刪除指定下標對應的元素。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

index

number

指定元素下標。

返回值:

類型

說明

T

返回刪除的元素。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The removeAt method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.removeAt(1);

removeRangeFrom

removeRangeFrom(index: number, size: number): number

刪除一定范圍內的元素。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

index

number

刪除元素的起始下標。

size

number

期望刪除元素個數。

返回值:

類型

說明

number

實際刪除元素個數。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The removeRangeFrom method cannot be bound.

10200001

The value of index is out of range.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.removeRangeFrom(1, 3);

setValueAt

setValueAt(index: number, value: T): void

替換容器中指定下標對應鍵值對中的鍵值。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

index

number

指定替換數據下標。

value

T

替換鍵值對中的值。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The setValueAt method cannot be bound.

10200001

The value of index is out of range.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. plainArray.setValueAt(1, 3546);

toString

toString(): String

獲取包含容器中所有鍵和值的字符串。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型

說明

String

返回對應字符串。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼

錯誤碼ID

錯誤信息

10200011

The toString method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. let result = plainArray.toString();

clear

clear(): void

清除容器中的所有元素,并把length置為0。

系統(tǒng)能力: SystemCapability.Utils.Lang

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The clear method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. plainArray.clear();

forEach

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

通過回調函數來遍歷實例對象上的元素以及元素對應的下標。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數:

參數名

類型

必填

說明

callbackFn

function

回調函數。

thisArg

Object

callbackfn被調用時用作this值。

callbackfn的參數說明:

參數名

類型

必填

說明

value

T

當前遍歷到的元素鍵值對的值。

index

number

當前遍歷到的元素鍵值對的鍵。

PlainArray

PlainArray<T>

當前調用forEach方法的實例對象。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The forEach method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. plainArray.forEach((value, index) => {
  5. console.log("value:" + value, "index:" + index);
  6. });

[Symbol.iterator]

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

返回一個包含key-value鍵值對的迭代器對象,其中key是number類型。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型

說明

IterableIterator<[number, T]>

返回一個迭代器。

錯誤碼:

以下錯誤碼的詳細介紹請參見語言基礎類庫錯誤碼。

錯誤碼ID

錯誤信息

10200011

The Symbol.iterator method cannot be bound.

示例:

  1. let plainArray = new PlainArray();
  2. plainArray.add(1, "squirrel");
  3. plainArray.add(2, "sparrow");
  4. // 使用方法一:
  5. for (let item of plainArray) {
  6. console.log("key:" + item[0]);
  7. console.log("value:" + item[1]);
  8. }
  9. // 使用方法二:
  10. let iter = plainArray[Symbol.iterator]();
  11. let temp = iter.next().value;
  12. while(temp != undefined) {
  13. console.log("key:" + temp[0]);
  14. console.log("value:" + temp[1]);
  15. temp = iter.next().value;
  16. }
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號