js-apis-deque.md 6.5 KB
Newer Older
W
wusongqing 已提交
1 2
# Linear Container Deque

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6
> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

W
wusongqing 已提交
7 8 9 10
Double-ended queue (deque) is a sequence container implemented based on the queue data structure that follows the principles of First In First Out (FIFO) and Last In First Out (LIFO). It allows insertion and removal of elements at both the ends. **Deque** can dynamically adjust the capacity based on project requirements. It doubles the capacity each time. **Deque** differs from **[Queue](js-apis-queue.md)** and **[Vector](js-apis-vector.md)** mainly in the following aspects:

**Queue** follows the principle of FIFO only and allows element removal at the front and insertion at the rear.

Z
zengyawen 已提交
11
**Vector** supports insertion and deletion of elements in between, as well as at both the ends. When compared with **Vector**, **Deque** is more efficient in inserting and removing header elements, but less efficient in accessing elements.
W
wusongqing 已提交
12 13

**Recommended use case**: Use **Deque** when you need to frequently insert or remove elements at both the ends of a container.
W
wusongqing 已提交
14

G
Gloria 已提交
15 16 17
This topic uses the following to identify the use of generics:
- T: Type

W
wusongqing 已提交
18 19
## Modules to Import

W
wusongqing 已提交
20
```ts
W
wusongqing 已提交
21
import Deque from '@ohos.util.Deque'; 
W
wusongqing 已提交
22 23 24 25 26 27
```

## Deque

### Attributes

W
wusongqing 已提交
28 29
**System capability**: SystemCapability.Utils.Lang

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

### constructor

constructor()

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

W
wusongqing 已提交
40 41
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
42 43
**Example**

W
wusongqing 已提交
44
```ts
W
wusongqing 已提交
45 46 47 48 49 50 51
let deque = new Deque();
```

### insertFront

insertFront(element: T): void

W
wusongqing 已提交
52 53 54
Inserts an element at the front of this container.

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

**Parameters**

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

**Example**

W
wusongqing 已提交
64
```ts
W
wusongqing 已提交
65
let deque = new Deque();
W
wusongqing 已提交
66 67 68 69
deque.insertFront("a");
deque.insertFront(1);
let b = [1, 2, 3];
deque.insertFront(b);
G
Gloria 已提交
70
let c = {name : "Dylon", age : "13"};
W
wusongqing 已提交
71 72 73 74 75 76 77
deque.insertFront(false);
```

### insertEnd

insertEnd(element: T): void

W
wusongqing 已提交
78 79 80
Inserts an element at the end of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
81 82 83 84 85

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
86
| element | T | Yes| Target element.|
W
wusongqing 已提交
87 88 89

**Example**

W
wusongqing 已提交
90
```ts
W
wusongqing 已提交
91
let deque = new Deque();
W
wusongqing 已提交
92 93 94 95
deque.insertEnd("a");
deque.insertEnd(1);
let b = [1, 2, 3];
deque.insertEnd(b);
G
Gloria 已提交
96
let c = {name : "Dylon", age : "13"};
W
wusongqing 已提交
97 98 99 100 101 102 103
deque.insertEnd(false);
```

### has

has(element: T): boolean

W
wusongqing 已提交
104 105 106
Checks whether this container has the specified element.

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

**Parameters**

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

**Return value**

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

**Example**

W
wusongqing 已提交
122
```ts
W
wusongqing 已提交
123
let deque = new Deque();
G
Gloria 已提交
124 125 126
let result = deque.has("squirrel");
deque.insertFront("squirrel");
let result1 = deque.has("squirrel");
W
wusongqing 已提交
127 128 129 130 131 132
```

### popFirst

popFirst(): T

W
wusongqing 已提交
133 134 135
Removes the first element of this container.

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

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
141
| T | Element removed.|
W
wusongqing 已提交
142 143 144

**Example**

W
wusongqing 已提交
145
```ts
W
wusongqing 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158
let deque = new Deque();
deque.insertFront(2);
deque.insertFront(4);
deque.insertEnd(5);
deque.insertFront(2);
deque.insertFront(4);
let result = deque.popFirst();
```

### popLast

popLast(): T

W
wusongqing 已提交
159 160 161
Removes the last element of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
162 163 164 165 166

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
167
| T | Element removed.|
W
wusongqing 已提交
168 169 170

**Example**

W
wusongqing 已提交
171
```ts
W
wusongqing 已提交
172 173 174 175 176 177 178 179 180 181
let deque = new Deque();
deque.insertFront(2);
deque.insertEnd(4);
deque.insertFront(5);
deque.insertFront(2);
deque.insertFront(4);
let result = deque.popLast();
```

### forEach
W
wusongqing 已提交
182

W
wusongqing 已提交
183 184 185
forEach(callbackfn: (value: T, index?: number, deque?: Deque<T>) => void,
thisArg?: Object): void

W
wusongqing 已提交
186 187 188
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
189 190 191 192 193

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
194
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
195 196 197 198 199 200 201
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the element that is currently traversed.|
W
wusongqing 已提交
202
| index | number | No| Position index of the element that is currently traversed.|
W
wusongqing 已提交
203 204 205 206
| deque | Deque<T> | No| Instance that invokes the **forEach** method.|

**Example**

W
wusongqing 已提交
207
```ts
W
wusongqing 已提交
208 209 210 211 212 213
let deque = new Deque();
deque.insertFront(2);
deque.insertEnd(4);
deque.insertFront(5);
deque.insertEnd(4);
deque.forEach((value, index) => {
W
wusongqing 已提交
214
  console.log("value:" + value, index);
W
wusongqing 已提交
215 216 217 218 219
});
```

### getFirst

W
wusongqing 已提交
220
getFirst(): T
W
wusongqing 已提交
221

W
wusongqing 已提交
222 223 224
Obtains the first element of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
225 226 227 228 229

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
230
| T | Element obtained.|
W
wusongqing 已提交
231 232 233

**Example**

W
wusongqing 已提交
234
```ts
W
wusongqing 已提交
235 236 237 238 239 240 241 242 243 244 245 246
let deque = new Deque();
deque.insertEnd(2);
deque.insertEnd(4);
deque.insertFront(5);
deque.insertFront(4);
let result = deque.getFirst();
```

### getLast

getLast(): T

W
wusongqing 已提交
247 248 249
Obtains the last element of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
250 251 252 253 254

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
255
| T | Element obtained.|
W
wusongqing 已提交
256 257 258

**Example**

W
wusongqing 已提交
259
```ts
W
wusongqing 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273
let deque = new Deque();
deque.insertFront(2);
deque.insertFront(4);
deque.insertFront(5);
deque.insertFront(4);
let result = deque.getLast();
```

### [Symbol.iterator]

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

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

W
wusongqing 已提交
274 275
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
276 277 278 279 280 281 282
**Return value**

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

**Example**
W
wusongqing 已提交
283
```ts
W
wusongqing 已提交
284 285 286 287 288 289 290 291
let deque = new Deque();
deque.insertFront(2);
deque.insertFront(4);
deque.insertFront(5);
deque.insertFront(4);

// Method 1:
for (let item of deque) { 
W
wusongqing 已提交
292
  console.log("value:" + item); 
W
wusongqing 已提交
293 294 295 296 297 298
}

// Method 2:
let iter = deque[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
299
  console.log("value:" + temp);
W
wusongqing 已提交
300 301 302
  temp = iter.next().value;
}
```