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