js-apis-plainarray.md 15.1 KB
Newer Older
G
Gloria 已提交
1
# @ohos.util.PlainArray (Nonlinear Container PlainArray) 
W
wusongqing 已提交
2

W
wusongqing 已提交
3 4
**PlainArray** stores key-value (KV) pairs. Each key must be unique, be of the number type, and have only one value.

G
Gloria 已提交
5
**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.
W
wusongqing 已提交
6 7 8 9

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 已提交
10

G
Gloria 已提交
11 12 13
This topic uses the following to identify the use of generics:
- T: Type

G
Gloria 已提交
14 15 16 17 18
> **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.


W
wusongqing 已提交
19 20
## Modules to Import

W
wusongqing 已提交
21
```ts
W
wusongqing 已提交
22
import PlainArray from '@ohos.util.PlainArray';  
W
wusongqing 已提交
23 24
```

25

W
wusongqing 已提交
26 27 28 29
## PlainArray

### Attributes

W
wusongqing 已提交
30 31
**System capability**: SystemCapability.Utils.Lang

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


### constructor

constructor()

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

W
wusongqing 已提交
43 44
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
45 46
**Error codes**

47
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
48 49 50 51 52

| ID| Error Message|
| -------- | -------- |
| 10200012 | The PlainArray's constructor cannot be directly invoked. |

W
wusongqing 已提交
53 54
**Example**

W
wusongqing 已提交
55
```ts
W
wusongqing 已提交
56 57 58 59 60 61 62 63 64 65
let plainArray = new PlainArray();
```


### isEmpty

isEmpty(): boolean

Checks whether this container is empty.

W
wusongqing 已提交
66 67
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
68 69 70 71 72 73
**Return value**

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

G
Gloria 已提交
74 75
**Error codes**

76
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
77 78 79 80 81

| ID| Error Message|
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

W
wusongqing 已提交
82 83
**Example**

W
wusongqing 已提交
84
```ts
W
wusongqing 已提交
85 86 87 88 89 90 91 92 93 94 95
const plainArray = new PlainArray();
let result = plainArray.isEmpty();
```


### has

has(key: number): boolean

Checks whether this container contains the specified key.

W
wusongqing 已提交
96 97
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
98 99 100 101
**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
102
| key | number | Yes| Target key.|
W
wusongqing 已提交
103 104 105 106 107 108 109

**Return value**

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

G
Gloria 已提交
110 111
**Error codes**

112
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
113 114 115 116 117

| ID| Error Message|
| -------- | -------- |
| 10200011 | The has method cannot be bound. |

W
wusongqing 已提交
118 119
**Example**

W
wusongqing 已提交
120
```ts
W
wusongqing 已提交
121
let plainArray = new PlainArray();
G
Gloria 已提交
122
plainArray.add(1, "squirrel");
G
Gloria 已提交
123
let result = plainArray.has(1);
W
wusongqing 已提交
124 125 126 127 128 129 130 131 132
```


### get

get(key: number): T

Obtains the value of the specified key in this container.

W
wusongqing 已提交
133 134
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
135 136 137 138
**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
139
| key | number | Yes| Target key.|
W
wusongqing 已提交
140 141 142 143 144 145 146

**Return value**

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

G
Gloria 已提交
147 148
**Error codes**

149
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
150 151 152 153 154

| ID| Error Message|
| -------- | -------- |
| 10200011 | The get method cannot be bound. |

W
wusongqing 已提交
155 156
**Example**

W
wusongqing 已提交
157
```ts
W
wusongqing 已提交
158
let plainArray = new PlainArray();
G
Gloria 已提交
159 160
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
161 162 163 164 165 166
let result = plainArray.get(1);
```


### getIndexOfKey

W
wusongqing 已提交
167
getIndexOfKey(key: number): number
W
wusongqing 已提交
168

W
wusongqing 已提交
169 170 171
Obtains the index of the first occurrence of an element with the specified key in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
172 173 174 175 176

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
177
| key | number | Yes| Target key.|
W
wusongqing 已提交
178 179 180 181 182

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
183
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
184

G
Gloria 已提交
185 186
**Error codes**

187
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
188 189 190 191 192

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getIndexOfKey method cannot be bound. |

W
wusongqing 已提交
193 194
**Example**

W
wusongqing 已提交
195
```ts
W
wusongqing 已提交
196
let plainArray = new PlainArray();
G
Gloria 已提交
197 198
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
199
let result = plainArray.getIndexOfKey(2);
W
wusongqing 已提交
200 201 202 203 204
```


### getIndexOfValue

W
wusongqing 已提交
205
getIndexOfValue(value: T): number
W
wusongqing 已提交
206

W
wusongqing 已提交
207 208 209
Obtains the index of the first occurrence of an element with the specified value in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
210 211 212 213 214

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
215
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
216 217 218 219 220

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
221
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
222

G
Gloria 已提交
223 224
**Error codes**

