@ohos.util.Stack (Linear Container Stack)
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, 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.
This topic uses the following to identify the use of generics:
- T: Type
NOTE
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.
Container classes, implemented in static languages, have restrictions on storage locations and properties, and do not support custom properties or methods.
Modules to Import
import { Stack } from '@kit.ArkTS';
Stack
Properties
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| length | number | Yes | No | Number of elements in a Stack. |
constructor
constructor()
A constructor used to create a Stack instance.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200012 | The Stack's constructor cannot be directly invoked. |
Example
let stack = new Stack<number | string | Object>();
push
push(item: T): T
Adds an element at the top of this Stack.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| item | T | Yes | Target element. |
Return value
| Type | Description |
|---|---|
| T | Element added. |
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200011 | The push method cannot be bound. |
Example
class C1 {
name: string = ""
age: string = ""
}
let stack = new Stack<number | string | C1>();
let result = stack.push("a");
let result1 = stack.push(1);
let c : C1 = {name : "Dylan", age : "13"};
let result2 = stack.push(c);
console.info("length:", stack.length); // length: 3
pop
pop(): T
Removes the top element from this Stack.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
| Type | Description |
|---|---|
| T | Element removed. If the Stack is empty, undefined is returned. |
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200011 | The pop method cannot be bound. |
Example
let stack = new Stack<number>();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(4);
let result = stack.pop();
console.info("result = " + result); // result = 4
peek
peek(): T
Obtains the top element of this Stack. If the Stack is empty, undefined is returned.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
| Type | Description |
|---|---|
| T | Element removed. If the Stack is empty, undefined is returned. |
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200011 | The peek method cannot be bound. |
Example
let stack = new Stack<number>();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.peek();
console.info("result:", result); // result: 2
locate
locate(element: T): number
Obtains the index of the first occurrence of the specified element in this Stack.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| element | T | Yes | Target element. |
Return value
| Type | Description |
|---|---|
| number | Index of the element. If no match is found, -1 is returned. |
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200011 | The locate method cannot be bound. |
Example
let stack = new Stack<number>();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(5);
console.info("result:", result); // result: 2
forEach
forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object): void
Uses a callback to traverse each element in the Stack instance.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| callbackFn | function | Yes | Callback invoked to traverse the elements in the Stack. |
| thisArg | Object | No | Value of this to use when callbackFn is invoked. The default value is this instance. |
callbackFn parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | T | Yes | Value of the element that is currently traversed. |
| index | number | No | Position index of the element that is currently traversed. The default value is 0. |
| stack | Stack<T> | No | Instance that calls the forEach API. The default value is this instance. |
Error codes
For details about the error codes, see Universal Error Codes and Utils Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 10200011 | The forEach method cannot be bound. |
Example
let stack = new Stack<number>();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value : number, index: number) :void => {
console.info("value:" + value, "index:" + index);
});
// value:2 index:0
// value:4 index:1
// value:5 index:2
// value:4 index:3
isEmpty
isEmpty(): boolean
Checks whether this Stack is empty (contains no elements).
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
| Type | Description |
|---|---|
| boolean | Returns true if the Stack is empty; returns false otherwise. |
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200011 | The isEmpty method cannot be bound. |
Example
let stack = new Stack<number>();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
let result = stack.isEmpty();
console.info("result:", result); // result: false
[Symbol.iterator]
[Symbol.iterator](): IterableIterator<T>
Returns an iterator, each item of which is a JavaScript object.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.Utils.Lang
Return value
| Type | Description |
|---|---|
| IterableIterator<T> | Iterator obtained. |
Error codes
For details about the error codes, see Utils Error Codes.
| ID | Error Message |
|---|---|
| 10200011 | The Symbol.iterator method cannot be bound. |
Example
let stack = new Stack<number>();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
// Method 1:
for (let value of stack) {
console.info("value:", value);
}
// value: 2
// value: 4
// value: 5
// value: 4
// Method 2:
let iter = stack[Symbol.iterator]();
let temp: IteratorResult<number> = iter.next().value;
while(temp != undefined) {
console.info("value: " + temp);
temp = iter.next().value;
}
// value: 2
// value: 4
// value: 5
// value: 4