You need to sign in or sign up before continuing.
js-apis-stack.md 4.2 KB
Newer Older
W
wusongqing 已提交
1 2
# Linear Container Stack

W
wusongqing 已提交
3
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br/>
W
wusongqing 已提交
4 5 6 7 8
> 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.


## Modules to Import

W
wusongqing 已提交
9
```ts
W
wusongqing 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import Stack from '@ohos.util.Stack'  
```

## System Capabilities

SystemCapability.Utils.Lang


## Stack


### Attributes

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| length | number | Yes| No| Number of entries in a stack (called container later).|


### constructor

constructor()

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

**Example**

W
wusongqing 已提交
36
```ts
W
wusongqing 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
let stack = new Stack();
```


### push

push(item: T): T

Adds an entry at the top of this container.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| item | T | Yes| Element to add.|

**Return value**

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

**Example**

W
wusongqing 已提交
61
```ts
W
wusongqing 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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

Removes the top entry from this container.

**Return value**

| Type| Description|
| -------- | -------- |
| T | Entry removed.|

**Example**

W
wusongqing 已提交
85
```ts
W
wusongqing 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
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

Obtains the top entry of this container.

**Return value**

| Type| Description|
| -------- | -------- |
| T | Entry obtained.|

**Example**

W
wusongqing 已提交
109
```ts
W
wusongqing 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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

Obtains the index of the first occurrence of the specified entry in this container.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| element | T | Yes| Entry to query.|

**Return value**

| Type| Description|
| -------- | -------- |
| number | Returns the position index if obtained; returns **-1** if the specified entry is not found.|

**Example**

W
wusongqing 已提交
138
```ts
W
wusongqing 已提交
139 140 141 142 143 144 145 146 147
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(2);
```

### forEach
W
wusongqing 已提交
148

W
wusongqing 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
forEach(callbackfn: (value: T, index?: number, stack?: Stack&lt;T&gt;) => void,
thisArg?: Object): void

Uses a callback to traverse the entries in this container and obtain their position indexes.

**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| callbackfn | function | Yes| Callback invoked to traverse the entries in the container.|
| thisArg | Object | No| Value to use when the callback is invoked.|

callbackfn

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| value | T | Yes| Value of the entry that is currently traversed.|
| index | number | No| Position index of the entry that is currently traversed.|
| stack | Stack&lt;T&gt; | No| Instance that invokes the **forEach** method.|

**Example**

W
wusongqing 已提交
171
```ts
W
wusongqing 已提交
172 173 174 175 176 177 178 179 180 181 182
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value, index) => {
 console.log(value, index);
});
```

### isEmpty
W
wusongqing 已提交
183

W
wusongqing 已提交
184 185 186 187 188 189 190 191 192 193 194 195
isEmpty(): boolean

Checks whether this container is empty (contains no entries).

**Return value**

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

**Example**

W
wusongqing 已提交
196
```ts
W
wusongqing 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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&lt;T&gt;


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

**Return value**

| Type| Description|
| -------- | -------- |
| IterableIterator&lt;T&gt; | Iterator obtained.|

**Example**
W
wusongqing 已提交
219
```ts
W
wusongqing 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
let stack = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);

// Method 1:
for (let item of stack) { 
  console.log(item); 
}

// Method 2:
let iter = stack[Symbol.iterator]();
let temp = iter.next().value;
while(temp != undefined) {
  console.log(temp);
  temp = iter.next().value;
}
```