js-apis-stack.md 5.0 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 13 14

## Modules to Import

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




## Stack

### Attributes

W
wusongqing 已提交
26 27
**System capability**: SystemCapability.Utils.Lang

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


### constructor

constructor()

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

W
wusongqing 已提交
39 40
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
41 42
**Example**

W
wusongqing 已提交
43
```ts
W
wusongqing 已提交
44 45 46 47 48 49 50 51
let stack = new Stack();
```


### push

push(item: T): T

W
wusongqing 已提交
52 53 54
Adds an element at the top of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
55 56 57 58 59

**Parameters**

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

**Return value**

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

**Example**

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

### pop

pop(): T

W
wusongqing 已提交
84 85 86
Removes the top element from this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
87 88 89 90 91

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
92
| T | Element removed.|
W
wusongqing 已提交
93 94 95

**Example**

W
wusongqing 已提交
96
```ts
W
wusongqing 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
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 已提交
110 111 112
Obtains the top element of this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
113 114 115 116 117

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
118
| T | Element obtained.|
W
wusongqing 已提交
119 120 121

**Example**

W
wusongqing 已提交
122
```ts
W
wusongqing 已提交
123 124 125 126 127 128 129 130 131 132 133 134
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 已提交
135 136 137
Obtains the index of the first occurrence of the specified element in this container.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
138 139 140 141 142

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
143
| element | T | Yes| Target element.|
W
wusongqing 已提交
144 145 146 147 148

**Return value**

| Type| Description|
| -------- | -------- |
W
wusongqing 已提交
149
| number | Returns the position index if obtained; returns **-1** otherwise.|
W
wusongqing 已提交
150 151 152

**Example**

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

### forEach
W
wusongqing 已提交
163

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

W
wusongqing 已提交
167 168 169
Uses a callback to traverse the elements in this container and obtain their position indexes.

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
170 171 172 173 174

**Parameters**

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

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
W
wusongqing 已提交
182 183
| 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 已提交
184 185 186 187
| stack | Stack<T> | No| Instance that invokes the **forEach** method.|

**Example**

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

### isEmpty
W
wusongqing 已提交
200

W
wusongqing 已提交
201 202
isEmpty(): boolean

W
wusongqing 已提交
203 204 205
Checks whether this container is empty (contains no elements).

**System capability**: SystemCapability.Utils.Lang
W
wusongqing 已提交
206 207 208 209 210 211 212 213 214

**Return value**

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

**Example**

W
wusongqing 已提交
215
```ts
W
wusongqing 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229
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 已提交
230 231
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
232 233 234 235 236 237 238
**Return value**

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

**Example**
W
wusongqing 已提交
239
```ts
W
wusongqing 已提交
240 241 242 243 244 245 246 247
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 已提交
248
  console.log("value:" + item); 
W
wusongqing 已提交
249 250 251 252 253 254
}

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