• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.util.Stack (线性容器Stack)
2
3> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
4> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
5
6Stack基于数组的数据结构实现,特点是先进后出,只能在一端进行数据的插入和删除。
7
8Stack和[Queue](js-apis-queue.md)相比,Queue基于循环队列实现,只能在一端删除,另一端插入,而Stack都在一端操作。
9
10**推荐使用场景:** 一般符合先进后出的场景可以使用Stack。
11
12文档中存在泛型的使用,涉及以下泛型标记符:<br>
13- T: Type, 类
14
15## 导入模块
16
17```ts
18import Stack from '@ohos.util.Stack';
19```
20
21## Stack
22
23### 属性
24
25**系统能力:** SystemCapability.Utils.Lang
26
27| 名称 | 类型 | 可读 | 可写 | 说明 |
28| -------- | -------- | -------- | -------- | -------- |
29| length | number | 是 | 否 | Stack的元素个数。 |
30
31
32### constructor
33
34constructor()
35
36Stack的构造函数。
37
38**系统能力:** SystemCapability.Utils.Lang
39
40**错误码:**
41
42以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
43
44| 错误码ID | 错误信息 |
45| -------- | -------- |
46| 10200012 | The Stack's constructor cannot be directly invoked. |
47
48**示例:**
49
50```ts
51let stack = new Stack();
52```
53
54
55### push
56
57push(item: T): T
58
59在栈顶插入元素,并返回该元素。
60
61**系统能力:** SystemCapability.Utils.Lang
62
63**参数:**
64
65| 参数名 | 类型 | 必填 | 说明 |
66| -------- | -------- | -------- | -------- |
67| item | T | 是 | 添加进去的元素。 |
68
69**返回值:**
70
71| 类型 | 说明 |
72| -------- | -------- |
73| T | 返回被添加进去的元素。 |
74
75**错误码:**
76
77以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
78
79| 错误码ID | 错误信息 |
80| -------- | -------- |
81| 10200011 | The push method cannot be bound. |
82
83**示例:**
84
85```ts
86let stack = new Stack();
87let result = stack.push("a");
88let result1 = stack.push(1);
89let b = [1, 2, 3];
90let result2 = stack.push(b);
91let c = {name : "Dylon", age : "13"};
92let result3 = stack.push(c);
93```
94
95### pop
96
97pop(): T
98
99删除栈顶元素并返回该删除元素。
100
101**系统能力:** SystemCapability.Utils.Lang
102
103**返回值:**
104
105| 类型 | 说明 |
106| -------- | -------- |
107| T | 返回删除的元素。 |
108
109**错误码:**
110
111以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
112
113| 错误码ID | 错误信息 |
114| -------- | -------- |
115| 10200011 | The pop method cannot be bound. |
116
117**示例:**
118
119```ts
120let stack = new Stack();
121stack.push(2);
122stack.push(4);
123stack.push(5);
124stack.push(2);
125stack.push(4);
126let result = stack.pop();
127```
128
129### peek
130
131peek(): T
132
133获取并返回栈顶元素。
134
135**系统能力:** SystemCapability.Utils.Lang
136
137**返回值:**
138
139| 类型 | 说明 |
140| -------- | -------- |
141| T | 返回栈顶元素。 |
142
143**错误码:**
144
145以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
146
147| 错误码ID | 错误信息 |
148| -------- | -------- |
149| 10200011 | The peek method cannot be bound. |
150
151**示例:**
152
153```ts
154let stack = new Stack();
155stack.push(2);
156stack.push(4);
157stack.push(5);
158stack.push(2);
159let result = stack.peek();
160```
161
162### locate
163
164locate(element: T): number
165
166返回指定元素第一次出现时的下标值,查找失败返回-1。
167
168**系统能力:** SystemCapability.Utils.Lang
169
170**参数:**
171
172| 参数名 | 类型 | 必填 | 说明 |
173| -------- | -------- | -------- | -------- |
174| element | T | 是 | 指定元素。 |
175
176**返回值:**
177
178| 类型 | 说明 |
179| -------- | -------- |
180| number | 找到就返回下标值,查找失败返回-1。 |
181
182**错误码:**
183
184以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
185
186| 错误码ID | 错误信息 |
187| -------- | -------- |
188| 10200011 | The locate method cannot be bound. |
189
190**示例:**
191
192```ts
193let stack = new Stack();
194stack.push(2);
195stack.push(4);
196stack.push(5);
197stack.push(2);
198let result = stack.locate(2);
199```
200
201### forEach
202
203forEach(callbackFn: (value: T, index?: number, stack?: Stack&lt;T&gt;) => void,
204thisArg?: Object): void
205
206通过回调函数来遍历Stack实例对象上的元素以及元素对应的下标。
207
208**系统能力:** SystemCapability.Utils.Lang
209
210**参数:**
211
212| 参数名 | 类型 | 必填 | 说明 |
213| -------- | -------- | -------- | -------- |
214| callbackFn | function | 是 | 回调函数。 |
215| thisArg | Object | 否 | callbackfn被调用时用作this值。 |
216
217callbackfn的参数说明:
218
219| 参数名 | 类型 | 必填 | 说明 |
220| -------- | -------- | -------- | -------- |
221| value | T | 是 | 当前遍历到的元素。 |
222| index | number | 否 | 当前遍历到的下标值。 |
223| stack | Stack&lt;T&gt; | 否 | 当前调用forEach方法的实例对象。 |
224
225**错误码:**
226
227以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
228
229| 错误码ID | 错误信息 |
230| -------- | -------- |
231| 10200011 | The forEach method cannot be bound. |
232
233**示例:**
234
235```ts
236let stack = new Stack();
237stack.push(2);
238stack.push(4);
239stack.push(5);
240stack.push(4);
241stack.forEach((value, index) => {
242    console.log("value:" + value, "index:" + index);
243});
244```
245
246### isEmpty
247
248isEmpty(): boolean
249
250判断该栈是否为空。
251
252**系统能力:** SystemCapability.Utils.Lang
253
254**返回值:**
255
256| 类型 | 说明 |
257| -------- | -------- |
258| boolean | 为空返回true,不为空返回false。 |
259
260**错误码:**
261
262以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
263
264| 错误码ID | 错误信息 |
265| -------- | -------- |
266| 10200011 | The isEmpty method cannot be bound. |
267
268**示例:**
269
270```ts
271let stack = new Stack();
272stack.push(2);
273stack.push(4);
274stack.push(5);
275stack.push(4);
276let result = stack.isEmpty();
277```
278
279### [Symbol.iterator]
280
281[Symbol.iterator]\(): IterableIterator&lt;T&gt;
282
283返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
284
285**系统能力:** SystemCapability.Utils.Lang
286
287**返回值:**
288
289| 类型 | 说明 |
290| -------- | -------- |
291| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
292
293**错误码:**
294
295以下错误码的详细介绍请参见[语言基础类库错误码](../errorcodes/errorcode-utils.md)。
296
297| 错误码ID | 错误信息 |
298| -------- | -------- |
299| 10200011 | The Symbol.iterator method cannot be bound. |
300
301**示例:**
302```ts
303let stack = new Stack();
304stack.push(2);
305stack.push(4);
306stack.push(5);
307stack.push(4);
308
309// 使用方法一:
310for (let item of stack) {
311  console.log("value:" + item);
312}
313
314// 使用方法二:
315let iter = stack[Symbol.iterator]();
316let temp = iter.next().value;
317while(temp != undefined) {
318  console.log("value:" + temp);
319  temp = iter.next().value;
320}
321```