js-apis-arraylist.md 14.0 KB
Newer Older
W
wusongqing 已提交
1 2
# Linear Container ArrayList

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 14
**ArrayList** is a linear data structure that is implemented based on arrays. **ArrayList** can dynamically adjust the capacity based on project requirements. It increases the capacity by 50% each time.

Similar to **ArrayList**, **[Vector](js-apis-vector.md)** is also implemented based on arrays and can dynamically adjust the capacity. It increases the capability by 100% each time.

When compared with **[LinkedList](js-apis-linkedlist.md)**, **ArrayList** is more efficient in random access but less efficient in the addition or removal operation, because its addition or removal operation affects the position of other elements in the container.

**Recommended use case**: Use **ArrayList** when elements in a container need to be frequently read.

W
wusongqing 已提交
15 16
## Modules to Import

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

## ArrayList

### Attributes

W
wusongqing 已提交
25 26
**System capability**: SystemCapability.Utils.Lang

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


### constructor

constructor()

A constructor used to create an **ArrayList** instance.

W
wusongqing 已提交
38 39
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
40 41
**Example**

W
wusongqing 已提交
42
```ts
W
wusongqing 已提交
43 44 45 46 47 48 49 50
let arrayList = new ArrayList();
```


### add

add(element: T): boolean

W
wusongqing 已提交
51 52 53
Adds an element at the end of this container.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
59
| element | T | Yes| Target element.|
W
wusongqing 已提交
60 61 62 63 64

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
65
| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
W
wusongqing 已提交
66 67 68

**Example**

W
wusongqing 已提交
69
  ```ts
W
wusongqing 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
  let arrayList = new ArrayList();
  let result = arrayList.add("a");
  let result1 = arrayList.add(1);
  let b = [1, 2, 3];
  let result2 = arrayList.add(b);
  let c = {name: "lala", age: "13"};
  let result3 = arrayList.add(false);
  ```

### insert

insert(element: T, index: number): void

W
wusongqing 已提交
83 84 85
Inserts an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
86 87 88 89 90

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
91 92
| element | T | Yes| Target element.|
| index | number | Yes| Index of the position where the element is to be inserted.|
W
wusongqing 已提交
93 94 95

**Example**

W
wusongqing 已提交
96
```ts
W
wusongqing 已提交
97 98 99 100 101 102 103 104 105 106
let arrayList = new ArrayList();
arrayList.insert("A", 0);
arrayList.insert(0, 1);
arrayList.insert(true, 2);
```

### has

has(element: T): boolean

W
wusongqing 已提交
107 108 109
Checks whether this container has the specified element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
110 111 112 113 114

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
115
| element | T | Yes| Target element.|
W
wusongqing 已提交
116 117 118 119 120

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
121
| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
W
wusongqing 已提交
122 123 124

**Example**

W
wusongqing 已提交
125
```ts
W
wusongqing 已提交
126 127 128 129 130 131 132 133 134 135
let arrayList = new ArrayList();
let result = arrayList.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
arrayList.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
let result1 = arrayList.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```

### getIndexOf

getIndexOf(element: T): number

W
wusongqing 已提交
136 137 138
Obtains the index of the first occurrence of the specified element in this container.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
144
| element | T | Yes| Target element.|
W
wusongqing 已提交
145 146 147 148 149

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
150
| number | Returns the position index if obtained; returns **-1** if the specified element is not found.|
W
wusongqing 已提交
151 152 153

**Example**

W
wusongqing 已提交
154
```ts
W
wusongqing 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(1);
arrayList.add(2);
arrayList.add(4);
let result = arrayList.getIndexOf(2);
```

### getLastIndexOf

getLastIndexOf(element: T): number

W
wusongqing 已提交
170 171 172
Obtains the index of the last occurrence of the specified element in this container.

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

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
178
| element | T | Yes| Target element.|
W
wusongqing 已提交
179 180 181 182 183

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
184
| number | Returns the position index if obtained; returns **-1** if the specified element is not found.|
W
wusongqing 已提交
185 186 187

**Example**

W
wusongqing 已提交
188
```ts
W
wusongqing 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(1);
arrayList.add(2);
arrayList.add(4);
let result = arrayList.getLastIndexOf(2);
```

### removeByIndex

