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