js-apis-stack.md 6.9 KB
Newer Older
G
Gloria 已提交
1
# @ohos.util.Stack (Linear Container Stack)
W
wusongqing 已提交
2

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

W
wusongqing 已提交
7 8 9 10 11
**Stack** is implemented based on the array data structure. It follows the principle Last Out First In (LOFI) and supports data insertion and removal at one end.

Unlike **[Queue](js-apis-queue.md)**, which is implemented based on the queue data structure and supports insertion at one end and removal at the other end, **Stack** supports insertion and removal at the same end.

**Recommended use case**: Use **Stack** in LOFI scenarios.
W
wusongqing 已提交
12

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

W
wusongqing 已提交
16 17
## Modules to Import

W
wusongqing 已提交
18
```ts
W
wusongqing 已提交
19
import Stack from '@ohos.util.Stack';  
W
wusongqing 已提交
20 21 22 23 24 25
```

## Stack

### Attributes

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

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


### constructor

constructor()

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

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

G
Gloria 已提交
41 42 43 44 45 46 47 48
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200012 | The Stack's constructor cannot be directly invoked. |

W
wusongqing 已提交
49 50
**Example**

W
wusongqing 已提交
51
```ts
W
wusongqing 已提交
52 53 54 55 56 57 58 59
let stack = new Stack();
```


### push

push(item: T): T

W
wusongqing 已提交
60 61 62
Adds an element at the top of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
63 64 65 66 67

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
68
| item | T | Yes| Target element.|
W
wusongqing 已提交
69 70 71 72 73 74 75

**Return value**

| Type| Description|
| -------- | -------- |
| T | Element added.|

G
Gloria 已提交
76 77 78 79 80 81 82 83
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The push method cannot be bound. |

W
wusongqing 已提交
84 85
**Example**

W
wusongqing 已提交
86
```ts
W
wusongqing 已提交
87 88 89 90
let stack = new Stack();
let result = stack.push("a");
let result1 = stack.push(1);
let b = [1, 2, 3];
G
Gloria 已提交
91
let result2 = stack.push(b);
G
Gloria 已提交
92
let c = {name : "Dylon", age : "13"};
W
wusongqing 已提交
93 94 95 96 97 98 99
let result3 = stack.push(c);
```

### pop

pop(): T

W
wusongqing 已提交
100 101 102
Removes the top element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
103 104 105 106 107

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
108
| T | Element removed.|
W
wusongqing 已提交
109

G
Gloria 已提交
110 111 112 113 114 115 116 117
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The pop method cannot be bound. |

W
wusongqing 已提交
118 119
**Example**

W
wusongqing 已提交
120
```ts
W
wusongqing 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(4);
let result = stack.pop();
```

### peek

peek(): T

W
wusongqing 已提交
134 135 136
Obtains the top element of this container.

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

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
142
| T | Element obtained.|
W
wusongqing 已提交
143

G
Gloria 已提交
144 145 146 147 148 149 150 151
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The peek method cannot be bound. |

W
wusongqing 已提交
152 153
**Example**

W
wusongqing 已提交
154
```ts
W
wusongqing 已提交
155 156 157 158 159 160 161 162 163 164 165 166
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.peek();
```

### locate

locate(element: T): number

W
wusongqing 已提交
167 168 169
Obtains the index of the first occurrence of the specified element in this container.

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

**Parameters**

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

**Return value**

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

G
Gloria 已提交
183 184 185 186 187 188 189 190
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The locate method cannot be bound. |

W
wusongqing 已提交
191 192
**Example**

W
wusongqing 已提交
193
```ts
W
wusongqing 已提交
194 195 196 197 198 199 200 201 202
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(2);
```

### forEach
W
wusongqing 已提交
203

G
Gloria 已提交
204
forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void,
W
wusongqing 已提交
205 206
thisArg?: Object): void

W
wusongqing 已提交
207 208 209
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
210 211 212 213 214

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
G
Gloria 已提交
215
| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
216 217 218 219 220 221
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
222 223
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
W
wusongqing 已提交
224 225
| stack | Stack<T> | No| Instance that invokes the **forEach** method.|

G
Gloria 已提交
226 227 228 229 230 231 232 233
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The forEach method cannot be bound. |

W
wusongqing 已提交
234 235
**Example**

W
wusongqing 已提交
236
```ts
W
wusongqing 已提交
237 238 239 240 241 242
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value, index) => {
W
wusongqing 已提交
243
 console.log("value:" + value, index);
W
wusongqing 已提交
244 245 246 247
});
```

### isEmpty
W
wusongqing 已提交
248

W
wusongqing 已提交
249 250
isEmpty(): boolean

W
wusongqing 已提交
251 252 253
Checks whether this container is empty (contains no elements).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
254 255 256 257 258 259 260

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

G
Gloria 已提交
261 262 263 264 265 266 267 268
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The isEmpty method cannot be bound. |

W
wusongqing 已提交
269 270
**Example**

W
wusongqing 已提交
271
```ts
W
wusongqing 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
let result = stack.isEmpty();
```

### [Symbol.iterator]

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

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

W
wusongqing 已提交
286 287
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
288 289 290 291 292 293
**Return value**

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

G
Gloria 已提交
294 295 296 297 298 299 300 301
**Error codes**

For details about the error codes, see [containers Error Codes](../errorcodes/errorcode-containers.md).

| ID| Error Message|
| -------- | -------- |
| 10200011 | The Symbol.iterator method cannot be bound. |

W
wusongqing 已提交
302
**Example**
W
wusongqing 已提交
303
```ts
W
wusongqing 已提交
304 305 306 307 308 309 310 311
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);

// Method 1:
for (let item of stack) { 
W
wusongqing 已提交
312
  console.log("value:" + item); 
W
wusongqing 已提交
313 314 315 316 317 318
}

// Method 2:
let iter = stack[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
319
  console.log("value:" + temp);
W
wusongqing 已提交
320 321 322
  temp = iter.next().value;
}
```