js-apis-plainarray.md 20.1 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
**PlainArray** stores key-value (KV) pairs. Each key must be unique, be of the number type, and have only one value.

G
Gloria 已提交
9
**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 已提交
10 11 12 13

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

G
Gloria 已提交
15 16 17
This topic uses the following to identify the use of generics:
- T: Type

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

W
wusongqing 已提交
20
```ts
W
wusongqing 已提交
21
import PlainArray from '@ohos.util.PlainArray';  
W
wusongqing 已提交
22 23 24 25 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 47 48 49 50 51 52
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

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

W
wusongqing 已提交
55
```ts
W
wusongqing 已提交
56
let plainArray = new PlainArray();
G
Gloria 已提交
57 58 59 60 61
try {
  let plainArray2 = PlainArray();
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
62 63 64 65 66 67 68 69 70
```


### isEmpty

isEmpty(): boolean

Checks whether this container is empty.

W
wusongqing 已提交
71 72
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
73 74 75 76 77 78
**Return value**

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

G
Gloria 已提交
79 80 81 82 83 84 85 86
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
87 88
**Example**

W
wusongqing 已提交
89
```ts
W
wusongqing 已提交
90 91
const plainArray = new PlainArray();
let result = plainArray.isEmpty();
G
Gloria 已提交
92 93 94 95 96
try {
  plainArray.isEmpty.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
97 98 99 100 101 102 103 104 105
```


### has

has(key: number): boolean

Checks whether this container contains the specified key.

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

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

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

**Return value**

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

G
Gloria 已提交
120 121 122 123 124 125 126 127
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
128 129
**Example**

W
wusongqing 已提交
130
```ts
W
wusongqing 已提交
131 132
let plainArray = new PlainArray();
plainArray.has(1);
G
Gloria 已提交
133
plainArray.add(1, "squirrel");
W
wusongqing 已提交
134
let result1 = plainArray.has(1);
G
Gloria 已提交
135 136 137 138 139
try {
  plainArray.has.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
140 141 142 143 144 145 146 147 148
```


### get

get(key: number): T

Obtains the value of the specified key in this container.

W
wusongqing 已提交
149 150
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
151 152 153 154
**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
155
| key | number | Yes| Target key.|
W
wusongqing 已提交
156 157 158 159 160 161 162

**Return value**

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

G
Gloria 已提交
163 164 165 166 167 168 169 170
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
171 172
**Example**

W
wusongqing 已提交
173
```ts
W
wusongqing 已提交
174
let plainArray = new PlainArray();
G
Gloria 已提交
175 176
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
177
let result = plainArray.get(1);
G
Gloria 已提交
178 179 180 181 182
try {
  plainArray.get.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
183 184 185 186 187
```


### getIndexOfKey

W
wusongqing 已提交
188
getIndexOfKey(key: number): number
W
wusongqing 已提交
189

W
wusongqing 已提交
190 191 192
Obtains the index of the first occurrence of an element with the specified key in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
193 194 195 196 197

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
198
| key | number | Yes| Target key.|
W
wusongqing 已提交
199 200 201 202 203

**Return value**

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

G
Gloria 已提交
206 207 208 209 210 211 212 213
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
214 215
**Example**

W
wusongqing 已提交
216
```ts
W
wusongqing 已提交
217
let plainArray = new PlainArray();
G
Gloria 已提交
218 219
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
220
let result = plainArray.getIndexOfKey(2);
G
Gloria 已提交
221 222 223 224 225
try {
  plainArray.getIndexOfKey.bind({}, 2)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
226 227 228 229 230
```


### getIndexOfValue

W
wusongqing 已提交
231
getIndexOfValue(value: T): number
W
wusongqing 已提交
232

W
wusongqing 已提交
233 234 235
Obtains the index of the first occurrence of an element with the specified value in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
236 237 238 239 240

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
241
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
242 243 244 245 246

**Return value**

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

G
Gloria 已提交
249 250 251 252 253 254 255 256
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
257 258
**Example**

W
wusongqing 已提交
259
```ts
W
wusongqing 已提交
260
let plainArray = new PlainArray();
G
Gloria 已提交
261 262 263
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getIndexOfValue("squirrel");
G
Gloria 已提交
264 265 266 267 268
try {
  plainArray.getIndexOfValue.bind({}, "squirrel")(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
269 270 271 272 273
```


### getKeyAt

W
wusongqing 已提交
274
getKeyAt(index: number): number
W
wusongqing 已提交
275

W
wusongqing 已提交
276 277 278
Obtains the key of the element at the specified position in this container.

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

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
284
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
285 286 287 288 289

**Return value**

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

G
Gloria 已提交
292 293 294 295 296 297 298 299
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
300 301
**Example**

W
wusongqing 已提交
302
```ts
W
wusongqing 已提交
303
let plainArray = new PlainArray();
G
Gloria 已提交
304 305
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
306
let result = plainArray.getKeyAt(1);
G
Gloria 已提交
307 308 309 310 311
try {
  plainArray.getKeyAt.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
312 313 314 315 316 317
```

### getValueAt

getValueAt(index: number): T

W
wusongqing 已提交
318 319 320
Obtains the value of an element at the specified position in this container.

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

**Parameters**

W
wusongqing 已提交
324 325 326
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
327 328 329

**Return value**

W
wusongqing 已提交
330 331 332
| Type| Description|
| -------- | -------- |
| T | Returns the value of the element if obtained; returns **undefined** otherwise.|
W
wusongqing 已提交
333

G
Gloria 已提交
334 335 336 337 338 339 340 341 342
  **Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The getValueAt method cannot be bound. |
| 10200001 | The value of parameters are out of range. |

W
wusongqing 已提交
343 344
**Example**

G
Gloria 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
```ts
let plainArray = new PlainArray();
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
let result = plainArray.getValueAt(1);
try {
  plainArray.getValueAt.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
try {
  plainArray.getValueAt(10);
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
```
W
wusongqing 已提交
361 362 363 364 365 366 367

### clone

clone(): PlainArray<T>

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

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

W
wusongqing 已提交
370 371 372 373 374 375
**Return value**

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

G
Gloria 已提交
376 377 378 379 380 381 382 383
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
384 385
**Example**

W
wusongqing 已提交
386
```ts
W
wusongqing 已提交
387
let plainArray = new PlainArray();
G
Gloria 已提交
388 389
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
390
let newPlainArray = plainArray.clone();
G
Gloria 已提交
391 392 393 394 395
try {
  plainArray.clone.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
396 397 398 399 400 401 402
```


### add

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

W
wusongqing 已提交
403 404 405
Adds an element to this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
406 407 408 409 410

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
411 412
| key | number | Yes| Key of the target element.|
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
413

G
Gloria 已提交
414 415 416 417 418 419 420 421
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
422 423
**Example**

W
wusongqing 已提交
424
```ts
W
wusongqing 已提交
425
let plainArray = new PlainArray();
G
Gloria 已提交
426
plainArray.add(1, "squirrel");
G
Gloria 已提交
427 428 429 430 431
try {
  plainArray.add.bind({}, "squirrel")(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
432 433 434 435 436 437 438
```


### remove

remove(key: number): T

W
wusongqing 已提交
439 440 441
Removes an element with the specified key from this container.

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

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
447
| key | number | Yes| Target key.|
W
wusongqing 已提交
448 449 450 451 452

**Return value**

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

G
Gloria 已提交
455 456 457 458 459 460 461 462
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
463 464
**Example**

W
wusongqing 已提交
465
```ts
W
wusongqing 已提交
466
let plainArray = new PlainArray();
G
Gloria 已提交
467 468
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
469
let result = plainArray.remove(2);
G
Gloria 已提交
470 471 472 473 474
try {
  plainArray.remove.bind({}, 2)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
475 476 477 478 479 480 481
```


### removeAt

removeAt(index: number): T

W
wusongqing 已提交
482 483 484
Removes an element at the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
485 486 487 488 489

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
490
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
491 492 493 494 495

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
496
| T | Element removed.|
W
wusongqing 已提交
497

G
Gloria 已提交
498 499 500 501 502 503 504 505
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
506 507
**Example**

W
wusongqing 已提交
508
```ts
W
wusongqing 已提交
509
let plainArray = new PlainArray();
G
Gloria 已提交
510 511
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
512
let result = plainArray.removeAt(1);
G
Gloria 已提交
513 514 515 516 517
try {
  plainArray.removeAt.bind({}, 1)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
518 519 520 521 522 523 524
```


### removeRangeFrom

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

W
wusongqing 已提交
525 526 527
Removes elements in a specified range from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
528 529 530 531 532

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
533 534
| index | number | Yes| Start position of the elements to remove.|
| size | number | Yes| Number of elements to remove.|
W
wusongqing 已提交
535 536 537 538 539

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
540
| number | Number of elements removed.|
W
wusongqing 已提交
541

G
Gloria 已提交
542 543 544 545 546 547 548 549 550
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The removeRangeFrom method cannot be bound. |
| 10200001 | The value of parameters are out of range. |

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
let result = plainArray.removeRangeFrom(1, 3);
G
Gloria 已提交
558 559 560 561 562 563 564 565 566 567
try {
  plainArray.removeRangeFrom.bind({}, 1, 3)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
try {
  plainArray.removeRangeFrom(10, 3);
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
568 569 570 571 572 573 574
```


### setValueAt

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

W
wusongqing 已提交
575 576 577
Sets a value for an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
578 579 580 581 582

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
583 584
| index | number | Yes| Position index of the target element.|
| value | T | Yes| Value of the target element.|
W
wusongqing 已提交
585

G
Gloria 已提交
586 587 588 589 590 591 592 593 594
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The setValueAt method cannot be bound. |
| 10200001 | The value of parameters are out of range. |

W
wusongqing 已提交
595 596
**Example**

W
wusongqing 已提交
597
```ts
W
wusongqing 已提交
598
let plainArray = new PlainArray();
G
Gloria 已提交
599 600
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
601
plainArray.setValueAt(1, 3546);
G
Gloria 已提交
602 603 604 605 606 607 608 609 610 611
try {
  plainArray.setValueAt.bind({}, 1, 3546)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
try {
  plainArray.setValueAt(10, 3);
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
612 613 614 615 616 617 618
```


### toString

toString(): String

W
wusongqing 已提交
619 620 621
Obtains a string that contains all elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
622 623 624 625 626 627 628

**Return value**

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

G
Gloria 已提交
629 630 631 632 633 634 635 636
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
637 638
**Example**

W
wusongqing 已提交
639
```ts
W
wusongqing 已提交
640
let plainArray = new PlainArray();
G
Gloria 已提交
641 642
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
643
let result = plainArray.toString();
G
Gloria 已提交
644 645 646 647 648
try {
  plainArray.toString.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
649 650 651 652 653 654 655 656 657
```


### clear

clear(): void

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

W
wusongqing 已提交
658 659
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
660 661 662 663 664 665 666 667
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
668 669
**Example**

W
wusongqing 已提交
670
```ts
W
wusongqing 已提交
671
let plainArray = new PlainArray();
G
Gloria 已提交
672 673
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
674
plainArray.clear();
G
Gloria 已提交
675 676 677 678 679
try {
  plainArray.clear.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
680 681 682 683 684
```


### forEach

W
wusongqing 已提交
685
forEach(callbackfn: (value: T, index?: number, PlainArray?: PlainArray<T>) => void, thisArg?: Object): void
W
wusongqing 已提交
686

W
wusongqing 已提交
687 688 689
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
690 691 692 693 694

**Parameters**

| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
695
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
696 697 698 699 700
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
701 702
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Key of the element that is currently traversed.|
W
wusongqing 已提交
703
| PlainArray | PlainArray<T>| No| Instance that invokes the **forEach** API.|
W
wusongqing 已提交
704

G
Gloria 已提交
705 706 707 708 709 710 711 712
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
713 714
**Example**

W
wusongqing 已提交
715
```ts
W
wusongqing 已提交
716
let plainArray = new PlainArray();
G
Gloria 已提交
717 718
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
719 720
plainArray.forEach((value, index) => {
  console.log("value:" + value, index);
W
wusongqing 已提交
721
});
G
Gloria 已提交
722 723 724 725 726 727 728
try {
  plainArray.forEach.bind({}, (value, index) => {
    console.log("value:" + value, index);
  })(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
729 730 731 732 733 734 735
```


### [Symbol.iterator]

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

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

W
wusongqing 已提交
738 739
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
740 741 742 743
**Return value**

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

G
Gloria 已提交
746 747 748 749 750 751 752 753
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

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

W
wusongqing 已提交
754 755
**Example**

W
wusongqing 已提交
756
```ts
W
wusongqing 已提交
757
let plainArray = new PlainArray();
G
Gloria 已提交
758 759
plainArray.add(1, "squirrel");
plainArray.add(2, "sparrow");
W
wusongqing 已提交
760 761 762

// Method 1:
for (let item of plainArray) { 
G
Gloria 已提交
763
  console.log("key:" + item[0]);
W
wusongqing 已提交
764
  console.log("value:" + item[1]);
W
wusongqing 已提交
765 766 767 768 769 770
}

// Method 2:
let iter = plainArray[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
G
Gloria 已提交
771
  console.log("key:" + temp[0]);
W
wusongqing 已提交
772
  console.log("value:" + temp[1]);
W
wusongqing 已提交
773 774
  temp = iter.next().value;
}
G
Gloria 已提交
775 776 777 778 779
try {
  plainArray[Symbol.iterator].bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
780
```