225
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
226 227 228 229 230

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getIndexOfValue method cannot be bound. |

W
wusongqing 已提交
231 232
**Example**

W
wusongqing 已提交
233
```ts
W
wusongqing 已提交
234
let plainArray = new PlainArray();
G
Gloria 已提交
235 236 237
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getIndexOfValue("squirrel");
W
wusongqing 已提交
238 239 240 241 242
```


### getKeyAt

W
wusongqing 已提交
243
getKeyAt(index: number): number
W
wusongqing 已提交
244

W
wusongqing 已提交
245 246 247
Obtains the key of the element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
248 249 250 251 252

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
253
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
254 255 256 257 258

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
259
| number | Returns the key of the element if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
260

G
Gloria 已提交
261 262
**Error codes**

263
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
264 265 266 267 268

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getKeyAt method cannot be bound. |

W
wusongqing 已提交
269 270
**Example**

W
wusongqing 已提交
271
```ts
W
wusongqing 已提交
272
let plainArray = new PlainArray();
G
Gloria 已提交
273 274
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
275 276 277 278 279 280 281
let result = plainArray.getKeyAt(1);
```

### getValueAt

getValueAt(index: number): T

W
wusongqing 已提交
282 283 284
Obtains the value of an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
285 286 287

**Parameters**

288 289 290
  | Name| Type | Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
291 292 293

**Return value**

294 295 296
  | Type| Description|
  | -------- | -------- |
  | T | Returns the value of the element if obtained; returns **undefined** otherwise.|
W
wusongqing 已提交
297

G
Gloria 已提交
298 299
  **Error codes**

300
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
301 302 303 304

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getValueAt method cannot be bound. |
305
| 10200001 | The value of index is out of range. |
G
Gloria 已提交
306

W
wusongqing 已提交
307 308
**Example**

G
Gloria 已提交
309 310 311 312 313 314
```ts
let plainArray = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getValueAt(1);
```
W
wusongqing 已提交
315 316 317 318 319 320 321

### clone

clone(): PlainArray<T>

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

W
wusongqing 已提交
322 323
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
324 325 326 327 328 329
**Return value**

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

G
Gloria 已提交
330 331
**Error codes**

332
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
333 334 335 336 337

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clone method cannot be bound. |

W
wusongqing 已提交
338 339
**Example**

W
wusongqing 已提交
340
```ts
W
wusongqing 已提交
341
let plainArray = new PlainArray();
G
Gloria 已提交
342 343
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
344 345 346 347 348 349 350 351
let newPlainArray = plainArray.clone();
```


### add

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

W
wusongqing 已提交
352 353 354
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
355 356 357 358 359

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
360 361
| key | number | Yes| Key of the target element.|
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
362

G
Gloria 已提交
363 364
**Error codes**

365
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
366 367 368 369 370

| ID| Error Message|
| -------- | -------- |
| 10200011 | The add method cannot be bound. |

W
wusongqing 已提交
371 372
**Example**

W
wusongqing 已提交
373
```ts
W
wusongqing 已提交
374
let plainArray = new PlainArray();
G
Gloria 已提交
375
plainArray.add(1, "squirrel");
W
wusongqing 已提交
376 377 378 379 380 381 382
```


### remove

remove(key: number): T

W
wusongqing 已提交
383 384 385
Removes an element with the specified key from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
386 387 388 389 390

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
391
| key | number | Yes| Target key.|
W
wusongqing 已提交
392 393 394 395 396

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
397
| T | Value of the element removed.|
W
wusongqing 已提交
398

G
Gloria 已提交
399 400
**Error codes**

401
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
402 403 404 405 406

| ID| Error Message|
| -------- | -------- |
| 10200011 | The remove method cannot be bound. |

W
wusongqing 已提交
407 408
**Example**

W
wusongqing 已提交
409
```ts
W
wusongqing 已提交
410
let plainArray = new PlainArray();
G
Gloria 已提交
411 412
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
413 414 415 416 417 418 419 420
let result = plainArray.remove(2);
```


### removeAt

removeAt(index: number): T

W
wusongqing 已提交
421 422 423
Removes an element at the specified position from this container.

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

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
429
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
430 431 432 433 434

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
435
| T | Element removed.|
W
wusongqing 已提交
436

G
Gloria 已提交
437 438
**Error codes**

439
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
440 441 442 443 444

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeAt method cannot be bound. |

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

W
wusongqing 已提交
447
```ts
W
wusongqing 已提交
448
let plainArray = new PlainArray();
G
Gloria 已提交
449 450
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
451 452 453 454 455 456 457 458
let result = plainArray.removeAt(1);
```


### removeRangeFrom

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

W
wusongqing 已提交
459 460 461
Removes elements in a specified range from this container.

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

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
467 468
| index | number | Yes| Start position of the elements to remove.|
| size | number | Yes| Number of elements to remove.|
W
wusongqing 已提交
469 470 471 472 473

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
474
| number | Number of elements removed.|
W
wusongqing 已提交
475

