js-apis-plainarray.md 11.0 KB
Newer Older
W
wusongqing 已提交
1 2
# Nonlinear Container PlainArray 

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6
> 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.

W
wusongqing 已提交
7 8 9 10 11 12 13
**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, which map to values in other arrays.

Both **PlainArray** and **[LightWeightMap](js-apis-lightweightmap.md)** 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.
W
wusongqing 已提交
14 15 16

## Modules to Import

W
wusongqing 已提交
17 18
```ts
import PlainArray from '@ohos.util.PlainArray';  
W
wusongqing 已提交
19 20 21 22 23 24 25 26
```



## PlainArray

### Attributes

W
wusongqing 已提交
27 28
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
29 30
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
31
| length | number | Yes| No| Number of elements in a plain array (called container later).|
W
wusongqing 已提交
32 33 34 35 36 37 38 39


### constructor

constructor()

A constructor used to create a **PlainArray** instance.

W
wusongqing 已提交
40 41
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
42 43
**Example**

W
wusongqing 已提交
44
```ts
W
wusongqing 已提交
45 46 47 48 49 50 51 52 53 54
let plainArray = new PlainArray();
```


### isEmpty

isEmpty(): boolean

Checks whether this container is empty.

W
wusongqing 已提交
55 56
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
57 58 59 60 61 62 63 64
**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

**Example**

W
wusongqing 已提交
65
```ts
W
wusongqing 已提交
66 67 68 69 70 71 72 73 74 75 76
const plainArray = new PlainArray();
let result = plainArray.isEmpty();
```


### has

has(key: number): boolean

Checks whether this container contains the specified key.

W
wusongqing 已提交
77 78
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
79 80 81 82
**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
83
| key | number | Yes| Target key.|
W
wusongqing 已提交
84 85 86 87 88 89 90 91 92

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|

**Example**

W
wusongqing 已提交
93
```ts
W
wusongqing 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
let plainArray = new PlainArray();
plainArray.has(1);
plainArray.add(1, "sddfhf");
let result1 = plainArray.has(1);
```


### get

get(key: number): T

Obtains the value of the specified key in this container.

W
wusongqing 已提交
107 108
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
109 110 111 112
**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
113
| key | number | Yes| Target key.|
W
wusongqing 已提交
114 115 116 117 118 119 120 121 122

**Return value**

| Type| Description|
| -------- | -------- |
| T | Value of the key.|

**Example**

W
wusongqing 已提交
123
```ts
W
wusongqing 已提交
124 125 126 127 128 129 130 131 132
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.get(1);
```


### getIndexOfKey

Z
zengyawen 已提交
133
getIndexOfKey(key: number): number
W
wusongqing 已提交
134

W
wusongqing 已提交
135 136 137
Obtains the index of the first occurrence of an element with the specified key in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
138 139 140 141 142

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
143
| key | number | Yes| Target key.|
W
wusongqing 已提交
144 145 146 147 148

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
149
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
150 151 152

**Example**

W
wusongqing 已提交
153
```ts
W
wusongqing 已提交
154 155 156
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
W
wusongqing 已提交
157
let result = plainArray.getIndexOfKey(2);
W
wusongqing 已提交
158 159 160 161 162
```


### getIndexOfValue

Z
zengyawen 已提交
163
getIndexOfValue(value: T): number
W
wusongqing 已提交
164

W
wusongqing 已提交
165 166 167
Obtains the index of the first occurrence of an element with the specified value in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
168 169 170 171 172

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
173
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
174 175 176 177 178

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
179
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
180 181 182

**Example**

W
wusongqing 已提交
183
```ts
W
wusongqing 已提交
184 185 186 187 188 189 190 191 192
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.getIndexOfValue("sddfhf");
```


### getKeyAt

Z
zengyawen 已提交
193
getKeyAt(index: number): number
W
wusongqing 已提交
194

W
wusongqing 已提交
195 196 197
Obtains the key of the element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
198 199 200 201 202

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
203
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
204 205 206 207 208

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
209
| number | Returns the key of the element if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
210 211 212

**Example**

W
wusongqing 已提交
213
```ts
W
wusongqing 已提交
214 215 216 217 218 219 220 221 222 223
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.getKeyAt(1);
```

### getValueAt

getValueAt(index: number): T

W
wusongqing 已提交
224 225 226
Obtains the value of an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
227 228 229

**Parameters**

W
wusongqing 已提交
230 231 232
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
233 234 235

**Return value**

W
wusongqing 已提交
236 237 238
| Type| Description|
| -------- | -------- |
| T | Returns the value of the element if obtained; returns **undefined** otherwise.|
W
wusongqing 已提交
239 240 241

**Example**

W
wusongqing 已提交
242
  ```ts
W
wusongqing 已提交
243 244 245 246 247 248 249 250 251 252 253 254
  let plainArray = new PlainArray();
  plainArray.add(1, "sddfhf");
  plainArray.add(2, "sffdfhf");
  let result = plainArray.getKeyAt(1);
  ```

### clone

clone(): PlainArray<T>

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