removeByIndex(index: number): T

W
wusongqing 已提交
204 205 206
Removes an element with the specified position from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
207 208 209 210 211

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
212
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
213 214 215 216 217

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
218
| T | Element removed.|
W
wusongqing 已提交
219 220 221

**Example**

W
wusongqing 已提交
222
```ts
W
wusongqing 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(2);
arrayList.add(4);
let result = arrayList.removeByIndex(2);
```

### remove

remove(element: T): boolean

W
wusongqing 已提交
236 237 238
Removes the first occurrence of the specified element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
239 240 241 242 243

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
244
| element | T | Yes| Target element.|
W
wusongqing 已提交
245 246 247 248 249

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
250
| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
W
wusongqing 已提交
251 252 253

**Example**

W
wusongqing 已提交
254
```ts
W
wusongqing 已提交
255 256 257 258 259 260 261 262 263 264 265 266
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.remove(2);
```

### removeByRange

removeByRange(fromIndex: number, toIndex: number): void

W
wusongqing 已提交
267 268 269
Removes from this container all of the elements within a range, including the element at the start position but not that at the end position.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
270 271 272 273 274 275 276 277 278 279

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|

**Example**

W
wusongqing 已提交
280
```ts
W
wusongqing 已提交
281 282 283 284 285 286 287 288 289 290 291
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.removeByRange(2, 4);
arrayList.removeByRange(4, 3);
arrayList.removeByRange(2, 6);
```

### replaceAllElements
W
wusongqing 已提交
292

W
wusongqing 已提交
293 294 295
replaceAllElements(callbackfn: (value: T, index?: number, arrlist?: ArrayList<T>) => T,
thisArg?: Object): void

W
wusongqing 已提交
296 297 298
Replaces all elements in this container with new elements, and returns the new ones.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
299 300 301 302 303 304 305 306 307 308 309 310

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for the replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
311 312
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
W
wusongqing 已提交
313 314 315 316
| arrlist | ArrayList<T> | No| Instance that invokes the **replaceAllElements** method.|

**Example**

W
wusongqing 已提交
317
```ts
W
wusongqing 已提交
318 319 320 321 322
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
323
arrayList.replaceAllElements((value: number, index: number)=> {
W
wusongqing 已提交
324 325
  return value = 2 * value;
});
326
arrayList.replaceAllElements((value: number, index: number) => {
W
wusongqing 已提交
327 328 329 330 331
  return value = value - 2;
});
```

### forEach
W
wusongqing 已提交
332

W
wusongqing 已提交
333 334 335
forEach(callbackfn: (value: T, index?: number, arrlist?: ArrayList<T>) => void,
thisArg?: Object): void

W
wusongqing 已提交
336 337 338
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
339 340 341 342 343

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
344
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
345 346 347 348 349 350
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
351 352
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
W
wusongqing 已提交
353 354 355 356
| arrlist | ArrayList<T> | No| Instance that invokes the **forEach** method.|

**Example**

W
wusongqing 已提交
357
```ts
W
wusongqing 已提交
358 359 360 361 362 363
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.forEach((value, index) => {
W
wusongqing 已提交
364
  console.log("value:" + value, index);
W
wusongqing 已提交
365 366 367 368
});
```

### sort
W
wusongqing 已提交
369

W
wusongqing 已提交
370 371
sort(comparator?: (firstValue: T, secondValue: T) => number): void

W
wusongqing 已提交
372 373 374
Sorts elements in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
375 376 377 378 379 380 381 382 383 384 385

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Callback invoked for sorting.|

comparator

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
386 387
| firstValue | T | Yes| Previous element.|
| secondValue | T | Yes| Next element.|
W
wusongqing 已提交
388 389 390

**Example**

W
wusongqing 已提交
391
```ts
W
wusongqing 已提交
392 393 394 395 396
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
397 398
arrayList.sort((a: number, b: number) => a - b);
arrayList.sort((a: number, b: number) => b - a);
W
wusongqing 已提交
399 400 401 402
arrayList.sort();
```

### subArrayList
W
wusongqing 已提交
403

W
wusongqing 已提交
404 405
subArrayList(fromIndex: number, toIndex: number): ArrayList<T>