G
Gloria 已提交
476 477
**Error codes**

478
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
479 480 481 482

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeRangeFrom method cannot be bound. |
483
| 10200001 | The value of index is out of range. |
G
Gloria 已提交
484

W
wusongqing 已提交
485 486
**Example**

W
wusongqing 已提交
487
```ts
W
wusongqing 已提交
488
let plainArray = new PlainArray();
G
Gloria 已提交
489 490
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
491 492 493 494 495 496 497 498
let result = plainArray.removeRangeFrom(1, 3);
```


### setValueAt

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

W
wusongqing 已提交
499 500 501
Sets a value for an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
502 503 504 505 506

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
507 508
| index | number | Yes| Position index of the target element.|
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
509

G
Gloria 已提交
510 511
**Error codes**

512
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
513 514 515 516

| ID| Error Message|
| -------- | -------- |
| 10200011 | The setValueAt method cannot be bound. |
517
| 10200001 | The value of index is out of range. |
G
Gloria 已提交
518

W
wusongqing 已提交
519 520
**Example**

W
wusongqing 已提交
521
```ts
W
wusongqing 已提交
522
let plainArray = new PlainArray();
G
Gloria 已提交
523 524
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
525 526 527 528 529 530 531 532
plainArray.setValueAt(1, 3546);
```


### toString

toString(): String

W
wusongqing 已提交
533 534 535
Obtains a string that contains all elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
536 537 538 539 540 541 542

**Return value**

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

G
Gloria 已提交
543 544
**Error codes**

545
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
546 547 548 549 550

| ID| Error Message|
| -------- | -------- |
| 10200011 | The toString method cannot be bound. |

W
wusongqing 已提交
551 552
**Example**

W
wusongqing 已提交
553
```ts
W
wusongqing 已提交
554
let plainArray = new PlainArray();
G
Gloria 已提交
555 556
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
557 558 559 560 561 562 563 564 565 566
let result = plainArray.toString();
```


### clear

clear(): void

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

W
wusongqing 已提交
567 568
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
569 570
**Error codes**

571
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
572 573 574 575 576

| ID| Error Message|
| -------- | -------- |
| 10200011 | The clear method cannot be bound. |

W
wusongqing 已提交
577 578
**Example**

W
wusongqing 已提交
579
```ts
W
wusongqing 已提交
580
let plainArray = new PlainArray();
G
Gloria 已提交
581 582
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
583 584 585 586 587 588
plainArray.clear();
```


### forEach

G
Gloria 已提交
589
forEach(callbackFn: (value: T, index?: number, PlainArray?: PlainArray<T>) => void, thisArg?: Object): void
W
wusongqing 已提交
590

W
wusongqing 已提交
591 592 593
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
594 595 596 597 598

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
599
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
G
Gloria 已提交
600
| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
W
wusongqing 已提交
601

G
Gloria 已提交
602
callbackFn
W
wusongqing 已提交
603 604
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
605
| value | T | Yes| Value of the element that is currently traversed.|
G
Gloria 已提交
606 607
| 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.|
W
wusongqing 已提交
608

G
Gloria 已提交
609 610
**Error codes**

611
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
612 613 614 615 616

| ID| Error Message|
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

W
wusongqing 已提交
617 618
**Example**

W
wusongqing 已提交
619
```ts
W
wusongqing 已提交
620
let plainArray = new PlainArray();
G
Gloria 已提交
621 622
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
623
plainArray.forEach((value, index) => {
G
Gloria 已提交
624
    console.log("value:" + value, "index:" + index);
W
wusongqing 已提交
625 626 627 628 629 630 631 632
});
```


### [Symbol.iterator]

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

G
Gloria 已提交
633
Obtains an iterator object that contains key-value pairs, where the key is of the number type.
W
wusongqing 已提交
634

W
wusongqing 已提交
635 636
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
637 638 639 640
**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
641
| IterableIterator<[number, T]> | Iterator obtained.|
W
wusongqing 已提交
642

G
Gloria 已提交
643 644
**Error codes**

645
For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
G
Gloria 已提交
646 647 648 649 650

| ID| Error Message|
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

W
wusongqing 已提交
651 652
**Example**

W
wusongqing 已提交
653
```ts
W
wusongqing 已提交
654
let plainArray = new PlainArray();
G
Gloria 已提交
655 656
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
657 658 659

// Method 1:
for (let item of plainArray) { 
G
Gloria 已提交
660
  console.log("key:" + item[0]);
W
wusongqing 已提交
661
  console.log("value:" + item[1]);
W
wusongqing 已提交
662 663 664 665 666 667
}

// Method 2:
let iter = plainArray[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
G
Gloria 已提交
668
  console.log("key:" + temp[0]);
W
wusongqing 已提交
669
  console.log("value:" + temp[1]);
W
wusongqing 已提交
670 671 672
  temp = iter.next().value;
}
```