You need to sign in or sign up before continuing.
js-apis-stack.md 8.8 KB
Newer Older
W
wusongqing 已提交
1 2
# Linear Container Stack

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 26 27 28
```




## Stack

### Attributes

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

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


### constructor

constructor()

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

W
wusongqing 已提交
42 43
**System capability**: SystemCapability.Utils.Lang

G
Gloria 已提交
44 45 46 47 48 49 50 51
**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 已提交
52 53
**Example**

W
wusongqing 已提交
54
```ts
W
wusongqing 已提交
55
let stack = new Stack();
G
Gloria 已提交
56 57 58 59 60
try {
  let stack2 = Stack();
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
61 62 63 64 65 66 67
```


### push

push(item: T): T

W
wusongqing 已提交
68 69 70
Adds an element at the top of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
71 72 73 74 75

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
76
| item | T | Yes| Target element.|
W
wusongqing 已提交
77 78 79 80 81 82 83

**Return value**

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

G
Gloria 已提交
84 85 86 87 88 89 90 91
**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 已提交
92 93
**Example**

W
wusongqing 已提交
94
```ts
W
wusongqing 已提交
95 96 97 98
let stack = new Stack();
let result = stack.push("a");
let result1 = stack.push(1);
let b = [1, 2, 3];
G
Gloria 已提交
99
let result2 = stack.push(b);
G
Gloria 已提交
100
let c = {name : "Dylon", age : "13"};
W
wusongqing 已提交
101
let result3 = stack.push(c);
G
Gloria 已提交
102 103 104 105 106
try {
  stack.push.bind({}, "b")(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
107 108 109 110 111 112
```

### pop

pop(): T

W
wusongqing 已提交
113 114 115
Removes the top element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
116 117 118 119 120

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
121
| T | Element removed.|
W
wusongqing 已提交
122

G
Gloria 已提交
123 124 125 126 127 128 129 130
**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 已提交
131 132
**Example**

W
wusongqing 已提交
133
```ts
W
wusongqing 已提交
134 135 136 137 138 139 140
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(4);
let result = stack.pop();
G
Gloria 已提交
141 142 143 144 145
try {
  stack.pop.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
146 147 148 149 150 151
```

### peek

peek(): T

W
wusongqing 已提交
152 153 154
Obtains the top element of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
155 156 157 158 159

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
160
| T | Element obtained.|
W
wusongqing 已提交
161

G
Gloria 已提交
162 163 164 165 166 167 168 169
**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 已提交
170 171
**Example**

W
wusongqing 已提交
172
```ts
W
wusongqing 已提交
173 174 175 176 177 178
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.peek();
G
Gloria 已提交
179 180 181 182 183
try {
  stack.peek.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
184 185 186 187 188 189
```

### locate

locate(element: T): number

W
wusongqing 已提交
190 191 192
Obtains the index of the first occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
193 194 195 196 197

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
198
| element | T | Yes| Target element.|
W
wusongqing 已提交
199 200 201 202 203

**Return value**

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

G
Gloria 已提交
206 207 208 209 210 211 212 213
**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 已提交
214 215
**Example**

W
wusongqing 已提交
216
```ts
W
wusongqing 已提交
217 218 219 220 221 222
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(2);
G
Gloria 已提交
223 224 225 226 227
try {
  stack.locate.bind({}, 2)(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
228 229 230
```

### forEach
W
wusongqing 已提交
231

W
wusongqing 已提交
232 233 234
forEach(callbackfn: (value: T, index?: number, stack?: Stack<T>) => void,
thisArg?: Object): void

W
wusongqing 已提交
235 236 237
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
238 239 240 241 242

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
243
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
244 245 246 247 248 249
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
250 251
| 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 已提交
252 253
| stack | Stack<T> | No| Instance that invokes the **forEach** method.|

G
Gloria 已提交
254 255 256 257 258 259 260 261
**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 已提交
262 263
**Example**

W
wusongqing 已提交
264
```ts
W
wusongqing 已提交
265 266 267 268 269 270
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value, index) => {
W
wusongqing 已提交
271
 console.log("value:" + value, index);
W
wusongqing 已提交
272
});
G
Gloria 已提交
273 274 275 276 277 278 279
try {
  stack.forEach.bind({}, (value, index) => {
    console.log("value:" + value, index);
  })(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
280 281 282
```

### isEmpty
W
wusongqing 已提交
283

W
wusongqing 已提交
284 285
isEmpty(): boolean

W
wusongqing 已提交
286 287 288
Checks whether this container is empty (contains no elements).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
289 290 291 292 293 294 295

**Return value**

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

G
Gloria 已提交
296 297 298 299 300 301 302 303
**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 已提交
304 305
**Example**

W
wusongqing 已提交
306
```ts
W
wusongqing 已提交
307 308 309 310 311 312
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
let result = stack.isEmpty();
G
Gloria 已提交
313 314 315 316 317
try {
  stack.isEmpty.bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
318 319 320 321 322 323 324 325
```

### [Symbol.iterator]

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

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

W
wusongqing 已提交
326 327
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
328 329 330 331 332 333
**Return value**

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

G
Gloria 已提交
334 335 336 337 338 339 340 341
**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 已提交
342
**Example**
W
wusongqing 已提交
343
```ts
W
wusongqing 已提交
344 345 346 347 348 349 350 351
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 已提交
352
  console.log("value:" + item); 
W
wusongqing 已提交
353 354 355 356 357 358
}

// Method 2:
let iter = stack[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
359
  console.log("value:" + temp);
W
wusongqing 已提交
360 361
  temp = iter.next().value;
}
G
Gloria 已提交
362 363 364 365 366
try {
  stack[Symbol.iterator].bind({})(); // bind() creates a new bound function that, when called, has its this keyword set to the provided value. It is used to test exception capture.
} catch(err) {
  console.log(`${err.code} - ${err.name} - ${err.message}`);
}
W
wusongqing 已提交
367
```