W
wusongqing 已提交
406 407 408
Obtains elements within a range in this container, including the element at the start position but not that at the end position, and returns these elements as a new **ArrayList** instance.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|

**Return value**

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

**Example**

W
wusongqing 已提交
425
```ts
W
wusongqing 已提交
426 427 428 429 430 431 432 433 434 435 436
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result1 = arrayList.subArrayList(2, 4);
let result2 = arrayList.subArrayList(4, 3);
let result3 = arrayList.subArrayList(2, 6);
```

### clear
W
wusongqing 已提交
437

W
wusongqing 已提交
438 439 440 441
clear(): void

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

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

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

W
wusongqing 已提交
446
```ts
W
wusongqing 已提交
447 448 449 450 451 452 453 454 455
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.clear();
```

### clone
W
wusongqing 已提交
456

W
wusongqing 已提交
457 458 459 460
clone(): ArrayList<T> 

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

W
wusongqing 已提交
461 462
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
463 464 465 466 467 468 469 470 471

**Return value**

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

**Example**

W
wusongqing 已提交
472
```ts
W
wusongqing 已提交
473 474 475 476 477 478 479 480 481
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.clone();
```

### getCapacity
W
wusongqing 已提交
482

W
wusongqing 已提交
483 484 485 486
getCapacity(): number

Obtains the capacity of this container.

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

W
wusongqing 已提交
489 490 491 492 493 494 495 496
**Return value**

| Type| Description|
| -------- | -------- |
| number | Capacity obtained.|

**Example**

W
wusongqing 已提交
497
```ts
W
wusongqing 已提交
498 499 500 501 502 503 504 505 506
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.getCapacity();
```

### convertToArray
W
wusongqing 已提交
507

W
wusongqing 已提交
508 509 510 511
convertToArray(): Array<T>

Converts this container into an array.

W
wusongqing 已提交
512 513
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
514 515 516 517 518 519 520 521
**Return value**

| Type| Description|
| -------- | -------- |
| Array<T> | Array obtained.|

**Example**

W
wusongqing 已提交
522
```ts
W
wusongqing 已提交
523 524 525 526 527 528 529 530 531
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.convertToArray();
```

### isEmpty
W
wusongqing 已提交
532

W
wusongqing 已提交
533 534
isEmpty(): boolean

W
wusongqing 已提交
535 536 537
Checks whether this container is empty (contains no element).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
538 539 540 541 542 543 544 545 546

**Return value**

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

**Example**

W
wusongqing 已提交
547
```ts
W
wusongqing 已提交
548 549 550 551 552 553 554 555 556
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
let result = arrayList.isEmpty();
```

### increaseCapacityTo
W
wusongqing 已提交
557

W
wusongqing 已提交
558 559 560 561
increaseCapacityTo(newCapacity: number): void

Increases the capacity of this container.

W
wusongqing 已提交
562 563
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
564 565 566 567 568 569 570 571
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| newCapacity | number | Yes| New capacity.|

**Example**

W
wusongqing 已提交
572
```ts
W
wusongqing 已提交
573 574 575 576 577 578 579 580 581 582
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.increaseCapacityTo(2);
arrayList.increaseCapacityTo(8);
```

### trimToCurrentLength
W
wusongqing 已提交
583

W
wusongqing 已提交
584 585 586 587
trimToCurrentLength(): void

Trims the capacity of this container to its current length.

W
wusongqing 已提交
588 589
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
590 591
**Example**

W
wusongqing 已提交
592
```ts
W
wusongqing 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);
arrayList.trimToCurrentLength();
```

### [Symbol.iterator]

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

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

W
wusongqing 已提交
607 608
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
609 610 611 612 613 614 615 616
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<T> | Iterator obtained.|

**Example**

W
wusongqing 已提交
617
```ts
W
wusongqing 已提交
618 619 620 621 622 623 624 625
let arrayList = new ArrayList();
arrayList.add(2);
arrayList.add(4);
arrayList.add(5);
arrayList.add(4);

// Method 1:
for (let item of arrayList) { 
W
wusongqing 已提交
626
  console.log("value:" + item); 
W
wusongqing 已提交
627 628 629 630 631 632
} 

// Method 2:
let iter = arrayList[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
633
  console.log("value:" + temp);
W
wusongqing 已提交
634 635 636
  temp = iter.next().value;
}
```