1# @ohos.util.Queue (线性容器Queue)
2
3Queue的特点是先进先出,在尾部增加元素,在头部删除元素。根据循环队列的数据结构实现。
4
5Queue和[Deque](js-apis-deque.md)相比,Queue只能在一端删除一端增加,Deque可以两端增删。
6
7**推荐使用场景:** 一般符合先进先出的场景可以使用Queue。
8
9文档中存在泛型的使用,涉及以下泛型标记符:<br>
10- T:Type,类
11
12> **说明:**
13>
14> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15
16
17## 导入模块
18
19```ts
20import { Queue } from '@kit.ArkTS';
21```
22
23
24## Queue
25
26### 属性
27
28**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
29
30**系统能力:** SystemCapability.Utils.Lang
31
32| 名称 | 类型 | 可读 | 可写 | 说明 |
33| -------- | -------- | -------- | -------- | -------- |
34| length | number | 是 | 否 | Queue的元素个数。 |
35
36
37### constructor
38
39constructor()
40
41Queue的构造函数。
42
43**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
44
45**系统能力:** SystemCapability.Utils.Lang
46
47**错误码:**
48
49以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
50
51| 错误码ID | 错误信息 |
52| -------- | -------- |
53| 10200012 | The Queue's constructor cannot be directly invoked. |
54
55**示例:**
56
57```ts
58let queue : Queue<number | string | Object> = new Queue();
59```
60
61
62### add
63
64add(element: T): boolean
65
66在队列尾部插入元素。
67
68**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
69
70**系统能力:** SystemCapability.Utils.Lang
71
72**参数:**
73
74| 参数名 | 类型 | 必填 | 说明 |
75| -------- | -------- | -------- | -------- |
76| element | T | 是 | 添加进去的元素。 |
77
78**返回值:**
79
80| 类型 | 说明 |
81| -------- | -------- |
82| boolean | 插入成功返回true,否则返回false。 |
83
84**错误码:**
85
86以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
87
88| 错误码ID | 错误信息 |
89| -------- | -------- |
90| 10200011 | The add method cannot be bound. |
91
92**示例:**
93
94```ts
95class C1 {
96  name: string = ""
97  age: string = ""
98}
99let queue : Queue<number | string | C1 | number[]> = new Queue();
100let result = queue.add("a");
101let result1 = queue.add(1);
102let b = [1, 2, 3];
103let result2 = queue.add(b);
104let c : C1 = {name : "Dylan", age : "13"};
105let result3 = queue.add(c);
106```
107
108### pop
109
110pop(): T
111
112删除头元素并返回该删除元素。
113
114**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
115
116**系统能力:** SystemCapability.Utils.Lang
117
118**返回值:**
119
120| 类型 | 说明 |
121| -------- | -------- |
122| T | 返回删除的元素。 |
123
124**错误码:**
125
126以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
127
128| 错误码ID | 错误信息 |
129| -------- | -------- |
130| 10200011 | The pop method cannot be bound. |
131
132**示例:**
133
134```ts
135let queue : Queue<number> = new Queue();
136queue.add(2);
137queue.add(4);
138queue.add(5);
139queue.add(2);
140queue.add(4);
141let result = queue.pop();
142```
143
144### getFirst
145
146getFirst(): T
147
148获取队列的头元素。
149
150**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
151
152**系统能力:** SystemCapability.Utils.Lang
153
154**返回值:**
155
156| 类型 | 说明 |
157| -------- | -------- |
158| T | 返回获取的元素。 |
159
160**错误码:**
161
162以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
163
164| 错误码ID | 错误信息 |
165| -------- | -------- |
166| 10200011 | The getFirst method cannot be bound. |
167
168**示例:**
169
170```ts
171let queue : Queue<number> = new Queue();
172queue.add(2);
173queue.add(4);
174queue.add(5);
175queue.add(2);
176let result = queue.getFirst();
177```
178
179### forEach
180
181forEach(callbackFn: (value: T, index?: number, Queue?: Queue&lt;T&gt;) => void,
182thisArg?: Object): void
183
184通过回调函数来遍历Queue实例对象上的元素以及元素对应的下标。
185
186**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
187
188**系统能力:** SystemCapability.Utils.Lang
189
190**参数:**
191
192| 参数名 | 类型 | 必填 | 说明 |
193| -------- | -------- | -------- | -------- |
194| callbackFn | function | 是 | 回调函数。 |
195| thisArg | Object | 否 | callbackfn被调用时用作this值,默认值为当前实例对象。 |
196
197callbackfn的参数说明:
198
199| 参数名 | 类型 | 必填 | 说明 |
200| -------- | -------- | -------- | -------- |
201| value | T | 是 | 当前遍历到的元素。 |
202| index | number | 否 | 当前遍历到的下标值,默认值为0。 |
203| Queue | Queue&lt;T&gt; | 否 | 当前调用forEach方法的实例对象,默认值为当前实例对象。 |
204
205**错误码:**
206
207以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[语言基础类库错误码](errorcode-utils.md)。
208
209| 错误码ID | 错误信息 |
210| -------- | -------- |
211| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
212| 10200011 | The forEach method cannot be bound. |
213
214**示例:**
215
216```ts
217let queue : Queue<number> = new Queue();
218queue.add(2);
219queue.add(4);
220queue.add(5);
221queue.add(4);
222queue.forEach((value : number, index ?: number) : void => {
223  console.log("value:" + value, "index:" + index);
224});
225```
226
227### [Symbol.iterator]
228
229[Symbol.iterator]\(): IterableIterator&lt;T&gt;
230
231返回一个迭代器,迭代器的每一项都是一个 JavaScript 对象,并返回该对象。
232
233**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
234
235**系统能力:** SystemCapability.Utils.Lang
236
237**返回值:**
238
239| 类型 | 说明 |
240| -------- | -------- |
241| IterableIterator&lt;T&gt; | 返回一个迭代器。 |
242
243**错误码:**
244
245以下错误码的详细介绍请参见[语言基础类库错误码](errorcode-utils.md)。
246
247| 错误码ID | 错误信息 |
248| -------- | -------- |
249| 10200011 | The Symbol.iterator method cannot be bound. |
250
251**示例:**
252```ts
253let queue : Queue<number> = new Queue();
254queue.add(2);
255queue.add(4);
256queue.add(5);
257queue.add(4);
258
259// 使用方法一:
260while(queue.length) {
261  let item = queue.pop()
262  console.log("value:" + item);
263}
264
265// 使用方法二:
266let iter = queue[Symbol.iterator]();
267let temp: IteratorResult<number> = iter.next().value;
268while(temp != undefined) {
269  console.log("value:" + temp);
270  temp = iter.next().value;
271}
272```