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 : Stack<number | string | Object> = 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``` 88class C1 { 89 name: string = "" 90 age: string = "" 91} 92let stack : Stack<number | string | C1> = new Stack(); 93let result = stack.push("a"); 94let result1 = stack.push(1); 95let b = [1, 2, 3]; 96let result2 = stack.push(b); 97let c : C1 = {name : "Dylon", age : "13"}; 98let result3 = stack.push(c); 99``` 100 101### pop 102 103pop(): T 104 105Removes the top element from this container. 106 107**System capability**: SystemCapability.Utils.Lang 108 109**Return value** 110 111| Type| Description| 112| -------- | -------- | 113| T | Element removed.| 114 115**Error codes** 116 117For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 118 119| ID| Error Message| 120| -------- | -------- | 121| 10200011 | The pop method cannot be bound. | 122 123**Example** 124 125```ts 126let stack : Stack<number> = new Stack(); 127stack.push(2); 128stack.push(4); 129stack.push(5); 130stack.push(2); 131stack.push(4); 132let result = stack.pop(); 133``` 134 135### peek 136 137peek(): T 138 139Obtains the top element of this container. 140 141**System capability**: SystemCapability.Utils.Lang 142 143**Return value** 144 145| Type| Description| 146| -------- | -------- | 147| T | Element obtained.| 148 149**Error codes** 150 151For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 152 153| ID| Error Message| 154| -------- | -------- | 155| 10200011 | The peek method cannot be bound. | 156 157**Example** 158 159```ts 160let stack : Stack<number> = new Stack(); 161stack.push(2); 162stack.push(4); 163stack.push(5); 164stack.push(2); 165let result = stack.peek(); 166``` 167 168### locate 169 170locate(element: T): number 171 172Obtains the index of the first occurrence of the specified element in this container. 173 174**System capability**: SystemCapability.Utils.Lang 175 176**Parameters** 177 178| Name| Type| Mandatory| Description| 179| -------- | -------- | -------- | -------- | 180| element | T | Yes| Target element.| 181 182**Return value** 183 184| Type| Description| 185| -------- | -------- | 186| number | Returns the position index if obtained; returns **-1** otherwise.| 187 188**Error codes** 189 190For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 191 192| ID| Error Message| 193| -------- | -------- | 194| 10200011 | The locate method cannot be bound. | 195 196**Example** 197 198```ts 199let stack : Stack<number> = new Stack(); 200stack.push(2); 201stack.push(4); 202stack.push(5); 203stack.push(2); 204let result = stack.locate(2); 205``` 206 207### forEach 208 209forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, 210thisArg?: Object): void 211 212Uses a callback to traverse the elements in this container and obtain their position indexes. 213 214**System capability**: SystemCapability.Utils.Lang 215 216**Parameters** 217 218| Name| Type| Mandatory| Description| 219| -------- | -------- | -------- | -------- | 220| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.| 221| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.| 222 223callbackFn 224 225| Name| Type| Mandatory| Description| 226| -------- | -------- | -------- | -------- | 227| value | T | Yes| Value of the element that is currently traversed.| 228| index | number | No| Position index of the element that is currently traversed. The default value is **0**.| 229| stack | Stack<T> | No| Instance that calls the **forEach** API. The default value is this instance.| 230 231**Error codes** 232 233For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 234 235| ID| Error Message| 236| -------- | -------- | 237| 10200011 | The forEach method cannot be bound. | 238 239**Example** 240 241```ts 242let stack : Stack<number> = new Stack(); 243stack.push(2); 244stack.push(4); 245stack.push(5); 246stack.push(4); 247stack.forEach((value : number, index ?: number) :void => { 248 console.log("value:" + value, "index:" + index); 249}); 250``` 251 252### isEmpty 253 254isEmpty(): boolean 255 256Checks whether this container is empty (contains no elements). 257 258**System capability**: SystemCapability.Utils.Lang 259 260**Return value** 261 262| Type| Description| 263| -------- | -------- | 264| boolean | Returns **true** if the container is empty; returns **false** otherwise.| 265 266**Error codes** 267 268For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 269 270| ID| Error Message| 271| -------- | -------- | 272| 10200011 | The isEmpty method cannot be bound. | 273 274**Example** 275 276```ts 277let stack : Stack<number> = new Stack(); 278stack.push(2); 279stack.push(4); 280stack.push(5); 281stack.push(4); 282let result = stack.isEmpty(); 283``` 284 285### [Symbol.iterator] 286 287[Symbol.iterator]\(): IterableIterator<T> 288 289Obtains an iterator, each item of which is a JavaScript object. 290 291**System capability**: SystemCapability.Utils.Lang 292 293**Return value** 294 295| Type| Description| 296| -------- | -------- | 297| IterableIterator<T> | Iterator obtained.| 298 299**Error codes** 300 301For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 302 303| ID| Error Message| 304| -------- | -------- | 305| 10200011 | The Symbol.iterator method cannot be bound. | 306 307**Example** 308```ts 309let stack : Stack<number> = new Stack(); 310stack.push(2); 311stack.push(4); 312stack.push(5); 313stack.push(4); 314 315// Method 1: 316while(!stack.isEmpty()) { 317 // Service logic 318 let item = stack.pop() 319 console.log("value:" + item); 320} 321 322// Method 2: 323let iter = stack[Symbol.iterator](); 324let temp: IteratorResult<number> = iter.next().value; 325while(temp != undefined) { 326 console.log("value:" + temp); 327 temp = iter.next().value; 328} 329``` 330