js-apis-vector.md 18.1 KB
Newer Older
W
wusongqing 已提交
1 2
# Linear Container Vector

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
**Vector** is a linear data structure that is implemented based on arrays. When the memory of a vector is used up, a larger contiguous memory area is automatically allocated, all the elements are copied to the new memory area, and the current memory area is reclaimed. **Vector** can be used to efficiently access elements.

Both **Vector** and **[ArrayList](js-apis-arraylist.md)** are implemented based on arrays, but **Vector** provides more interfaces for operating the arrays. Both of them can dynamically adjust the capacity. **Vector** doubles the capacity each time, whereas **ArrayList** increases the capacity by 50%.

**Recommended use case**: Use **Vector** when the data volume is large.
W
wusongqing 已提交
12 13 14

## Modules to Import

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


## Vector

### Attributes

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

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


### constructor

constructor()

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

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

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

W
wusongqing 已提交
41
```ts
W
wusongqing 已提交
42 43 44 45 46 47 48 49
let vector = new Vector();
```


### add

add(element: T): boolean

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

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

**Parameters**

W
wusongqing 已提交
56 57 58
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
W
wusongqing 已提交
59 60 61

**Return value**

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

**Example**

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

### insert

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

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

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
85 86 87

**Parameters**

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

**Example**

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

### has

has(element: T): boolean

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

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
109 110 111

**Parameters**

W
wusongqing 已提交
112 113 114
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
W
wusongqing 已提交
115 116 117

**Return value**

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

**Example**

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

### getIndexOf

getIndexOf(element: T): number

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

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

**Parameters**

W
wusongqing 已提交
141 142 143
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
W
wusongqing 已提交
144 145 146

**Return value**