W
wusongqing 已提交
255 256
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
257 258 259 260 261 262 263 264
**Return value**

| Type| Description|
| -------- | -------- |
| PlainArray<T> | New **PlainArray** instance obtained.|

**Example**

W
wusongqing 已提交
265
```ts
Z
zengyawen 已提交
266
let plainArray = new PlainArray();
W
wusongqing 已提交
267 268 269 270 271 272 273 274 275 276
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let newPlainArray = plainArray.clone();
```


### add

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

W
wusongqing 已提交
277 278 279
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
280 281 282 283 284

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
285 286
| key | number | Yes| Key of the target element.|
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
287 288 289

**Example**

W
wusongqing 已提交
290
```ts
W
wusongqing 已提交
291 292 293 294 295 296 297 298 299
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
```


### remove

remove(key: number): T

W
wusongqing 已提交
300 301 302
Removes an element with the specified key from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
303 304 305 306 307

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
308
| key | number | Yes| Target key.|
W
wusongqing 已提交
309 310 311 312 313

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
314
| T | Value of the element removed.|
W
wusongqing 已提交
315 316 317

**Example**

W
wusongqing 已提交
318
```ts
W
wusongqing 已提交
319 320 321 322 323 324 325 326 327 328 329 330
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.remove(2);
let result = plainArray.remove(2);
```


### removeAt

removeAt(index: number): T

W
wusongqing 已提交
331 332 333
Removes an element at the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
334 335 336 337 338

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
339
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
340 341 342 343 344

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
345
| T | Element removed.|
W
wusongqing 已提交
346 347 348

**Example**

W
wusongqing 已提交
349
```ts
W
wusongqing 已提交
350 351 352 353 354 355 356 357 358 359 360 361
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.removeAt(1);
let result = plainArray.removeAt(1);
```


### removeRangeFrom

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

W
wusongqing 已提交
362 363 364
Removes elements in a specified range from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
365 366 367 368 369

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
370 371
| index | number | Yes| Start position of the elements to remove.|
| size | number | Yes| Number of elements to remove.|
W
wusongqing 已提交
372 373 374 375 376

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
377
| number | Number of elements removed.|
W
wusongqing 已提交
378 379 380

**Example**

W
wusongqing 已提交
381
```ts
W
wusongqing 已提交
382 383 384 385 386 387 388 389 390 391 392
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.removeRangeFrom(1, 3);
```


### setValueAt

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

W
wusongqing 已提交
393 394 395
Sets a value for an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
396 397 398 399 400

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
401 402
| index | number | Yes| Position index of the target element.|
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
403 404 405

**Example**

W
wusongqing 已提交
406
```ts
W
wusongqing 已提交
407 408 409 410 411 412 413 414 415 416 417
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.setValueAt(1, 3546);
```


### toString

toString(): String

W
wusongqing 已提交
418 419 420
Obtains a string that contains all elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
421 422 423 424 425 426 427 428 429

**Return value**

| Type| Description|
| -------- | -------- |
| String | String obtained.|

**Example**

W
wusongqing 已提交
430
```ts
W
wusongqing 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.toString();
```


### clear

clear(): void

Clears this container and sets its length to **0**.

W
wusongqing 已提交
444 445
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
446 447
**Example**

W
wusongqing 已提交
448
```ts
W
wusongqing 已提交
449 450 451 452 453 454 455 456 457
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.clear();
```


### forEach

Z
zengyawen 已提交
458
forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray<T>) => void, thisArg?: Object): void
W
wusongqing 已提交
459

W
wusongqing 已提交
460 461 462
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
463 464 465 466 467

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
468
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
469 470 471 472 473
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
474 475
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Key of the element that is currently traversed.|
Z
zengyawen 已提交
476
| PlainArray | PlainArray<T>| No| Instance that invokes the **forEach** API.|
W
wusongqing 已提交
477 478 479

**Example**

W
wusongqing 已提交
480
```ts
W
wusongqing 已提交
481 482 483
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
W
wusongqing 已提交
484 485
plainArray.forEach((value, index) => {
  console.log("value:" + value, index);
W
wusongqing 已提交
486 487 488 489 490 491 492 493 494 495
});
```


### [Symbol.iterator]

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

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

W
wusongqing 已提交
496 497
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
498 499 500 501
**Return value**

| Type| Description|
| -------- | -------- |
Z
zengyawen 已提交
502
| IterableIterator<[number, T]> | Iterator obtained.|
W
wusongqing 已提交
503 504 505

**Example**

W
wusongqing 已提交
506
```ts
W
wusongqing 已提交
507 508 509 510 511 512
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");

// Method 1:
for (let item of plainArray) { 
W
wusongqing 已提交
513 514
  console.log("index:" + item[0]);
  console.log("value:" + item[1]);
W
wusongqing 已提交
515 516 517 518 519 520
}

// Method 2:
let iter = plainArray[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
521 522
  console.log("index:" + temp[0]);
  console.log("value:" + temp[1]);
W
wusongqing 已提交
523 524 525
  temp = iter.next().value;
}
```