1# @ohos.util.Vector (Linear Container Vector) 2 3**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. 4 5Both **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%. 6 7**Recommended use case**: Use **Vector** when the data volume is large. 8 9This topic uses the following to identify the use of generics: 10- T: Type 11 12> **NOTE** 13> 14> 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. 15> 16> The APIs provided by this module are deprecated since API version 9. You are advised to use [@ohos.util.ArrayList](js-apis-arraylist.md). 17 18## Modules to Import 19 20```ts 21import Vector from '@ohos.util.Vector'; 22``` 23 24 25## Vector 26 27### Attributes 28 29**System capability**: SystemCapability.Utils.Lang 30 31| Name| Type| Readable| Writable| Description| 32| -------- | -------- | -------- | -------- | -------- | 33| length | number | Yes| No| Number of elements in a vector (called container later).| 34 35 36### constructor 37 38constructor() 39 40A constructor used to create a **Vector** instance. 41 42**System capability**: SystemCapability.Utils.Lang 43 44**Example** 45 46```ts 47let vector = new Vector(); 48``` 49 50 51### add 52 53add(element: T): boolean 54 55Adds an element at the end of this container. 56 57**System capability**: SystemCapability.Utils.Lang 58 59**Parameters** 60 61| Name| Type| Mandatory| Description| 62| -------- | -------- | -------- | -------- | 63| element | T | Yes| Target element.| 64 65**Return value** 66 67| Type| Description| 68| -------- | -------- | 69| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.| 70 71**Example** 72 73```ts 74let vector = new Vector(); 75let result = vector.add("a"); 76let result1 = vector.add(1); 77let b = [1, 2, 3]; 78let result2 = vector.add(b); 79let c = {name : "Dylon", age : "13"}; 80let result3 = vector.add(c); 81``` 82 83### insert 84 85insert(element: T, index: number): void 86 87Inserts an element at the specified position in this container. 88 89**System capability**: SystemCapability.Utils.Lang 90 91**Parameters** 92 93| Name| Type| Mandatory| Description| 94| -------- | -------- | -------- | -------- | 95| element | T | Yes| Target element.| 96| index | number | Yes| Index of the position where the element is to be inserted.| 97 98**Example** 99 100```ts 101let vector = new Vector(); 102vector.insert("A", 0); 103vector.insert(0, 1); 104vector.insert(true, 2); 105``` 106 107### has 108 109has(element: T): boolean 110 111Checks whether this container has the specified element. 112 113**System capability**: SystemCapability.Utils.Lang 114 115**Parameters** 116 117| Name| Type| Mandatory| Description| 118| -------- | -------- | -------- | -------- | 119| element | T | Yes| Target element.| 120 121**Return value** 122 123| Type| Description| 124| -------- | -------- | 125| boolean | Returns **true** if the element is contained; returns **false** otherwise.| 126 127**Example** 128 129```ts 130let vector = new Vector(); 131let result = vector.has("squirrel"); 132vector.add("squirrel"); 133let result1 = vector.has("squirrel"); 134``` 135 136### getIndexOf 137 138getIndexOf(element: T): number 139 140Obtains the index of the first occurrence of the specified element in this container. 141 142**System capability**: SystemCapability.Utils.Lang 143 144**Parameters** 145 146| Name| Type| Mandatory| Description| 147| -------- | -------- | -------- | -------- | 148| element | T | Yes| Target element.| 149 150**Return value** 151 152| Type| Description| 153| -------- | -------- | 154| number | Returns the position index if obtained; returns **-1** otherwise.| 155 156**Example** 157 158```ts 159let vector = new Vector(); 160vector.add(2); 161vector.add(4); 162vector.add(5); 163vector.add(2); 164vector.add(1); 165vector.add(2); 166vector.add(4); 167let result = vector.getIndexOf(2); 168``` 169 170### getLastIndexOf 171 172getLastIndexOf(element: T): number 173 174Obtains the index of the last occurrence of the specified element in this container. 175 176**System capability**: SystemCapability.Utils.Lang 177 178**Parameters** 179 180| Name| Type| Mandatory| Description| 181| -------- | -------- | -------- | -------- | 182| element | T | Yes| Target element.| 183 184**Return value** 185 186| Type| Description| 187| -------- | -------- | 188| number | Returns the position index if obtained; returns **-1** otherwise.| 189 190**Example** 191 192```ts 193let vector = new Vector(); 194vector.add(2); 195vector.add(4); 196vector.add(5); 197vector.add(2); 198vector.add(1); 199vector.add(2); 200vector.add(4); 201let result = vector.getLastIndexOf(2); 202``` 203 204### removeByIndex 205 206removeByIndex(index: number): T 207 208Removes an element at the specified position from this container. 209 210**System capability**: SystemCapability.Utils.Lang 211 212**Parameters** 213 214| Name| Type| Mandatory| Description| 215| -------- | -------- | -------- | -------- | 216| index | number | Yes| Position index of the target element.| 217 218**Return value** 219 220| Type| Description| 221| -------- | -------- | 222| T | Element removed.| 223 224**Example** 225 226```ts 227let vector = new Vector(); 228vector.add(2); 229vector.add(4); 230vector.add(5); 231vector.add(2); 232vector.add(4); 233let result = vector.removeByIndex(2); 234``` 235 236### remove 237 238remove(element: T): boolean 239 240Removes the first occurrence of the specified element from this container. 241 242**System capability**: SystemCapability.Utils.Lang 243 244**Parameters** 245 246| Name| Type| Mandatory| Description| 247| -------- | -------- | -------- | -------- | 248| element | T | Yes| Target element.| 249 250**Return value** 251 252| Type| Description| 253| -------- | -------- | 254| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.| 255 256**Example** 257 258```ts 259let vector = new Vector(); 260vector.add(2); 261vector.add(4); 262vector.add(5); 263vector.add(4); 264let result = vector.remove(2); 265``` 266 267### removeByRange 268 269removeByRange(fromIndex: number, toIndex: number): void 270 271Removes from this container all of the elements within a range, including the element at the start position but not that at the end position. 272 273**System capability**: SystemCapability.Utils.Lang 274 275**Parameters** 276 277| Name| Type| Mandatory| Description| 278| -------- | -------- | -------- | -------- | 279| fromIndex | number | Yes| Index of the start position.| 280| toIndex | number | Yes| Index of the end position.| 281 282**Example** 283 284```ts 285let vector = new Vector(); 286vector.add(2); 287vector.add(4); 288vector.add(5); 289vector.add(4); 290vector.removeByRange(2,4); 291``` 292 293### replaceAllElements 294 295replaceAllElements(callbackFn: (value: T, index?: number, vector?: Vector<T>) => T, 296thisArg?: Object): void 297 298Replaces all elements in this container with new elements, and returns the new ones. 299 300**System capability**: SystemCapability.Utils.Lang 301 302**Parameters** 303 304| Name| Type| Mandatory| Description| 305| -------- | -------- | -------- | -------- | 306| callbackFn | function | Yes| Callback invoked for replacement.| 307| thisArg | Object | No| Value to use when the callback is invoked.| 308 309callbackfn 310 311| Name| Type| Mandatory| Description| 312| -------- | -------- | -------- | -------- | 313| value | T | Yes| Value of the element that is currently traversed.| 314| index | number | No| Position index of the element that is currently traversed.| 315| vector | Vector<T> | No| Instance that invokes the **replaceAllElements** API.| 316 317**Example** 318 319```ts 320let vector = new Vector(); 321vector.add(2); 322vector.add(4); 323vector.add(5); 324vector.add(4); 325vector.replaceAllElements((value) => { 326 // Add the user operation logic based on the actual scenario. 327 return value; 328}); 329``` 330 331### forEach 332 333forEach(callbackFn: (value: T, index?: number, vector?: Vector<T>) => void, 334thisArg?: Object): void 335 336Uses a callback to traverse the elements in this container and obtain their position indexes. 337 338**System capability**: SystemCapability.Utils.Lang 339 340**Parameters** 341 342| Name| Type| Mandatory| Description| 343| -------- | -------- | -------- | -------- | 344| callbackFn | function | Yes| Callback invoked for replacement.| 345| thisArg | Object | No| Value to use when the callback is invoked.| 346 347callbackfn 348 349| Name| Type| Mandatory| Description| 350| -------- | -------- | -------- | -------- | 351| value | T | Yes| Value of the element that is currently traversed.| 352| index | number | No| Position index of the element that is currently traversed.| 353| vector | Vector<T> | No| Instance that invokes the **forEach** API.| 354 355**Example** 356 357```ts 358let vector = new Vector(); 359vector.add(2); 360vector.add(4); 361vector.add(5); 362vector.add(4); 363vector.forEach((value, index) => { 364 console.log("value:" + value, "index:" + index); 365}); 366 367``` 368 369### sort 370 371sort(comparator?: (firstValue: T, secondValue: T) => number): void 372 373Sorts elements in this container. 374 375**System capability**: SystemCapability.Utils.Lang 376 377**Parameters** 378 379| Name| Type| Mandatory| Description| 380| -------- | -------- | -------- | -------- | 381| comparator | function | No| Callback invoked for sorting.| 382 383comparator 384 385| Name| Type| Mandatory| Description| 386| -------- | -------- | -------- | -------- | 387| firstValue | T | Yes| Previous element.| 388| secondValue | T | Yes| Next element.| 389 390**Example** 391 392```ts 393let vector = new Vector(); 394vector.add(2); 395vector.add(4); 396vector.add(5); 397vector.add(4); 398vector.sort((a: number, b: number) => a - b); 399vector.sort((a: number, b: number) => b - a); 400vector.sort(); 401``` 402 403### subVector 404 405subVector(fromIndex: number, toIndex: number): Vector<T> 406 407Obtains 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. 408 409**System capability**: SystemCapability.Utils.Lang 410 411**Parameters** 412 413| Name| Type| Mandatory| Description| 414| -------- | -------- | -------- | -------- | 415| fromIndex | number | Yes| Index of the start position.| 416| toIndex | number | Yes| Index of the end position.| 417 418**Return value** 419 420| Type| Description| 421| -------- | -------- | 422| Vector<T> | New **Vector** instance obtained.| 423 424**Example** 425 426```ts 427let vector = new Vector(); 428vector.add(2); 429vector.add(4); 430vector.add(5); 431vector.add(4); 432vector.add(6); 433vector.add(8); 434let result = vector.subVector(0,4); 435let result1 = vector.subVector(2,4); 436 437``` 438 439### clear 440 441clear(): void 442 443Clears all elements in this container and sets its length to **0**. 444 445**System capability**: SystemCapability.Utils.Lang 446 447**Example** 448 449```ts 450let vector = new Vector(); 451vector.add(2); 452vector.add(4); 453vector.add(5); 454vector.add(4); 455vector.clear(); 456``` 457 458### clone 459 460clone(): Vector<T> 461 462Clones this container and returns a copy. The modification to the copy does not affect the original instance. 463 464**System capability**: SystemCapability.Utils.Lang 465 466**Return value** 467 468| Type| Description| 469| -------- | -------- | 470| Vector<T> | New **Vector** instance obtained.| 471 472**Example** 473 474```ts 475let vector = new Vector(); 476vector.add(2); 477vector.add(4); 478vector.add(5); 479vector.add(4); 480let result = vector.clone(); 481``` 482 483### getCapacity 484 485getCapacity(): number 486 487Obtains the capacity of this container. 488 489**System capability**: SystemCapability.Utils.Lang 490 491**Return value** 492 493| Type| Description| 494| -------- | -------- | 495| number | Capacity obtained.| 496 497**Example** 498 499```ts 500let vector = new Vector(); 501vector.add(2); 502vector.add(4); 503vector.add(5); 504vector.add(4); 505let result = vector.getCapacity(); 506``` 507 508### convertToArray 509 510convertToArray(): Array<T> 511 512Converts this container into an array. 513 514**System capability**: SystemCapability.Utils.Lang 515 516**Return value** 517 518| Type| Description| 519| -------- | -------- | 520| Array<T> | Array obtained.| 521 522**Example** 523 524```ts 525let vector = new Vector(); 526vector.add(2); 527vector.add(4); 528vector.add(5); 529vector.add(4); 530let result = vector.convertToArray(); 531``` 532 533### isEmpty 534 535isEmpty(): boolean 536 537Checks whether this container is empty (contains no elements). 538 539**System capability**: SystemCapability.Utils.Lang 540 541**Return value** 542 543| Type| Description| 544| -------- | -------- | 545| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 546 547**Example** 548 549```ts 550let vector = new Vector(); 551vector.add(2); 552vector.add(4); 553vector.add(5); 554vector.add(4); 555let result = vector.isEmpty(); 556``` 557 558### increaseCapacityTo 559 560increaseCapacityTo(newCapacity: number): void 561 562Increases the capacity of this container. 563 564**System capability**: SystemCapability.Utils.Lang 565 566**Parameters** 567 568| Name| Type| Mandatory| Description| 569| -------- | -------- | -------- | -------- | 570| newCapacity | number | Yes| New capacity.| 571 572**Example** 573 574```ts 575let vector = new Vector(); 576vector.add(2); 577vector.add(4); 578vector.add(5); 579vector.add(4); 580vector.increaseCapacityTo(2); 581vector.increaseCapacityTo(8); 582``` 583 584### trimToCurrentLength 585 586trimToCurrentLength(): void 587 588Trims the capacity of this container into its current length. 589 590**System capability**: SystemCapability.Utils.Lang 591 592**Example** 593 594```ts 595let vector = new Vector(); 596vector.add(2); 597vector.add(4); 598vector.add(5); 599vector.add(4); 600vector.trimToCurrentLength(); 601``` 602 603### toString 604 605toString(): string 606 607Uses commas (,) to concatenate elements in this container into a string. 608 609**System capability**: SystemCapability.Utils.Lang 610 611**Return value** 612 613| Type| Description| 614| -------- | -------- | 615| string | String obtained.| 616 617**Example** 618 619```ts 620let vector = new Vector(); 621vector.add(2); 622vector.add(4); 623vector.add(5); 624vector.add(4); 625let result = vector.toString(); 626``` 627 628### copyToArray 629 630copyToArray(array: Array<T>): void 631 632Copies elements in this container into an array to overwrite elements of the same position indexes. 633 634**System capability**: SystemCapability.Utils.Lang 635 636**Parameters** 637 638| Name| Type| Mandatory| Description| 639| -------- | -------- | -------- | -------- | 640| array | Array<T> | Yes| Array to which the elements in the container will be copied.| 641 642### getFirstElement 643 644getFirstElement(): T 645 646Obtains the first element in this container. 647 648**System capability**: SystemCapability.Utils.Lang 649 650**Return value** 651 652| Type| Description| 653| -------- | -------- | 654| T | The first element obtained.| 655 656**Example** 657 658```ts 659let vector = new Vector(); 660vector.add(2); 661vector.add(4); 662vector.add(5); 663vector.add(4); 664let result = vector.getFirstElement(); 665``` 666 667### getLastElement 668 669getLastElement(): T 670 671Obtains the last element in this container. 672 673**System capability**: SystemCapability.Utils.Lang 674 675**Return value** 676 677| Type| Description| 678| -------- | -------- | 679| T | The last element obtained.| 680 681**Example** 682 683```ts 684let vector = new Vector(); 685vector.add(2); 686vector.add(4); 687vector.add(5); 688vector.add(4); 689let result = vector.getLastElement(); 690``` 691 692### getLastIndexFrom 693 694getLastIndexFrom(element: T, index: number): number 695 696Searches for an element backward from the specified position index and returns the position index of the element. 697 698**System capability**: SystemCapability.Utils.Lang 699 700**Parameters** 701 702| Name| Type| Mandatory| Description| 703| -------- | -------- | -------- | -------- | 704| element | T | Yes| Target element.| 705| index | number | Yes| Position index where the search starts.| 706 707**Return value** 708 709| Type| Description| 710| -------- | -------- | 711| number | Returns the position index if obtained; returns **-1** otherwise.| 712 713**Example** 714 715```ts 716let vector = new Vector(); 717vector.add(2); 718vector.add(4); 719vector.add(5); 720vector.add(4); 721vector.add("a"); 722let result = vector.getLastIndexFrom(4,3); 723``` 724 725### getIndexFrom 726 727getIndexFrom(element: T, index: number): number 728 729Searches for an element forward from the specified position index and returns the position index of the element. 730 731**System capability**: SystemCapability.Utils.Lang 732 733**Parameters** 734 735| Name| Type| Mandatory| Description| 736| -------- | -------- | -------- | -------- | 737| element | T | Yes| Target element.| 738| index | number | Yes| Position index where the search starts.| 739 740**Return value** 741 742| Type| Description| 743| -------- | -------- | 744| number | Returns the position index if obtained; returns **-1** otherwise.| 745 746**Example** 747 748```ts 749let vector = new Vector(); 750vector.add(2); 751vector.add(4); 752vector.add(5); 753vector.add(4); 754vector.add("a"); 755let result = vector.getIndexFrom(4, 3); 756``` 757 758### setLength 759 760setLength(newSize: number): void 761 762Sets a new length for this container. 763 764**System capability**: SystemCapability.Utils.Lang 765 766**Parameters** 767 768| Name| Type| Mandatory| Description| 769| -------- | -------- | -------- | -------- | 770| newSize | number | Yes| New length to set.| 771 772**Example** 773 774```ts 775let vector = new Vector(); 776vector.add(2); 777vector.add(4); 778vector.add(5); 779vector.add(4); 780vector.setLength(8); 781vector.setLength(2); 782``` 783 784### get 785 786get(index: number): T 787 788Obtains an element at the specified position in this container. 789 790**System capability**: SystemCapability.Utils.Lang 791 792**Parameters** 793 794 | Name| Type| Mandatory| Description| 795 | -------- | -------- | -------- | -------- | 796 | index | number | Yes| Position index of the target element.| 797 798**Return value** 799 800 | Type| Description| 801 | -------- | -------- | 802 | T | Element obtained.| 803 804**Example** 805 806 ```ts 807 let vector = new Vector(); 808 vector.add(2); 809 vector.add(4); 810 vector.add(5); 811 vector.add(4); 812 let result = vector.get(2); 813 ``` 814### set 815 816set(index: number, element: T): T 817 818Replaces an element at the specified position in this container with a given element. 819 820**System capability**: SystemCapability.Utils.Lang 821 822**Parameters** 823 824| Name| Type| Mandatory| Description| 825| -------- | -------- | -------- | -------- | 826| index | number | Yes| Position index of the target element.| 827| element | T | Yes| Element to be used for replacement.| 828 829**Return value** 830 831 | Type| Description| 832 | -------- | -------- | 833 | T | New element.| 834 835### [Symbol.iterator] 836 837[Symbol.iterator]\(): IterableIterator<T> 838 839Obtains an iterator. Each item of the iterator is a JavaScript object. 840 841**System capability**: SystemCapability.Utils.Lang 842 843**Return value** 844| Type| Description| 845| -------- | -------- | 846| IterableIterator<T> | Iterator obtained.| 847 848**Example** 849 850```ts 851let vector = new Vector(); 852vector.add(2); 853vector.add(4); 854vector.add(5); 855vector.add(4); 856 857// Method 1: 858for (let item of vector) { 859 console.log("value:" + item); 860} 861 862// Method 2: 863let iter = vector[Symbol.iterator](); 864let temp = iter.next().value; 865while(temp != undefined) { 866 console.log("value:" + temp); 867 temp = iter.next().value; 868} 869``` 870