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