1# @ohos.util.Stack (Linear Container Stack)
2
3**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.
4
5Unlike **[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.
6
7**Recommended use case**: Use **Stack** in LOFI scenarios.
8
9This topic uses the following to identify the use of generics:
10- T: Type
11
12> **NOTE**
13>
14> 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.
15
16
17## Modules to Import
18
19```ts
20import { Stack } from '@kit.ArkTS';
21```
22
23## Stack
24
25### Attributes
26
27**Atomic service API**: This API can be used in atomic services since API version 12.
28
29**System capability**: SystemCapability.Utils.Lang
30
31| Name| Type| Readable| Writable| Description|
32| -------- | -------- | -------- | -------- | -------- |
33| length | number | Yes| No| Number of elements in a stack (called container later).|
34
35
36### constructor
37
38constructor()
39
40A constructor used to create a **Stack** instance.
41
42**Atomic service API**: This API can be used in atomic services since API version 12.
43
44**System capability**: SystemCapability.Utils.Lang
45
46**Error codes**
47
48For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
49
50| ID| Error Message|
51| -------- | -------- |
52| 10200012 | The Stack's constructor cannot be directly invoked. |
53
54**Example**
55
56```ts
57let stack : Stack<number | string | Object> = new Stack();
58```
59
60
61### push
62
63push(item: T): T
64
65Adds an element at the top of this container.
66
67**Atomic service API**: This API can be used in atomic services since API version 12.
68
69**System capability**: SystemCapability.Utils.Lang
70
71**Parameters**
72
73| Name| Type| Mandatory| Description|
74| -------- | -------- | -------- | -------- |
75| item | T | Yes| Target element.|
76
77**Return value**
78
79| Type| Description|
80| -------- | -------- |
81| T | Element added.|
82
83**Error codes**
84
85For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
86
87| ID| Error Message|
88| -------- | -------- |
89| 10200011 | The push method cannot be bound. |
90
91**Example**
92
93```
94class C1 {
95  name: string = ""
96  age: string = ""
97}
98let stack : Stack<number | string | C1> = new Stack();
99let result = stack.push("a");
100let result1 = stack.push(1);
101let c : C1  = {name : "Dylan", age : "13"};
102let result2 = stack.push(c);
103```
104
105### pop
106
107pop(): T
108
109Removes the top element from this container.
110
111**Atomic service API**: This API can be used in atomic services since API version 12.
112
113**System capability**: SystemCapability.Utils.Lang
114
115**Return value**
116
117| Type| Description|
118| -------- | -------- |
119| T | Element removed. If the container is empty, **undefined** is returned.|
120
121**Error codes**
122
123For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
124
125| ID| Error Message|
126| -------- | -------- |
127| 10200011 | The pop method cannot be bound. |
128
129**Example**
130
131```ts
132let stack : Stack<number> = new Stack();
133stack.push(2);
134stack.push(4);
135stack.push(5);
136stack.push(2);
137stack.push(4);
138let result = stack.pop();
139```
140
141### peek
142
143peek(): T
144
145Obtains the top element of this container.
146
147**Atomic service API**: This API can be used in atomic services since API version 12.
148
149**System capability**: SystemCapability.Utils.Lang
150
151**Return value**
152
153| Type| Description|
154| -------- | -------- |
155| T | Element obtained.|
156
157**Error codes**
158
159For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
160
161| ID| Error Message|
162| -------- | -------- |
163| 10200011 | The peek method cannot be bound. |
164
165**Example**
166
167```ts
168let stack : Stack<number> = new Stack();
169stack.push(2);
170stack.push(4);
171stack.push(5);
172stack.push(2);
173let result = stack.peek();
174```
175
176### locate
177
178locate(element: T): number
179
180Obtains the index of the first occurrence of the specified element in this container.
181
182**Atomic service API**: This API can be used in atomic services since API version 12.
183
184**System capability**: SystemCapability.Utils.Lang
185
186**Parameters**
187
188| Name| Type| Mandatory| Description|
189| -------- | -------- | -------- | -------- |
190| element | T | Yes| Target element.|
191
192**Return value**
193
194| Type| Description|
195| -------- | -------- |
196| number | Index of the first occurrence of the specified element. If the element does not exist, **-1** is returned.|
197
198**Error codes**
199
200For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
201
202| ID| Error Message|
203| -------- | -------- |
204| 10200011 | The locate method cannot be bound. |
205
206**Example**
207
208```ts
209let stack : Stack<number> = new Stack();
210stack.push(2);
211stack.push(4);
212stack.push(5);
213stack.push(2);
214let result = stack.locate(2);
215```
216
217### forEach
218
219forEach(callbackFn: (value: T, index?: number, stack?: Stack&lt;T&gt;) => void,
220thisArg?: Object): void
221
222Uses a callback to traverse the elements in this container and obtain their position indexes.
223
224**Atomic service API**: This API can be used in atomic services since API version 12.
225
226**System capability**: SystemCapability.Utils.Lang
227
228**Parameters**
229
230| Name| Type| Mandatory| Description|
231| -------- | -------- | -------- | -------- |
232| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
233| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
234
235callbackFn
236
237| Name| Type| Mandatory| Description|
238| -------- | -------- | -------- | -------- |
239| value | T | Yes| Value of the element that is currently traversed.|
240| index | number | No| Position index of the element that is currently traversed. The default value is **0**.|
241| stack | Stack&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
242
243**Error codes**
244
245For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
246
247| ID| Error Message|
248| -------- | -------- |
249| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
250| 10200011 | The forEach method cannot be bound. |
251
252**Example**
253
254```ts
255let stack : Stack<number> = new Stack();
256stack.push(2);
257stack.push(4);
258stack.push(5);
259stack.push(4);
260stack.forEach((value : number, index ?: number) :void => {
261  console.log("value:" + value, "index:" + index);
262});
263```
264
265### isEmpty
266
267isEmpty(): boolean
268
269Checks whether this container is empty (contains no elements).
270
271**Atomic service API**: This API can be used in atomic services since API version 12.
272
273**System capability**: SystemCapability.Utils.Lang
274
275**Return value**
276
277| Type| Description|
278| -------- | -------- |
279| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
280
281**Error codes**
282
283For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
284
285| ID| Error Message|
286| -------- | -------- |
287| 10200011 | The isEmpty method cannot be bound. |
288
289**Example**
290
291```ts
292let stack : Stack<number> = new Stack();
293stack.push(2);
294stack.push(4);
295stack.push(5);
296stack.push(4);
297let result = stack.isEmpty();
298```
299
300### [Symbol.iterator]
301
302[Symbol.iterator]\(): IterableIterator&lt;T&gt;
303
304Obtains an iterator, each item of which is a JavaScript object.
305
306**Atomic service API**: This API can be used in atomic services since API version 12.
307
308**System capability**: SystemCapability.Utils.Lang
309
310**Return value**
311
312| Type| Description|
313| -------- | -------- |
314| IterableIterator&lt;T&gt; | Iterator obtained.|
315
316**Error codes**
317
318For details about the error codes, see [Utils Error Codes](errorcode-utils.md).
319
320| ID| Error Message|
321| -------- | -------- |
322| 10200011 | The Symbol.iterator method cannot be bound. |
323
324**Example**
325```ts
326let stack : Stack<number> = new Stack();
327stack.push(2);
328stack.push(4);
329stack.push(5);
330stack.push(4);
331
332// Method 1:
333while(!stack.isEmpty()) {
334  // Service logic
335  let item = stack.pop()
336  console.log("value:" + item);
337}
338
339// Method 2:
340let iter = stack[Symbol.iterator]();
341let temp: IteratorResult<number> = iter.next().value;
342while(temp != undefined) {
343  console.log("value:" + temp);
344  temp = iter.next().value;
345}
346```
347