W
wusongqing 已提交
147 148 149
| Type| Description|
| -------- | -------- |
| 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 157 158 159 160 161 162 163 164 165 166 167 168
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(2);
vector.add(1);
vector.add(2);
vector.add(4);
let result = vector.getIndexOf(2);
```

### getLastIndexOf

getLastIndexOf(element: T): number

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

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

**Parameters**

W
wusongqing 已提交
175 176 177
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
W
wusongqing 已提交
178 179 180

**Return value**

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

**Example**

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

### removeByIndex

removeByIndex(index: number): T

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

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
206 207 208

**Parameters**

W
wusongqing 已提交
209 210 211
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
212 213 214

**Return value**

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

**Example**

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

### remove

remove(element: T): boolean

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

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

**Parameters**

W
wusongqing 已提交
241 242 243
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
W
wusongqing 已提交
244 245 246

**Return value**

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

**Return value**

W
wusongqing 已提交
253
```ts
W
wusongqing 已提交
254 255 256 257 258 259 260 261 262
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.remove(2);
```

### removeByRange
W
wusongqing 已提交
263 264

removeByRange(fromIndex: number, toIndex: number): void
W
wusongqing 已提交
265

W
wusongqing 已提交
266 267 268
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 已提交
269 270 271

**Parameters**

W
wusongqing 已提交
272 273 274 275
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|
W
wusongqing 已提交
276 277 278

**Example**

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

### replaceAllElements
W
wusongqing 已提交
291

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

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

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
298 299 300

**Parameters**

W
wusongqing 已提交
301 302 303 304
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|
W
wusongqing 已提交
305 306 307

callbackfn

W
wusongqing 已提交
308 309 310 311 312
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
| vector | Vector<T> | No| Instance that invokes the **replaceAllElements** API.|
W
wusongqing 已提交
313 314 315

**Example**

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

### forEach
W
wusongqing 已提交
331

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

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

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
338 339 340

**Parameters**

W
wusongqing 已提交
341 342 343 344
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|
W
wusongqing 已提交
345 346 347

callbackfn

W
wusongqing 已提交
348 349 350 351 352
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
| vector | Vector<T> | No| Instance that invokes the **forEach** API.|
W
wusongqing 已提交
353 354 355

**Example**

W
wusongqing 已提交
356
```ts
W
wusongqing 已提交
357 358 359 360 361 362
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.forEach((value, index) => {
W
wusongqing 已提交
363
  console.log("value:" + value, index)
W
wusongqing 已提交
364 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

**Parameters**

W
wusongqing 已提交
378 379 380
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Callback invoked for sorting.|
W
wusongqing 已提交
381 382 383

comparator

W
wusongqing 已提交
384 385 386 387
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| 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 vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
W
wusongqing 已提交
397 398
vector.sort((a, b) => a - b);
vector.sort((a, b) => b - a);
W
wusongqing 已提交
399 400 401 402
vector.sort();
```

### subVector
W
wusongqing 已提交
403

W
wusongqing 已提交
404 405
subVector(fromIndex: number, toIndex: number): Vector<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 **Vector** instance.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
409 410 411

**Parameters**

W
wusongqing 已提交
412 413 414 415
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|
W
wusongqing 已提交
416 417 418

**Return value**

W
wusongqing 已提交
419 420 421
| Type| Description|
| -------- | -------- |
| Vector<T> | New **Vector** instance obtained.|
W
wusongqing 已提交
422 423 424

**Return value**

W
wusongqing 已提交
425
```ts
W
wusongqing 已提交
426 427 428 429 430 431 432 433 434 435 436 437
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.subVector(2,4);
let result1 = vector.subVector(4,3);
let result2 = vector.subVector(2,6);

```

### clear
W
wusongqing 已提交
438

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

W
wusongqing 已提交
441 442 443
Clears all elements in this container and sets its length to **0**.

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

**Return value**

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

### clone
W
wusongqing 已提交
457

W
wusongqing 已提交
458 459 460 461
clone(): Vector<T>

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

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

W
wusongqing 已提交
464 465
**Return value**

W
wusongqing 已提交
466 467 468
| Type| Description|
| -------- | -------- |
| Vector<T> | New **Vector** instance obtained.|
W
wusongqing 已提交
469 470 471

**Example**

W
wusongqing 已提交
472
```ts
W
wusongqing 已提交
473 474 475 476 477 478 479 480 481
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.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
**Return value**

W
wusongqing 已提交
491 492 493
| Type| Description|
| -------- | -------- |
| number | Capacity obtained.|
W
wusongqing 已提交
494 495 496

**Example**

W
wusongqing 已提交
497
```ts
W
wusongqing 已提交
498 499 500 501 502 503 504 505 506
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.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
**Return value**

W
wusongqing 已提交
516 517 518
| Type| Description|
| -------- | -------- |
| Array<T> | Array obtained.|
W
wusongqing 已提交
519 520 521

**Example**

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

### isEmpty
W
wusongqing 已提交
532

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

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

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

**Return value**

W
wusongqing 已提交
541 542 543
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
W
wusongqing 已提交
544 545 546

**Example**

W
wusongqing 已提交
547
```ts
W
wusongqing 已提交
548 549 550 551 552 553 554 555 556
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.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
**Parameters**

W
wusongqing 已提交
566 567 568
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| newCapacity | number | Yes| New capacity.|
W
wusongqing 已提交
569 570 571

**Example**

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

### trimToCurrentLength
W
wusongqing 已提交
583

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

Trims the capacity of this container into 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
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.trimToCurrentLength();
```

### toString

toString(): string

W
wusongqing 已提交
605 606 607
Uses commas (,) to concatenate elements in this container into a string.

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

**Return value**

W
wusongqing 已提交
611 612 613
| Type| Description|
| -------- | -------- |
| string | String obtained.|
W
wusongqing 已提交
614 615 616

**Example**

W
wusongqing 已提交
617
```ts
W
wusongqing 已提交
618 619 620 621 622 623 624 625 626 627 628
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.toSting();
```

### copyToArray

copyToArray(array: Array<T>): void
W
wusongqing 已提交
629

W
wusongqing 已提交
630 631 632
Copies elements in this container into an array to overwrite elements of the same position indexes.

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

**Parameters**

W
wusongqing 已提交
636 637 638
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| array | Array<T> | Yes| Array to which the elements in the container will be copied.|
W
wusongqing 已提交
639 640 641

**Example**

W
wusongqing 已提交
642
```ts
W
wusongqing 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let array = ["a", "b", "c", "d", "e", "f"];
let result = vector.copyToArray(array);
```

### getFirstElement

getFirstElement(): T

W
wusongqing 已提交
656 657 658
Obtains the first element in this container.

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

**Return value**

W
wusongqing 已提交
662 663 664
| Type| Description|
| -------- | -------- |
| T | The first element obtained.|
W
wusongqing 已提交
665 666 667

**Example**

W
wusongqing 已提交
668
```ts
W
wusongqing 已提交
669 670 671 672 673 674 675 676 677 678 679 680
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.getFirstElement();
```

### getLastElement

getLastElement(): T

W
wusongqing 已提交
681 682 683
Obtains the last element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
684 685 686

**Return value**

W
wusongqing 已提交
687 688 689
| Type| Description|
| -------- | -------- |
| T | The last element obtained.|
W
wusongqing 已提交
690 691 692

**Example**

W
wusongqing 已提交
693
```ts
W
wusongqing 已提交
694 695 696 697 698 699 700 701 702 703 704 705
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.getLastElement();
```

### getLastIndexFrom

getLastIndexFrom(element: T, index: number): number

W
wusongqing 已提交
706 707 708
Searches for an element backward from the specified position index and returns the position index of the element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
709 710 711

**Parameters**

W
wusongqing 已提交
712 713 714 715
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
| index | number | Yes| Position index where the search starts.|
W
wusongqing 已提交
716 717 718

**Return value**

W
wusongqing 已提交
719 720 721
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
722 723 724

**Example**

W
wusongqing 已提交
725
```ts
W
wusongqing 已提交
726 727 728 729 730 731 732 733 734 735 736 737 738
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.add("a");
let result = vector.getLastIndexFrom(4,3);
```

### getIndexFrom

getIndexFrom(element: T, index: number): number

W
wusongqing 已提交
739 740 741
Searches for an element forward from the specified position index and returns the position index of the element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
742 743 744

**Parameters**

W
wusongqing 已提交
745 746 747 748
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Target element.|
| index | number | Yes| Position index where the search starts.|
W
wusongqing 已提交
749 750 751

**Return value**

W
wusongqing 已提交
752 753 754
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
755 756 757

**Example**

W
wusongqing 已提交
758
```ts
W
wusongqing 已提交
759 760 761 762 763 764 765 766 767 768
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.add("a");
let result = vector.getIndexFrom(4, 3);
```

### setLength
W
wusongqing 已提交
769

W
wusongqing 已提交
770 771 772 773
setLength(newSize: number): void

Sets a new length for this container.

W
wusongqing 已提交
774 775
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
776 777
**Parameters**

W
wusongqing 已提交
778 779 780
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| newSize | number | Yes| New length to set.|
W
wusongqing 已提交
781 782 783

**Example**

W
wusongqing 已提交
784
```ts
W
wusongqing 已提交
785 786 787 788 789 790 791 792 793 794
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.setLength(8);
vector.setLength(2);
```

### get
W
wusongqing 已提交
795

W
wusongqing 已提交
796 797
get(index: number): T 

W
wusongqing 已提交
798 799 800
Obtains an element at the specified position in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
801 802 803

**Parameters**

W
wusongqing 已提交
804 805 806
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the target element.|
W
wusongqing 已提交
807 808 809

**Return value**

W
wusongqing 已提交
810 811 812
| Type| Description|
| -------- | -------- |
| T | Element obtained.|
W
wusongqing 已提交
813 814 815

**Example**

W
wusongqing 已提交
816
  ```ts
W
wusongqing 已提交
817 818 819 820 821 822 823 824
  let vector = new Vector();
  vector.add(2);
  vector.add(4);
  vector.add(5);
  vector.add(4);
  let result = vector.get(2);
  ```
### set
W
wusongqing 已提交
825

W
wusongqing 已提交
826 827
set(index: number, element: T): T

W
wusongqing 已提交
828 829 830
Replaces an element at the specified position in this container with a given element.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
831 832 833

**Parameters**

W
wusongqing 已提交
834 835 836 837
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the target element.|
| element | T | Yes| Element to be used for replacement.|
W
wusongqing 已提交
838 839 840

**Return value**

W
wusongqing 已提交
841 842 843
| Type| Description|
| -------- | -------- |
| T | New element.|
W
wusongqing 已提交
844 845 846

**Example**

W
wusongqing 已提交
847
  ```ts
W
wusongqing 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861
  let vector = new Vector();
  vector.add(2);
  vector.add(4);
  vector.add(5);
  vector.add(4);
  let result = vector.set(2, "A");
  ```

### [Symbol.iterator]

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

Obtains an iterator. Each item of the iterator is a JavaScript object.

W
wusongqing 已提交
862 863
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
864
**Return value**
W
wusongqing 已提交
865 866 867 868

| Type| Description|
| -------- | -------- |
| IterableIterator<T> | Iterator obtained.|
W
wusongqing 已提交
869 870 871

**Example**

W
wusongqing 已提交
872
```ts
W
wusongqing 已提交
873 874 875 876 877 878 879 880
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);

// Method 1:
for (let item of vector) { 
W
wusongqing 已提交
881
  console.log("value:" + item); 
W
wusongqing 已提交
882 883 884 885 886 887
} 

// Method 2:
let iter = vector[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
888
  console.log("value:" + temp);
W
wusongqing 已提交
889 890 891
  temp = iter.next().value;
}
```