js-apis-stack.md 5.1 KB
Newer Older
W
wusongqing 已提交
1 2
# Linear Container Stack

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5 6
> 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.

W
wusongqing 已提交
7 8 9 10 11
**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](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.

**Recommended use case**: Use **Stack** in LOFI scenarios.
W
wusongqing 已提交
12

G
Gloria 已提交
13 14 15
This topic uses the following to identify the use of generics:
- T: Type

W
wusongqing 已提交
16 17
## Modules to Import

W
wusongqing 已提交
18
```ts
W
wusongqing 已提交
19
import Stack from '@ohos.util.Stack';  
W
wusongqing 已提交
20 21 22 23 24 25 26 27 28
```




## Stack

### Attributes

W
wusongqing 已提交
29 30
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
31 32
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
33
| length | number | Yes| No| Number of elements in a stack (called container later).|
W
wusongqing 已提交
34 35 36 37 38 39 40 41


### constructor

constructor()

A constructor used to create a **Stack** instance.

W
wusongqing 已提交
42 43
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
44 45
**Example**

W
wusongqing 已提交
46
```ts
W
wusongqing 已提交
47 48 49 50 51 52 53 54
let stack = new Stack();
```


### push

push(item: T): T

W
wusongqing 已提交
55 56 57
Adds an element at the top of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
58 59 60 61 62

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
63
| item | T | Yes| Target element.|
W
wusongqing 已提交
64 65 66 67 68 69 70 71 72

**Return value**

| Type| Description|
| -------- | -------- |
| T | Element added.|

**Example**

W
wusongqing 已提交
73
```ts
W
wusongqing 已提交
74 75 76 77 78
let stack = new Stack();
let result = stack.push("a");
let result1 = stack.push(1);
let b = [1, 2, 3];
stack.push(b);
G
Gloria 已提交
79
let c = {name : "Dylon", age : "13"};
W
wusongqing 已提交
80 81 82 83 84 85 86
let result3 = stack.push(c);
```

### pop

pop(): T

W
wusongqing 已提交
87 88 89
Removes the top element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
90 91 92 93 94

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
95
| T | Element removed.|
W
wusongqing 已提交
96 97 98

**Example**

W
wusongqing 已提交
99
```ts
W
wusongqing 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(4);
let result = stack.pop();
```

### peek

peek(): T

W
wusongqing 已提交
113 114 115
Obtains the top element of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
116 117 118 119 120

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
121
| T | Element obtained.|
W
wusongqing 已提交
122 123 124

**Example**

W
wusongqing 已提交
125
```ts
W
wusongqing 已提交
126 127 128 129 130 131 132 133 134 135 136 137
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.peek();
```

### locate

locate(element: T): number

W
wusongqing 已提交
138 139 140
Obtains the index of the first occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
141 142 143 144 145

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
146
| element | T | Yes| Target element.|
W
wusongqing 已提交
147 148 149 150 151

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
152
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
153 154 155

**Example**

W
wusongqing 已提交
156
```ts
W
wusongqing 已提交
157 158 159 160 161 162 163 164 165
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(2);
```

### forEach
W
wusongqing 已提交
166

W
wusongqing 已提交
167 168 169
forEach(callbackfn: (value: T, index?: number, stack?: Stack<T>) => void,
thisArg?: Object): void

W
wusongqing 已提交
170 171 172
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
173 174 175 176 177

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
178
| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
W
wusongqing 已提交
179 180 181 182 183 184
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
185 186
| value | T | Yes| Value of the element that is currently traversed.|
| index | number | No| Position index of the element that is currently traversed.|
W
wusongqing 已提交
187 188 189 190
| stack | Stack<T> | No| Instance that invokes the **forEach** method.|

**Example**

W
wusongqing 已提交
191
```ts
W
wusongqing 已提交
192 193 194 195 196 197
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value, index) => {
W
wusongqing 已提交
198
 console.log("value:" + value, index);
W
wusongqing 已提交
199 200 201 202
});
```

### isEmpty
W
wusongqing 已提交
203

W
wusongqing 已提交
204 205
isEmpty(): boolean

W
wusongqing 已提交
206 207 208
Checks whether this container is empty (contains no elements).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
209 210 211 212 213 214 215 216 217

**Return value**

| Type| Description|
| -------- | -------- |
| boolean | Returns **true** if the container is empty; returns **false** otherwise.|

**Example**

W
wusongqing 已提交
218
```ts
W
wusongqing 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
let result = stack.isEmpty();
```

### [Symbol.iterator]

[Symbol.iterator]\(): IterableIterator<T>

Obtains an iterator, each item of which is a JavaScript object.

W
wusongqing 已提交
233 234
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
235 236 237 238 239 240 241
**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator<T> | Iterator obtained.|

**Example**
W
wusongqing 已提交
242
```ts
W
wusongqing 已提交
243 244 245 246 247 248 249 250
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);

// Method 1:
for (let item of stack) { 
W
wusongqing 已提交
251
  console.log("value:" + item); 
W
wusongqing 已提交
252 253 254 255 256 257
}

// Method 2:
let iter = stack[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
W
wusongqing 已提交
258
  console.log("value:" + temp);
W
wusongqing 已提交
259 260 261
  temp = iter.next().value;
}
```