未验证 提交 d7a335b4 编写于 作者: O openharmony_ci 提交者: Gitee

!1990 Done! 1609:improve containers class API interface reference description

Merge pull request !1990 from wusongqing/TR1609
# Nonlinear Container HashMap
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.
## Modules to Import
```
import HashMap from '@ohos.util.HashMap'
```
## System Capabilities
SystemCapability.Utils.Lang
## HashMap
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a hash map (called container later).|
### constructor
constructor()
A constructor used to create a **HashMap** instance.
**Example**
```
let hashMap = new HashMap();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entry).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const hashMap = new HashMap();
let result = hashMap.isEmpty();
```
### hasKey
hasKey(key: K): boolean
Checks whether this container contains the specified key.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
**Example**
```
let hashMap = new HashMap();
let result = hashMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = hashMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### hasValue
hasValue(value: V): boolean
Checks whether this container contains the specified value.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|
**Example**
```
let hashMap = new HashMap();
let result = hashMap.hasValue(123);
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = hashMap.hasValue(123);
```
### get
get(key: K): V
Obtains the value of the specified key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value obtained.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
let result = hashMap.get("sdfs");
```
### setAll
setAll(map: HashMap<K, V>): void
Adds all entries in a **HashMap** instance to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| map | HashMap<K, V> | Yes| **HashMap** instance whose entries are to be added to the current container.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
let newHashMap = new HashMap();
hashMap.setAll(newHashMap);
```
### set
set(key: K, value: V): Object
Adds an entry to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to add.|
| value | V | Yes| Value of the entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| Object | Container that contains the new entry.|
**Example**
```
let hashMap = new HashMap();
let result = hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
```
### remove
remove(key: K): V
Removes an entry with the specified key from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value of the entry removed.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
let result = hashMap.remove("sdfs");
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
hashMap.clear();
```
### keys
keys(): IterableIterator&lt;K&gt;
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;K&gt; | Iterator obtained.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
let iter = hashMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### values
values(): IterableIterator&lt;V&gt;
Obtains an iterator that contains all the values in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;V&gt; | Iterator obtained.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
let iter = hashMap.values();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### replace
replace(key: K, newValue: V): boolean
Replaces an entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to replace.|
| newValue | V | Yes| New value of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is replaced successfully; returns **false** otherwise.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("sdfs", 123);
let result = hashMap.replace("sdfs", 357);
```
### forEach
forEach(callbackfn: (value: V, key?: K, map?: HashMap<K, V>) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value of the entry that is currently traversed.|
| key | K | Yes| Key of the entry that is currently traversed.|
| map | HashMap<K, V> | No| Instance that invokes the **forEach** method.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("sdfs", 123);
hashMap.set("dfsghsf", 357);
hashMap.forEach((value, key) => {
console.log(value, key);
});
```
### entries
entries(): IterableIterator<[K, V]>
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
let iter = hashMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|
**Example**
```
let hashMap = new HashMap();
hashMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
hashMap.set("sdfs", 356);
// Method 1:
for (let item of hashMap) {
console.log("key: " + item[0]);
console.log("value: " + item[1]);
}
// Method 2:
let iter = hashMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
# Nonlinear Container LightWeightMap
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.
## Modules to Import
```
import LightWeightMap from '@ohos.util.LightWeightMap'
```
## System Capabilities
SystemCapability.Utils.Lang
## LightWeightMap
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a lightweight map (called container later).|
### constructor
constructor()
A constructor used to create a **LightWeightMap** instance.
**Example**
```
let lightWeightMap = new LightWeightMap();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entry).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const lightWeightMap = new LightWeightMap();
let result = lightWeightMap.isEmpty();
```
### hasAll
hasAll(map: LightWeightMap<K, V>): boolean
Checks whether this container contains all entries of the specified **LightWeightMap** instance.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| map | LightWeightMap<K, V> | Yes| **LightWeightMap** instance to be used for comparison.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if all the entries in the specified **LightWeightMap** instance are contained; returns **false** otherwise.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let map = new LightWeightMap();
map.set("sdfs", 356);
let result = lightWeightMap.hasAll(map);
```
### hasKey
hasKey(key: K): boolean;
Checks whether this container contains the specified key.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
**Example**
```
let lightWeightMap = new LightWeightMap();
let result = lightWeightMap.hasKey;
lightWeightMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = lightWeightMap.hasKey("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### hasValue
hasValue(value: V): boolean
Checks whether this container contains the specified value.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified value is contained; returns **false** otherwise.|
**Example**
```
let lightWeightMap = new LightWeightMap();
let result = lightWeightMap.hasValue(123);
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
let result1 = lightWeightMap.hasValue(123);
```
### increaseCapacityTo
increaseCapacityTo(minimumCapacity: number): void
Increases the capacity of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| minimumCapacity | number | Yes| Minimum number of entries to accommodate in this container.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.increaseCapacityTo(10);
```
### get
get(key: K): V
Obtains the value of the specified key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value of the key.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let result = lightWeightMap.get("sdfs");
```
### getIndexOfKey
getIndexOfKey(key: K): number
Obtains the index of the first occurrence of an entry with the specified key in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let result = lightWeightMap.getIndexOfKey("sdfs");
```
### getIndexOfValue
getIndexOfValue(value: V): number
Obtains the index of the first occurrence of an entry with the specified value in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let result = lightWeightMap.getIndexOfValue(123);
```
### getKeyAt
getKeyAt(index: number): K
Obtains the key of an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| K | Returns the key if obtained; returns **undefined** otherwise.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let result = lightWeightMap.getKeyAt(1);
```
### setAll
setAll(map: LightWeightMap<K, V>): void
Adds all entries in a **LightWeightMap** instance to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| map | LightWeightMap<K, V> | Yes| **LightWeightMap** instance whose entries are to be added to the current container.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let map = new LightWeightMap();
lightWeightMap.setAll(map);
```
### set
set(key: K, value: V): Object
Adds an entry to this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to add.|
| value | V | Yes| Value of the entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| Object | Container that contains the new entry.|
**Example**
```
let lightWeightMap = new LightWeightMap();
let result = lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
```
### remove
remove(key: K): V
Removes an entry with the specified key from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | K | Yes| Key of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value of the entry removed.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
lightWeightMap.remove("sdfs");
```
### removeAt
removeAt(index: number): boolean
Removes an entry at the specified position from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let result = lightWeightMap.removeAt(1);
```
### setValueAt
setValueAt(index: number, newValue: V): boolean
Sets a value for an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.|
| newValue | V | Yes| Value of the entry to set.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the value is set successfully; returns **false** otherwise.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
lightWeightMap.setValueAt(1, 3546);
```
### getValueAt
getValueAt(index: number): V
Obtains the value of an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.|
**Return value**
| Type| Description|
| -------- | -------- |
| V | Value obtained.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let result = lightWeightMap.getValueAt(1);
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
lightWeightMap.clear();
```
### keys
keys(): IterableIterator&lt;K&gt;
Obtains an iterator that contains all the keys in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;K&gt; | Iterator obtained.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.keys();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### values
values(): IterableIterator&lt;V&gt;
Obtains an iterator that contains all the values in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;V&gt; | Iterator obtained.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.values();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
### forEach
forEach(callbackfn: (value: V, key?: K, map?: LightWeightMap<K, V>) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | V | Yes| Value of the entry that is currently traversed.|
| key | K | Yes| Key of the entry that is currently traversed.|
| map | LightWeightMap<K, V> | No| Instance that invokes the **forEach** method.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("sdfs", 123);
lightWeightMap.set("dfsghsf", 357);
lightWeightMap.forEach((value, key) => {
console.log(value, key);
});
```
### entries
entries(): IterableIterator<[K, V]>
Obtains an iterator that contains all the entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.entries();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
### toString
toString(): string
Concatenates the entries in this container into a string and returns the string.
**Return value**
| Type| Description|
| -------- | -------- |
| string | Returns a string.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("A", 123);
lightWeightMap.set("sdfs", 356);
let iter = lightWeightMap.toString();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;[K, V]&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[K, V]> | Iterator obtained.|
**Example**
```
let lightWeightMap = new LightWeightMap();
lightWeightMap.set("Ahfbrgrbgnutfodgorrogorgrogofdfdf", 123);
lightWeightMap.set("sdfs", 356);
// Method 1:
for (let item of lightWeightMap) {
console.log("key: " + item[0]);
console.log("value: " + item[1]);
}
// Method 2:
let iter = lightWeightMap[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
# Linear Container LinkedList
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.
## Modules to Import
```
import LinkedList from '@ohos.util.LinkedList'
```
## System Capabilities
SystemCapability.Utils.Lang
## LinkedList
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a linked list (called container later).|
### constructor
constructor(head?: NodeObj&lt;T&gt;, tail?: NodeObj&lt;T&gt;)
A constructor used to create a **LinkedList** instance.
**Parameters**
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| head | NodeObj&lt;T&gt; | Yes| No| Node object, including **element**, **next**, and **prev**.|
| tail | NodeObj&lt;T&gt; | Yes| No| Node object, including **element**, **next**, and **prev**.|
**Example**
```
let linkedList = new LinkedList();
```
### add
add(element: T): boolean
Adds an entry at the end of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
let linkedList = new LinkedList();
let result = linkedList.add("a");
let result1 = linkedList.add(1);
let b = [1, 2, 3];
linkedList.add(b);
let c = {name : "lala", age : "13"};
let result3 = linkedList.add(false);
```
### addFirst
addFirst(element: T): void
Adds an entry at the top of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.|
**Example**
```
let linkedList = new LinkedList();
linkedList.addFirst("a");
linkedList.addFirst(1);
let b = [1, 2, 3];
linkedList.addFirst(b);
let c = {name : "lala", age : "13"};
linkedList.addFirst(false);
```
### insert
insert(index: number, element: T): void
Inserts an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.|
| index | number | Yes| Index of the position where the entry is to be inserted.|
**Example**
```
let linkedList = new LinkedList();
linkedList.insert(0, "A");
linkedList.insert(1, 0);
linkedList.insert(2, true);
```
### has
has(element: T): boolean
Checks whether this container has the specified entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified entry is contained; returns **false** otherwise.|
**Example**
```
let linkedList = new LinkedList();
let result1 = linkedList.has("Ahfbrgrbgnutfodgorrogorg");
linkedList.add("Ahfbrgrbgnutfodgorrogorg");
let result = linkedList.has("Ahfbrgrbgnutfodgorrogorg");
```
### get
get(index: number): T
Obtains an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry obtained.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.get(2);
```
### getLastIndexOf
getLastIndexOf(element: T): number;
Obtains the index of the last occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.getLastIndexOf(2);
```
### getIndexOf
getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(1);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.getIndexOf(2);
```
### removeByIndex
removeByIndex(index: number): T
Removes an entry at the specified position from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeByIndex(2);
```
### removeFirst
removeFirst(): T
Removes the first entry from this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeFirst();
```
### removeLast
removeLst(): T
Removes the last entry from this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(2);
linkedList.add(4);
let result = linkedList.removeLast();
```
### remove
remove(element: T): boolean
Removes the first occurrence of the specified entry from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.remove(2);
```
### removeFirstFound
removeFirstFound(element: T): boolean
Removes the first occurrence of the specified entry from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.removeFirstFound(4);
```
### removeLastFound
removeLastFound(element: T): boolean
Removes the last occurrence of the specified entry from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.removeLastFound(4);
```
### clone
clone(): LinkedList&lt;T&gt;
Clones this container and returns a copy.
The modification to the copy does not affect the original instance.
**Return value**
| Type| Description|
| -------- | -------- |
| LinkedList&lt;T&gt; | New **LinkedList** instance obtained.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.clone();
```
### forEach
forEach(callbackfn: (value: T, index?: number, LinkedList?: LinkedList&lt;T&gt;) => void,
thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| LinkedList | LinkedList&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.forEach((value, index) => {
console.log(value, index);
});
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.clear();
```
### set
set(index: number, element: T): T
Replaces an entry at the specified position in this container with a given entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to replace.|
| element | T | Yes| Entry to be used for replacement.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | New entry.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.set(2, "b");
```
### convertToArray
convertToArray(): Array&lt;T&gt;
Converts this container into an array.
**Return value**
| Type| Description|
| -------- | -------- |
| Array&lt;T&gt; | Array obtained.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.convertToArray();
```
### getFirst
getFirst(): T
Obtains the first entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Returns the entry if obtained; returns **undefined** otherwise.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
let result = linkedList.getFirst();
```
### getLast
getLast(): T
Obtains the last entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | Returns the entry if obtained; returns **undefined** otherwise.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
linkedList.getLast();
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;T&gt;;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let linkedList = new LinkedList();
linkedList.add(2);
linkedList.add(4);
linkedList.add(5);
linkedList.add(4);
// Method 1:
for (let item of linkedList) {
console.log(item);
}
// Method 2:
let iter = linkedList[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
# Nonlinear Container PlainArray
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.
## Modules to Import
```
import PlainArray from '@ohos.util.PlainArray'
```
## System Capabilities
SystemCapability.Utils.Lang
## PlainArray
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a plain array (called container later).|
### constructor
constructor()
A constructor used to create a **PlainArray** instance.
**Example**
```
let plainArray = new PlainArray();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty.
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
const plainArray = new PlainArray();
let result = plainArray.isEmpty();
```
### has
has(key: number): boolean
Checks whether this container contains the specified key.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | number | Yes| Key to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the specified key is contained; returns **false** otherwise.|
**Example**
```
let plainArray = new PlainArray();
plainArray.has(1);
plainArray.add(1, "sddfhf");
let result1 = plainArray.has(1);
```
### get
get(key: number): T
Obtains the value of the specified key in this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | number | Yes| Key to query.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value of the key.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.get(1);
```
### getIndexOfKey
getIndexOfKey(key: number): number;
Obtains the index of the first occurrence of an entry with the specified key in this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | number | Yes| Key of the entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.getIndexOfKey("sdfs");
```
### getIndexOfValue
getIndexOfValue(value: T): number;
Obtains the index of the first occurrence of an entry with the specified value in this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.getIndexOfValue("sddfhf");
```
### getKeyAt
getKeyAt(index: number): number;
Obtains the key of the entry at the specified position in this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the key of the entry if obtained; returns **-1** otherwise.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.getKeyAt(1);
```
### getValueAt
getValueAt(index: number): T
Obtains the value of an entry at the specified position in this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Returns the value of the entry if obtained; returns **undefined** otherwise.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.getKeyAt(1);
```
### clone
clone(): PlainArray&lt;T&gt;
Clones this container and returns a copy. The modification to the copy does not affect the original instance.
**Return value**
| Type| Description|
| -------- | -------- |
| PlainArray&lt;T&gt; | New **PlainArray** instance obtained.|
**Example**
```
let plainArray = new ArrayList();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let newPlainArray = plainArray.clone();
```
### add
add(key: number, value: T): void
Adds an entry to this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | number | Yes| Key of the entry to add.|
| value | T | Yes| Value of the entry to add.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
```
### remove
remove(key: number): T
Removes an entry with the specified key from this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | number | Yes| Key of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Value of the entry removed.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.remove(2);
let result = plainArray.remove(2);
```
### removeAt
removeAt(index: number): T
Removes an entry at the specified position from this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.removeAt(1);
let result = plainArray.removeAt(1);
```
### removeRangeFrom
removeRangeFrom(index: number, size: number): number
Removes entries in a specified range from this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Start position of the entries to remove.|
| size | number | Yes| Number of entries to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Number of entries removed.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.removeRangeFrom(1, 3);
```
### setValueAt
setValueAt(index: number, value: T): void
Sets a value for an entry at the specified position in this container.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry.|
| value | T | Yes| Value of the entry to set.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.setValueAt(1, 3546);
```
### toString
toString(): String
Obtains a string that contains all entries in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| String | String obtained.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
let result = plainArray.toString();
```
### clear
clear(): void
Clears this container and sets its length to **0**.
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.clear();
```
### forEach
forEach(callbackfn: (value: T, key?: number, PlainArray?: PlainArray<number, T>) => void, thisArg?: Object): void
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type | Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| key | number | Yes| Key of the entry that is currently traversed.|
| plainArray | PlainArray<number, T> | No| Instance that invokes the **forEach** method.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
plainArray.forEach((value, key) => {
console.log(value, key);
});
```
### [Symbol.iterator]
[Symbol.iterator]\(): IterableIterator&lt;[number, T]&gt;
Obtains an iterator, each item of which is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator<[number, T]> | Iterator obtained.|
**Example**
```
let plainArray = new PlainArray();
plainArray.add(1, "sddfhf");
plainArray.add(2, "sffdfhf");
// Method 1:
for (let item of plainArray) {
console.log("index: " + item[0]);
console.log("value: " + item[1]);
}
// Method 2:
let iter = plainArray[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp[0]);
console.log(temp[1]);
temp = iter.next().value;
}
```
# Linear Container Vector
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.
## Modules to Import
```
import Vector from '@ohos.util.Vector'
```
## System Capabilities
SystemCapability.Utils.Lang
## Vector
### Attributes
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a vector (called container later).|
### constructor
constructor()
A constructor used to create a **Vector** instance.
**Example**
```
let vector = new Vector();
```
### add
add(element: T): boolean
Adds an entry at the end of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to add.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is added successfully; returns **false** otherwise.|
**Example**
```
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
Inserts an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to insert.|
| index | number | Yes| Index of the position where the entry is to be inserted.|
**Example**
```
let vector = new Vector();
vector.insert("A", 0);
vector.insert(0, 1);
vector.insert(true, 2);
```
### has
has(element: T): boolean
Checks whether this container has the specified entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to check.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is contained; returns **false** otherwise.|
**Example**
```
let vector = new Vector();
let result = vector.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
vector.add("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
let result1 = vector.has("Ahfbrgrbgnutfodgorrogorgrogofdfdf");
```
### getIndexOf
getIndexOf(element: T): number
Obtains the index of the first occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
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
Obtains the index of the last occurrence of the specified entry in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|
**Example**
```
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
Removes an entry at the specified position from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry removed.|
**Example**
```
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
Removes the first occurrence of the specified entry from this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to remove.|
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the entry is removed successfully; returns **false** otherwise.|
**Return value**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.remove(2);
```
### removeByRange
removeByRange(fromIndex: number, toIndex: number): void;
Removes from this container all of the entries within a range, including the entry at the start position but not that at the end position.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fromIndex | number | Yes| Index of the start position.|
| toIndex | number | Yes| Index of the end position.|
**Example**
```
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
replaceAllElements(callbackfn: (value: T, index?: number, vector?: Vector&lt;T&gt;) => T,
thisArg?: Object): void
Replaces all entries in this container with new entries, and returns the new ones.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| vector | Vector&lt;T&gt; | No| Instance that invokes the **replaceAllElements** method.|
**Example**
```
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
forEach(callbackfn: (value: T, index?: number, vector?: Vector&lt;T&gt;) => void,
thisArg?: Object): void;
Uses a callback to traverse the entries in this container and obtain their position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked for replacement.|
| thisArg | Object | No| Value to use when the callback is invoked.|
callbackfn
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| vector | Vector&lt;T&gt; | No| Instance that invokes the **forEach** method.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.forEach((value, index) => {
console.log(value, index)
});
```
### sort
sort(comparator?: (firstValue: T, secondValue: T) => number): void
Sorts entries in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| comparator | function | No| Callback invoked for sorting.|
comparator
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| firstValue | T | Yes| Previous entry.|
| secondValue | T | Yes| Next entry.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.sort(a, (b => a - b));
vector.sort(a, (b => b - a));
vector.sort();
```
### subVector
subVector(fromIndex: number, toIndex: number): Vector&lt;T&gt;
Obtains entries within a range in this container, including the entry at the start position but not that at the end position, and returns these entries as a new **Vector** instance.
**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|
| -------- | -------- |
| Vector&lt;T&gt; | New **Vector** instance obtained.|
**Return value**
```
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
clear(): void
Clears all entries in this container and sets its length to **0**.
**Return value**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.clear();
```
### clone
clone(): Vector&lt;T&gt;
Clones this container and returns a copy. The modification to the copy does not affect the original instance.
**Return value**
| Type| Description|
| -------- | -------- |
| Vector&lt;T&gt; | New **Vector** instance obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.clone();
```
### getCapacity
getCapacity(): number
Obtains the capacity of this container.
**Return value**
| Type| Description|
| -------- | -------- |
| number | Capacity obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.getCapacity();
```
### convertToArray
convertToArray(): Array&lt;T&gt;
Converts this container into an array.
**Return value**
| Type| Description|
| -------- | -------- |
| Array&lt;T&gt; | Array obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.convertToArray();
```
### isEmpty
isEmpty(): boolean
Checks whether this container is empty (contains no entries).
**Return value**
| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.isEmpty();
```
### increaseCapacityTo
increaseCapacityTo(newCapacity: number): void
Increases the capacity of this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| newCapacity | number | Yes| New capacity.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.increaseCapacityTo(2);
vector.increaseCapacityTo(8);
```
### trimToCurrentLength
trimToCurrentLength(): void
Trims the capacity of this container into its current length.
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.trimToCurrentLength();
```
### toString
toString(): string
Uses commas (,) to concatenate entries in this container into a string.
**Return value**
| Type| Description|
| -------- | -------- |
| string | String obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.toSting();
```
### copyToArray
copyToArray(array: Array&lt;T&gt;): void
Copies entries in this container into an array to overwrite elements of the same position indexes.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| array | Array&lt;T&gt; | Yes| Array to which the entries in the container will be copied.|
**Example**
```
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
Obtains the first entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | The first entry obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.getFirstElement();
```
### getLastElement
getLastElement(): T
Obtains the last entry in this container.
**Return value**
| Type| Description|
| -------- | -------- |
| T | The last entry obtained.|
**Example**
```
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
Searches for an entry backward from the specified position index and returns the position index of the entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
| index | number | Yes| Position index where the search starts.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the entry is not found.|
**Example**
```
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
Searches for an entry forward from the specified position index and returns the position index of the entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|
| index | number | Yes| Position index where the search starts.|
**Return value**
| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the entry is not found.|
**Example**
```
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
setLength(newSize: number): void
Sets a new length for this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| newSize | number | Yes| New length to set.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
vector.setLength(8);
vector.setLength(2);
```
### get
get(index: number): T
Obtains an entry at the specified position in this container.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to obtain.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | Entry obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
let result = vector.get(2);
```
### set
set(index: number, element: T): T
Replaces an entry at the specified position in this container with a given entry.
**Parameters**
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| index | number | Yes| Position index of the entry to replace.|
| element | T | Yes| Entry to be used for replacement.|
**Return value**
| Type| Description|
| -------- | -------- |
| T | New entry.|
**Example**
```
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&lt;T&gt;
Obtains an iterator. Each item of the iterator is a JavaScript object.
**Return value**
| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|
**Example**
```
let vector = new Vector();
vector.add(2);
vector.add(4);
vector.add(5);
vector.add(4);
// Method 1:
for (let item of vector) {
console.log(item);
}
// Method 2:
let iter = vector[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
console.log(temp);
temp = iter.next().value;
}
```
......@@ -485,6 +485,7 @@ linkedList.clear();
### set
set(index: number, element: T): T
将此LinkedList中指定位置的元素替换为指定元素。
**参数:**
......
......@@ -722,6 +722,7 @@ vector.setLength(2);
### get
get(index: number): T
根据下标值获取Vector实例中的元素。
**参数:**
......@@ -748,13 +749,15 @@ get(index: number): T
```
### set
set(index: number, element: T): T
替换指定下标对应的元素。
将此Vector中指定位置的元素替换为指定元素。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 指定下标。 |
| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| index | number | 是 | 查找的下标值。 |
| element | T | 是 | 用来替换的元素。 |
**返回值:**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册