1e41f4b71Sopenharmony_ci# @ohos.util.List (Linear Container List)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci**List** is implemented based on the singly linked list. Each node has a reference pointing to the next element. When querying an element, the system traverses the list from the beginning. **List** offers efficient insertion and removal operations but supports low query efficiency. **List** allows null elements.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciUnlike [LinkedList](js-apis-linkedlist.md), which is a doubly linked list, **List** is a singly linked list that does not support insertion or removal at both ends.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **NOTE**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> Although using \[index\] in **List** can obtain an element with the given index, this operation will result in undefined results. Due to this reason, **get()** method is recommended.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci**Recommended use case**: Use **List** for frequent insertion and removal operations.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciThis topic uses the following to identify the use of generics:
14e41f4b71Sopenharmony_ci- T: Type
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci> **NOTE**
17e41f4b71Sopenharmony_ci>
18e41f4b71Sopenharmony_ci> 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.
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## Modules to Import
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci```ts
24e41f4b71Sopenharmony_ciimport { List } from '@kit.ArkTS';
25e41f4b71Sopenharmony_ci```
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci## List
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci### Attributes
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci| Name| Type| Readable| Writable| Description|
37e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- |
38e41f4b71Sopenharmony_ci| length | number | Yes| No| Number of elements in a list (called container later).|
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci### constructor
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ciconstructor()
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ciA constructor used to create a **List** instance.
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci**Error codes**
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci| ID| Error Message|
56e41f4b71Sopenharmony_ci| -------- | -------- |
57e41f4b71Sopenharmony_ci| 10200012 | The List's constructor cannot be directly invoked. |
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci**Example**
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci```ts
62e41f4b71Sopenharmony_cilet list: List<string | number | boolean | object> = new List();
63e41f4b71Sopenharmony_ci```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci### add
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ciadd(element: T): boolean
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ciAdds an element at the end of this container.
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**Parameters**
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
79e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
80e41f4b71Sopenharmony_ci| element | T | Yes| Target element.|
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci**Return value**
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci| Value Type | Description|
85e41f4b71Sopenharmony_ci| -------- | -------- |
86e41f4b71Sopenharmony_ci| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Error codes**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci| ID| Error Message|
93e41f4b71Sopenharmony_ci| -------- | -------- |
94e41f4b71Sopenharmony_ci| 10200011 | The add method cannot be bound. |
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci**Example**
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci```ts
99e41f4b71Sopenharmony_cilet list: List<string | number | boolean | object> = new List();
100e41f4b71Sopenharmony_cilet result1 = list.add("a");
101e41f4b71Sopenharmony_cilet result2 = list.add(1);
102e41f4b71Sopenharmony_cilet b = [1, 2, 3];
103e41f4b71Sopenharmony_cilet result3 = list.add(b);
104e41f4b71Sopenharmony_ciclass C {
105e41f4b71Sopenharmony_ci  name: string = ''
106e41f4b71Sopenharmony_ci  age: string = ''
107e41f4b71Sopenharmony_ci}
108e41f4b71Sopenharmony_cilet c: C = {name : "Dylan", age : "13"};
109e41f4b71Sopenharmony_cilet result4 = list.add(c);
110e41f4b71Sopenharmony_cilet result5 = list.add(false);
111e41f4b71Sopenharmony_ci```
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci### insert
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ciinsert(element: T, index: number): void
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ciInserts an element at the specified position in this container.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci**Parameters**
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
126e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
127e41f4b71Sopenharmony_ci| element | T | Yes| Target element.|
128e41f4b71Sopenharmony_ci| index | number | Yes| Index of the position where the element is to be inserted. The value must be less than or equal to int32_max, that is, 2147483647.|
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**Error codes**
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci| ID| Error Message|
135e41f4b71Sopenharmony_ci| -------- | -------- |
136e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
137e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. |
138e41f4b71Sopenharmony_ci| 10200011 | The insert method cannot be bound. |
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**Example**
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci```ts
143e41f4b71Sopenharmony_cilet list: List<string | number | boolean> = new List();
144e41f4b71Sopenharmony_cilist.insert("A", 0);
145e41f4b71Sopenharmony_cilist.insert(0, 1);
146e41f4b71Sopenharmony_cilist.insert(true, 2);
147e41f4b71Sopenharmony_ci```
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci### has
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_cihas(element: T): boolean
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ciChecks whether this container has the specified element.
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci**Parameters**
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
162e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
163e41f4b71Sopenharmony_ci| element | T | Yes| Target element.|
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci**Return value**
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci| Value Type | Description|
168e41f4b71Sopenharmony_ci| -------- | -------- |
169e41f4b71Sopenharmony_ci| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci**Error codes**
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci| ID| Error Message|
176e41f4b71Sopenharmony_ci| -------- | -------- |
177e41f4b71Sopenharmony_ci| 10200011 | The has method cannot be bound. |
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci**Example**
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci```ts
182e41f4b71Sopenharmony_cilet list: List<string> = new List();
183e41f4b71Sopenharmony_cilist.add("squirrel");
184e41f4b71Sopenharmony_cilet result = list.has("squirrel");
185e41f4b71Sopenharmony_ci```
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci### get
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ciget(index: number): T
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ciObtains the element at the specified position in this container.
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci**Parameters**
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
200e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
201e41f4b71Sopenharmony_ci| index | number | Yes| Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647.|
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci**Return value**
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci| Value Type | Description|
206e41f4b71Sopenharmony_ci| -------- | -------- |
207e41f4b71Sopenharmony_ci| T | Element obtained.|
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_ci**Error codes**
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci| ID| Error Message|
214e41f4b71Sopenharmony_ci| -------- | -------- |
215e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
216e41f4b71Sopenharmony_ci| 10200011 | The get method cannot be bound. |
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci**Example**
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci```ts
221e41f4b71Sopenharmony_cilet list: List<number> = new List();
222e41f4b71Sopenharmony_cilist.add(2);
223e41f4b71Sopenharmony_cilist.add(4);
224e41f4b71Sopenharmony_cilist.add(5);
225e41f4b71Sopenharmony_cilist.add(2);
226e41f4b71Sopenharmony_cilist.add(1);
227e41f4b71Sopenharmony_cilist.add(2);
228e41f4b71Sopenharmony_cilist.add(4);
229e41f4b71Sopenharmony_cilet result = list.get(2);
230e41f4b71Sopenharmony_ci```
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci### getLastIndexOf
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_cigetLastIndexOf(element: T): number
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ciObtains the index of the last occurrence of the specified element in this container.
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci**Parameters**
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
245e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
246e41f4b71Sopenharmony_ci| element | T | Yes| Target element.|
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci**Return value**
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci| Value Type | Description|
251e41f4b71Sopenharmony_ci| -------- | -------- |
252e41f4b71Sopenharmony_ci| number | Returns the index if obtained; returns **-1** otherwise.|
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci**Error codes**
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci| ID| Error Message|
259e41f4b71Sopenharmony_ci| -------- | -------- |
260e41f4b71Sopenharmony_ci| 10200011 | The getLastIndexOf method cannot be bound. |
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci**Example**
263e41f4b71Sopenharmony_ci
264e41f4b71Sopenharmony_ci```ts
265e41f4b71Sopenharmony_cilet list: List<number> = new List();
266e41f4b71Sopenharmony_cilist.add(2);
267e41f4b71Sopenharmony_cilist.add(4);
268e41f4b71Sopenharmony_cilist.add(5);
269e41f4b71Sopenharmony_cilist.add(2);
270e41f4b71Sopenharmony_cilist.add(1);
271e41f4b71Sopenharmony_cilist.add(2);
272e41f4b71Sopenharmony_cilist.add(4);
273e41f4b71Sopenharmony_cilet result = list.getLastIndexOf(2);
274e41f4b71Sopenharmony_ci```
275e41f4b71Sopenharmony_ci
276e41f4b71Sopenharmony_ci### getIndexOf
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_cigetIndexOf(element: T): number
279e41f4b71Sopenharmony_ci
280e41f4b71Sopenharmony_ciObtains the index of the first occurrence of the specified element in this container.
281e41f4b71Sopenharmony_ci
282e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
283e41f4b71Sopenharmony_ci
284e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci**Parameters**
287e41f4b71Sopenharmony_ci
288e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
289e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
290e41f4b71Sopenharmony_ci| element | T | Yes| Target element.|
291e41f4b71Sopenharmony_ci
292e41f4b71Sopenharmony_ci**Return value**
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci| Value Type | Description|
295e41f4b71Sopenharmony_ci| -------- | -------- |
296e41f4b71Sopenharmony_ci| number | Returns the position index if obtained; returns **-1** otherwise.|
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci**Error codes**
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ci| ID| Error Message|
303e41f4b71Sopenharmony_ci| -------- | -------- |
304e41f4b71Sopenharmony_ci| 10200011 | The getIndexOf method cannot be bound. |
305e41f4b71Sopenharmony_ci
306e41f4b71Sopenharmony_ci**Example**
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci```ts
309e41f4b71Sopenharmony_cilet list: List<number> = new List();
310e41f4b71Sopenharmony_cilist.add(2);
311e41f4b71Sopenharmony_cilist.add(4);
312e41f4b71Sopenharmony_cilist.add(5);
313e41f4b71Sopenharmony_cilist.add(2);
314e41f4b71Sopenharmony_cilist.add(1);
315e41f4b71Sopenharmony_cilist.add(2);
316e41f4b71Sopenharmony_cilist.add(4);
317e41f4b71Sopenharmony_cilet result = list.getIndexOf(2);
318e41f4b71Sopenharmony_ci```
319e41f4b71Sopenharmony_ci
320e41f4b71Sopenharmony_ci### equal
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ciequal(obj: Object): boolean
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ciCompares whether a specified object is equal to this container.
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ci**Parameters**
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
333e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
334e41f4b71Sopenharmony_ci| obj | Object | Yes| Object used for comparison.|
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci**Return value**
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ci| Value Type | Description|
339e41f4b71Sopenharmony_ci| -------- | -------- |
340e41f4b71Sopenharmony_ci| boolean | Returns **true** if the two are equal; returns **false** otherwise.|
341e41f4b71Sopenharmony_ci
342e41f4b71Sopenharmony_ci**Error codes**
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_ci| ID| Error Message|
347e41f4b71Sopenharmony_ci| -------- | -------- |
348e41f4b71Sopenharmony_ci| 10200011 | The equal method cannot be bound. |
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci**Example**
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci```ts
353e41f4b71Sopenharmony_cilet list: List<number> = new List();
354e41f4b71Sopenharmony_cilist.add(2);
355e41f4b71Sopenharmony_cilist.add(4);
356e41f4b71Sopenharmony_cilist.add(5);
357e41f4b71Sopenharmony_cilet obj: List<number> = new List();
358e41f4b71Sopenharmony_ciobj.add(2);
359e41f4b71Sopenharmony_ciobj.add(4);
360e41f4b71Sopenharmony_ciobj.add(5);
361e41f4b71Sopenharmony_cilet result = list.equal(obj);
362e41f4b71Sopenharmony_ci```
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_ci### removeByIndex
365e41f4b71Sopenharmony_ci
366e41f4b71Sopenharmony_ciremoveByIndex(index: number): T
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ciSearches for an element based on its index and then removes it.
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
371e41f4b71Sopenharmony_ci
372e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ci**Parameters**
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
377e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
378e41f4b71Sopenharmony_ci| index | number | Yes| Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647.|
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci**Return value**
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ci| Value Type | Description|
383e41f4b71Sopenharmony_ci| -------- | -------- |
384e41f4b71Sopenharmony_ci| T | Element removed.|
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci**Error codes**
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
389e41f4b71Sopenharmony_ci
390e41f4b71Sopenharmony_ci| ID| Error Message|
391e41f4b71Sopenharmony_ci| -------- | -------- |
392e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
393e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. |
394e41f4b71Sopenharmony_ci| 10200011 | The removeByIndex method cannot be bound. |
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci**Example**
397e41f4b71Sopenharmony_ci
398e41f4b71Sopenharmony_ci```ts
399e41f4b71Sopenharmony_cilet list: List<number> = new List();
400e41f4b71Sopenharmony_cilist.add(2);
401e41f4b71Sopenharmony_cilist.add(4);
402e41f4b71Sopenharmony_cilist.add(5);
403e41f4b71Sopenharmony_cilist.add(2);
404e41f4b71Sopenharmony_cilist.add(4);
405e41f4b71Sopenharmony_cilet result = list.removeByIndex(2);
406e41f4b71Sopenharmony_ci```
407e41f4b71Sopenharmony_ci
408e41f4b71Sopenharmony_ci### remove
409e41f4b71Sopenharmony_ci
410e41f4b71Sopenharmony_ciremove(element: T): boolean
411e41f4b71Sopenharmony_ci
412e41f4b71Sopenharmony_ciRemoves the first occurrence of the specified element from this container.
413e41f4b71Sopenharmony_ci
414e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci**Parameters**
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
421e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
422e41f4b71Sopenharmony_ci| element | T | Yes| Target element.|
423e41f4b71Sopenharmony_ci
424e41f4b71Sopenharmony_ci**Return value**
425e41f4b71Sopenharmony_ci
426e41f4b71Sopenharmony_ci| Value Type | Description|
427e41f4b71Sopenharmony_ci| -------- | -------- |
428e41f4b71Sopenharmony_ci| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
429e41f4b71Sopenharmony_ci
430e41f4b71Sopenharmony_ci**Error codes**
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
433e41f4b71Sopenharmony_ci
434e41f4b71Sopenharmony_ci| ID| Error Message|
435e41f4b71Sopenharmony_ci| -------- | -------- |
436e41f4b71Sopenharmony_ci| 10200011 | The remove method cannot be bound. |
437e41f4b71Sopenharmony_ci
438e41f4b71Sopenharmony_ci**Example**
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_ci```ts
441e41f4b71Sopenharmony_cilet list: List<number> = new List();
442e41f4b71Sopenharmony_cilist.add(2);
443e41f4b71Sopenharmony_cilist.add(4);
444e41f4b71Sopenharmony_cilist.add(5);
445e41f4b71Sopenharmony_cilist.add(4);
446e41f4b71Sopenharmony_cilet result = list.remove(2);
447e41f4b71Sopenharmony_ci```
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_ci### replaceAllElements
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_cireplaceAllElements(callbackFn: (value: T, index?: number, list?: List&lt;T&gt;) => T,
452e41f4b71Sopenharmony_cithisArg?: Object): void
453e41f4b71Sopenharmony_ci
454e41f4b71Sopenharmony_ciReplaces all elements in this container with new elements, and returns the new ones.
455e41f4b71Sopenharmony_ci
456e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
457e41f4b71Sopenharmony_ci
458e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
459e41f4b71Sopenharmony_ci
460e41f4b71Sopenharmony_ci**Parameters**
461e41f4b71Sopenharmony_ci
462e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
463e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
464e41f4b71Sopenharmony_ci| callbackFn | function | Yes| Callback invoked for the replacement.|
465e41f4b71Sopenharmony_ci| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
466e41f4b71Sopenharmony_ci
467e41f4b71Sopenharmony_cicallbackFn
468e41f4b71Sopenharmony_ci
469e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
470e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
471e41f4b71Sopenharmony_ci| value | T | Yes| Value of the element that is currently traversed.|
472e41f4b71Sopenharmony_ci| index | number | No| Position index of the element that is currently traversed. The default value is **0**.|
473e41f4b71Sopenharmony_ci| list | List&lt;T&gt; | No| Instance that calls the **replaceAllElements** API. The default value is this instance.|
474e41f4b71Sopenharmony_ci
475e41f4b71Sopenharmony_ci**Error codes**
476e41f4b71Sopenharmony_ci
477e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
478e41f4b71Sopenharmony_ci
479e41f4b71Sopenharmony_ci| ID| Error Message|
480e41f4b71Sopenharmony_ci| -------- | -------- |
481e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
482e41f4b71Sopenharmony_ci| 10200011 | The replaceAllElements method cannot be bound. |
483e41f4b71Sopenharmony_ci
484e41f4b71Sopenharmony_ci**Example**
485e41f4b71Sopenharmony_ci
486e41f4b71Sopenharmony_ci```ts
487e41f4b71Sopenharmony_cilet list: List<number> = new List();
488e41f4b71Sopenharmony_cilist.add(2);
489e41f4b71Sopenharmony_cilist.add(4);
490e41f4b71Sopenharmony_cilist.add(5);
491e41f4b71Sopenharmony_cilist.add(4);
492e41f4b71Sopenharmony_cilist.replaceAllElements((value: number) => {
493e41f4b71Sopenharmony_ci  // Add the user operation logic based on the actual scenario.
494e41f4b71Sopenharmony_ci  return value;
495e41f4b71Sopenharmony_ci});
496e41f4b71Sopenharmony_ci```
497e41f4b71Sopenharmony_ci
498e41f4b71Sopenharmony_ci### forEach
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ciforEach(callbackFn: (value: T, index?: number, List?: List&lt;T&gt;) => void,
501e41f4b71Sopenharmony_cithisArg?: Object): void
502e41f4b71Sopenharmony_ci
503e41f4b71Sopenharmony_ciUses a callback to traverse the elements in this container and obtain their position indexes.
504e41f4b71Sopenharmony_ci
505e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
506e41f4b71Sopenharmony_ci
507e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
508e41f4b71Sopenharmony_ci
509e41f4b71Sopenharmony_ci**Parameters**
510e41f4b71Sopenharmony_ci
511e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
512e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
513e41f4b71Sopenharmony_ci| callbackFn | function | Yes| Callback invoked for the replacement.|
514e41f4b71Sopenharmony_ci| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this instance.|
515e41f4b71Sopenharmony_ci
516e41f4b71Sopenharmony_cicallbackFn
517e41f4b71Sopenharmony_ci
518e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
519e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
520e41f4b71Sopenharmony_ci| value | T | Yes| Value of the element that is currently traversed.|
521e41f4b71Sopenharmony_ci| index | number | No| Position index of the element that is currently traversed. The default value is **0**.|
522e41f4b71Sopenharmony_ci| List | List&lt;T&gt; | No| Instance that calls the **forEach** API. The default value is this instance.|
523e41f4b71Sopenharmony_ci
524e41f4b71Sopenharmony_ci**Error codes**
525e41f4b71Sopenharmony_ci
526e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
527e41f4b71Sopenharmony_ci
528e41f4b71Sopenharmony_ci| ID| Error Message|
529e41f4b71Sopenharmony_ci| -------- | -------- |
530e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
531e41f4b71Sopenharmony_ci| 10200011 | The forEach method cannot be bound. |
532e41f4b71Sopenharmony_ci
533e41f4b71Sopenharmony_ci**Example**
534e41f4b71Sopenharmony_ci
535e41f4b71Sopenharmony_ci```ts
536e41f4b71Sopenharmony_cilet list: List<number> = new List();
537e41f4b71Sopenharmony_cilist.add(2);
538e41f4b71Sopenharmony_cilist.add(4);
539e41f4b71Sopenharmony_cilist.add(5);
540e41f4b71Sopenharmony_cilist.add(4);
541e41f4b71Sopenharmony_cilist.forEach((value: number, index?: number) => {
542e41f4b71Sopenharmony_ci  console.log("value:" + value, "index:" + index);
543e41f4b71Sopenharmony_ci});
544e41f4b71Sopenharmony_ci```
545e41f4b71Sopenharmony_ci
546e41f4b71Sopenharmony_ci### sort
547e41f4b71Sopenharmony_ci
548e41f4b71Sopenharmony_cisort(comparator: (firstValue: T, secondValue: T) => number): void
549e41f4b71Sopenharmony_ci
550e41f4b71Sopenharmony_ciSorts elements in this container.
551e41f4b71Sopenharmony_ci
552e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
553e41f4b71Sopenharmony_ci
554e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
555e41f4b71Sopenharmony_ci
556e41f4b71Sopenharmony_ci**Parameters**
557e41f4b71Sopenharmony_ci
558e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
559e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
560e41f4b71Sopenharmony_ci| comparator | function | Yes| Callback invoked for sorting.|
561e41f4b71Sopenharmony_ci
562e41f4b71Sopenharmony_cicomparator
563e41f4b71Sopenharmony_ci
564e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
565e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
566e41f4b71Sopenharmony_ci| firstValue | T | Yes| Previous element.|
567e41f4b71Sopenharmony_ci| secondValue | T | Yes| Next element.|
568e41f4b71Sopenharmony_ci
569e41f4b71Sopenharmony_ci**Error codes**
570e41f4b71Sopenharmony_ci
571e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_ci| ID| Error Message|
574e41f4b71Sopenharmony_ci| -------- | -------- |
575e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
576e41f4b71Sopenharmony_ci| 10200011 | The sort method cannot be bound. |
577e41f4b71Sopenharmony_ci
578e41f4b71Sopenharmony_ci**Example**
579e41f4b71Sopenharmony_ci
580e41f4b71Sopenharmony_ci```ts
581e41f4b71Sopenharmony_cilet list: List<number> = new List();
582e41f4b71Sopenharmony_cilist.add(2);
583e41f4b71Sopenharmony_cilist.add(4);
584e41f4b71Sopenharmony_cilist.add(5);
585e41f4b71Sopenharmony_cilist.add(4);
586e41f4b71Sopenharmony_cilist.sort((a: number, b: number) => a - b); // The elements are sorted in ascending order.
587e41f4b71Sopenharmony_cilist.sort((a: number, b: number) => b - a); // The elements are sorted in descending order.
588e41f4b71Sopenharmony_ci```
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ci### getSubList
591e41f4b71Sopenharmony_ci
592e41f4b71Sopenharmony_cigetSubList(fromIndex: number, toIndex: number): List&lt;T&gt;
593e41f4b71Sopenharmony_ci
594e41f4b71Sopenharmony_ciObtains elements within a range in this container, including the element at the start position but not that at the end position, and returns these elements as a new **List** instance.
595e41f4b71Sopenharmony_ci
596e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
597e41f4b71Sopenharmony_ci
598e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
599e41f4b71Sopenharmony_ci
600e41f4b71Sopenharmony_ci**Parameters**
601e41f4b71Sopenharmony_ci
602e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
603e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
604e41f4b71Sopenharmony_ci| fromIndex | number | Yes| Index of the start position.|
605e41f4b71Sopenharmony_ci| toIndex | number | Yes| Index of the end position.|
606e41f4b71Sopenharmony_ci
607e41f4b71Sopenharmony_ci**Return value**
608e41f4b71Sopenharmony_ci
609e41f4b71Sopenharmony_ci| Value Type | Description|
610e41f4b71Sopenharmony_ci| -------- | -------- |
611e41f4b71Sopenharmony_ci| List&lt;T&gt; | New **List** instance obtained.|
612e41f4b71Sopenharmony_ci
613e41f4b71Sopenharmony_ci**Error codes**
614e41f4b71Sopenharmony_ci
615e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
616e41f4b71Sopenharmony_ci
617e41f4b71Sopenharmony_ci| ID| Error Message|
618e41f4b71Sopenharmony_ci| -------- | -------- |
619e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
620e41f4b71Sopenharmony_ci| 10200001 | The value of fromIndex or toIndex is out of range. |
621e41f4b71Sopenharmony_ci| 10200011 | The getSubList method cannot be bound. |
622e41f4b71Sopenharmony_ci
623e41f4b71Sopenharmony_ci**Example**
624e41f4b71Sopenharmony_ci
625e41f4b71Sopenharmony_ci```ts
626e41f4b71Sopenharmony_cilet list: List<number> = new List();
627e41f4b71Sopenharmony_cilist.add(2);
628e41f4b71Sopenharmony_cilist.add(4);
629e41f4b71Sopenharmony_cilist.add(5);
630e41f4b71Sopenharmony_cilist.add(4);
631e41f4b71Sopenharmony_cilet result = list.getSubList(1, 3);
632e41f4b71Sopenharmony_ci```
633e41f4b71Sopenharmony_ci
634e41f4b71Sopenharmony_ci### clear
635e41f4b71Sopenharmony_ci
636e41f4b71Sopenharmony_ciclear(): void
637e41f4b71Sopenharmony_ci
638e41f4b71Sopenharmony_ciClears this container and sets its length to **0**.
639e41f4b71Sopenharmony_ci
640e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
641e41f4b71Sopenharmony_ci
642e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
643e41f4b71Sopenharmony_ci
644e41f4b71Sopenharmony_ci**Error codes**
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
647e41f4b71Sopenharmony_ci
648e41f4b71Sopenharmony_ci| ID| Error Message|
649e41f4b71Sopenharmony_ci| -------- | -------- |
650e41f4b71Sopenharmony_ci| 10200011 | The clear method cannot be bound. |
651e41f4b71Sopenharmony_ci
652e41f4b71Sopenharmony_ci**Example**
653e41f4b71Sopenharmony_ci
654e41f4b71Sopenharmony_ci```ts
655e41f4b71Sopenharmony_cilet list: List<number> = new List();
656e41f4b71Sopenharmony_cilist.add(2);
657e41f4b71Sopenharmony_cilist.add(4);
658e41f4b71Sopenharmony_cilist.add(5);
659e41f4b71Sopenharmony_cilist.add(4);
660e41f4b71Sopenharmony_cilist.clear();
661e41f4b71Sopenharmony_ci```
662e41f4b71Sopenharmony_ci
663e41f4b71Sopenharmony_ci### set
664e41f4b71Sopenharmony_ci
665e41f4b71Sopenharmony_ciset(index: number, element: T): T
666e41f4b71Sopenharmony_ci
667e41f4b71Sopenharmony_ciReplaces an element at the specified position in this container with a given element.
668e41f4b71Sopenharmony_ci
669e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
670e41f4b71Sopenharmony_ci
671e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
672e41f4b71Sopenharmony_ci
673e41f4b71Sopenharmony_ci**Parameters**
674e41f4b71Sopenharmony_ci
675e41f4b71Sopenharmony_ci| Name| Value Type | Mandatory| Description|
676e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
677e41f4b71Sopenharmony_ci| index | number | Yes| Position index of the target element. The value must be less than or equal to int32_max, that is, 2147483647.|
678e41f4b71Sopenharmony_ci| element | T | Yes| Element to be used for replacement.|
679e41f4b71Sopenharmony_ci
680e41f4b71Sopenharmony_ci**Return value**
681e41f4b71Sopenharmony_ci
682e41f4b71Sopenharmony_ci| Value Type | Description|
683e41f4b71Sopenharmony_ci| -------- | -------- |
684e41f4b71Sopenharmony_ci| T | New element.|
685e41f4b71Sopenharmony_ci
686e41f4b71Sopenharmony_ci**Error codes**
687e41f4b71Sopenharmony_ci
688e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md).
689e41f4b71Sopenharmony_ci
690e41f4b71Sopenharmony_ci| ID| Error Message|
691e41f4b71Sopenharmony_ci| -------- | -------- |
692e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
693e41f4b71Sopenharmony_ci| 10200001 | The value of index is out of range. |
694e41f4b71Sopenharmony_ci| 10200011 | The set method cannot be bound. |
695e41f4b71Sopenharmony_ci
696e41f4b71Sopenharmony_ci**Example**
697e41f4b71Sopenharmony_ci
698e41f4b71Sopenharmony_ci```ts
699e41f4b71Sopenharmony_cilet list: List<number | string> = new List();
700e41f4b71Sopenharmony_cilist.add(2);
701e41f4b71Sopenharmony_cilist.add(4);
702e41f4b71Sopenharmony_cilist.add(5);
703e41f4b71Sopenharmony_cilist.add(4);
704e41f4b71Sopenharmony_cilet result = list.set(2, "b");
705e41f4b71Sopenharmony_ci```
706e41f4b71Sopenharmony_ci
707e41f4b71Sopenharmony_ci### convertToArray
708e41f4b71Sopenharmony_ci
709e41f4b71Sopenharmony_ciconvertToArray(): Array&lt;T&gt;
710e41f4b71Sopenharmony_ci
711e41f4b71Sopenharmony_ciConverts this container into an array.
712e41f4b71Sopenharmony_ci
713e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
714e41f4b71Sopenharmony_ci
715e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
716e41f4b71Sopenharmony_ci
717e41f4b71Sopenharmony_ci**Return value**
718e41f4b71Sopenharmony_ci
719e41f4b71Sopenharmony_ci| Value Type | Description|
720e41f4b71Sopenharmony_ci| -------- | -------- |
721e41f4b71Sopenharmony_ci| Array&lt;T&gt; | Array obtained.|
722e41f4b71Sopenharmony_ci
723e41f4b71Sopenharmony_ci**Error codes**
724e41f4b71Sopenharmony_ci
725e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
726e41f4b71Sopenharmony_ci
727e41f4b71Sopenharmony_ci| ID| Error Message|
728e41f4b71Sopenharmony_ci| -------- | -------- |
729e41f4b71Sopenharmony_ci| 10200011 | The convertToArray method cannot be bound. |
730e41f4b71Sopenharmony_ci
731e41f4b71Sopenharmony_ci**Example**
732e41f4b71Sopenharmony_ci
733e41f4b71Sopenharmony_ci```ts
734e41f4b71Sopenharmony_cilet list: List<number> = new List();
735e41f4b71Sopenharmony_cilist.add(2);
736e41f4b71Sopenharmony_cilist.add(4);
737e41f4b71Sopenharmony_cilist.add(5);
738e41f4b71Sopenharmony_cilist.add(4);
739e41f4b71Sopenharmony_cilet result = list.convertToArray();
740e41f4b71Sopenharmony_ci```
741e41f4b71Sopenharmony_ci
742e41f4b71Sopenharmony_ci### isEmpty
743e41f4b71Sopenharmony_ci
744e41f4b71Sopenharmony_ciisEmpty(): boolean
745e41f4b71Sopenharmony_ci
746e41f4b71Sopenharmony_ciChecks whether this container is empty (contains no element).
747e41f4b71Sopenharmony_ci
748e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
749e41f4b71Sopenharmony_ci
750e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
751e41f4b71Sopenharmony_ci
752e41f4b71Sopenharmony_ci**Return value**
753e41f4b71Sopenharmony_ci
754e41f4b71Sopenharmony_ci| Value Type | Description|
755e41f4b71Sopenharmony_ci| -------- | -------- |
756e41f4b71Sopenharmony_ci| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
757e41f4b71Sopenharmony_ci
758e41f4b71Sopenharmony_ci**Error codes**
759e41f4b71Sopenharmony_ci
760e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
761e41f4b71Sopenharmony_ci
762e41f4b71Sopenharmony_ci| ID| Error Message|
763e41f4b71Sopenharmony_ci| -------- | -------- |
764e41f4b71Sopenharmony_ci| 10200011 | The isEmpty method cannot be bound. |
765e41f4b71Sopenharmony_ci
766e41f4b71Sopenharmony_ci**Example**
767e41f4b71Sopenharmony_ci
768e41f4b71Sopenharmony_ci```ts
769e41f4b71Sopenharmony_cilet list: List<number> = new List();
770e41f4b71Sopenharmony_cilist.add(2);
771e41f4b71Sopenharmony_cilist.add(4);
772e41f4b71Sopenharmony_cilist.add(5);
773e41f4b71Sopenharmony_cilist.add(4);
774e41f4b71Sopenharmony_cilet result = list.isEmpty();
775e41f4b71Sopenharmony_ci```
776e41f4b71Sopenharmony_ci
777e41f4b71Sopenharmony_ci### getFirst
778e41f4b71Sopenharmony_ci
779e41f4b71Sopenharmony_cigetFirst(): T
780e41f4b71Sopenharmony_ci
781e41f4b71Sopenharmony_ciObtains the first element in this container.
782e41f4b71Sopenharmony_ci
783e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
784e41f4b71Sopenharmony_ci
785e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
786e41f4b71Sopenharmony_ci
787e41f4b71Sopenharmony_ci**Return value**
788e41f4b71Sopenharmony_ci
789e41f4b71Sopenharmony_ci| Value Type | Description|
790e41f4b71Sopenharmony_ci| -------- | -------- |
791e41f4b71Sopenharmony_ci| T | The first element obtained.|
792e41f4b71Sopenharmony_ci
793e41f4b71Sopenharmony_ci**Error codes**
794e41f4b71Sopenharmony_ci
795e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
796e41f4b71Sopenharmony_ci
797e41f4b71Sopenharmony_ci| ID| Error Message|
798e41f4b71Sopenharmony_ci| -------- | -------- |
799e41f4b71Sopenharmony_ci| 10200011 | The getFirst method cannot be bound. |
800e41f4b71Sopenharmony_ci
801e41f4b71Sopenharmony_ci**Example**
802e41f4b71Sopenharmony_ci
803e41f4b71Sopenharmony_ci```ts
804e41f4b71Sopenharmony_cilet list: List<number> = new List();
805e41f4b71Sopenharmony_cilist.add(2);
806e41f4b71Sopenharmony_cilist.add(4);
807e41f4b71Sopenharmony_cilist.add(5);
808e41f4b71Sopenharmony_cilist.add(4);
809e41f4b71Sopenharmony_cilet result = list.getFirst();
810e41f4b71Sopenharmony_ci```
811e41f4b71Sopenharmony_ci
812e41f4b71Sopenharmony_ci### getLast
813e41f4b71Sopenharmony_ci
814e41f4b71Sopenharmony_cigetLast(): T
815e41f4b71Sopenharmony_ci
816e41f4b71Sopenharmony_ciObtains the last element in this container.
817e41f4b71Sopenharmony_ci
818e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
819e41f4b71Sopenharmony_ci
820e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
821e41f4b71Sopenharmony_ci
822e41f4b71Sopenharmony_ci**Return value**
823e41f4b71Sopenharmony_ci
824e41f4b71Sopenharmony_ci| Value Type | Description|
825e41f4b71Sopenharmony_ci| -------- | -------- |
826e41f4b71Sopenharmony_ci| T | The last element obtained.|
827e41f4b71Sopenharmony_ci
828e41f4b71Sopenharmony_ci**Error codes**
829e41f4b71Sopenharmony_ci
830e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
831e41f4b71Sopenharmony_ci
832e41f4b71Sopenharmony_ci| ID| Error Message|
833e41f4b71Sopenharmony_ci| -------- | -------- |
834e41f4b71Sopenharmony_ci| 10200011 | The getLast method cannot be bound. |
835e41f4b71Sopenharmony_ci
836e41f4b71Sopenharmony_ci**Example**
837e41f4b71Sopenharmony_ci
838e41f4b71Sopenharmony_ci```ts
839e41f4b71Sopenharmony_cilet list: List<number> = new List();
840e41f4b71Sopenharmony_cilist.add(2);
841e41f4b71Sopenharmony_cilist.add(4);
842e41f4b71Sopenharmony_cilist.add(5);
843e41f4b71Sopenharmony_cilist.add(4);
844e41f4b71Sopenharmony_cilet result = list.getLast();
845e41f4b71Sopenharmony_ci```
846e41f4b71Sopenharmony_ci
847e41f4b71Sopenharmony_ci### [Symbol.iterator]
848e41f4b71Sopenharmony_ci
849e41f4b71Sopenharmony_ci[Symbol.iterator]\(): IterableIterator&lt;T&gt;
850e41f4b71Sopenharmony_ci
851e41f4b71Sopenharmony_ciObtains an iterator, each item of which is a JavaScript object.
852e41f4b71Sopenharmony_ci
853e41f4b71Sopenharmony_ci> **NOTE**
854e41f4b71Sopenharmony_ci>
855e41f4b71Sopenharmony_ci> This API cannot be used in .ets files.
856e41f4b71Sopenharmony_ci
857e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12.
858e41f4b71Sopenharmony_ci
859e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Utils.Lang
860e41f4b71Sopenharmony_ci
861e41f4b71Sopenharmony_ci**Return value**
862e41f4b71Sopenharmony_ci
863e41f4b71Sopenharmony_ci| Value Type | Description|
864e41f4b71Sopenharmony_ci| -------- | -------- |
865e41f4b71Sopenharmony_ci| IterableIterator&lt;T&gt; | Iterator obtained.|
866e41f4b71Sopenharmony_ci
867e41f4b71Sopenharmony_ci**Error codes**
868e41f4b71Sopenharmony_ci
869e41f4b71Sopenharmony_ciFor details about the error codes, see [Utils Error Codes](errorcode-utils.md).
870e41f4b71Sopenharmony_ci
871e41f4b71Sopenharmony_ci| ID| Error Message|
872e41f4b71Sopenharmony_ci| -------- | -------- |
873e41f4b71Sopenharmony_ci| 10200011 | The Symbol.iterator method cannot be bound. |
874e41f4b71Sopenharmony_ci
875e41f4b71Sopenharmony_ci**Example**
876e41f4b71Sopenharmony_ci
877e41f4b71Sopenharmony_ci```ts
878e41f4b71Sopenharmony_cilet list: List<number> = new List();
879e41f4b71Sopenharmony_cilist.add(2);
880e41f4b71Sopenharmony_cilist.add(4);
881e41f4b71Sopenharmony_cilist.add(5);
882e41f4b71Sopenharmony_cilist.add(4);
883e41f4b71Sopenharmony_ci
884e41f4b71Sopenharmony_ci// Method 1:
885e41f4b71Sopenharmony_cilet items = Array.from(list)
886e41f4b71Sopenharmony_cifor (let item of items) {
887e41f4b71Sopenharmony_ci  console.log("value: " + item);
888e41f4b71Sopenharmony_ci}
889e41f4b71Sopenharmony_ci
890e41f4b71Sopenharmony_ci// Method 2:
891e41f4b71Sopenharmony_cilet iter = list[Symbol.iterator]();
892e41f4b71Sopenharmony_cilet temp: IteratorResult<number> = iter.next();
893e41f4b71Sopenharmony_ciwhile(!temp.done) {
894e41f4b71Sopenharmony_ci  console.log("value: " + temp.value);
895e41f4b71Sopenharmony_ci  temp = iter.next();
896e41f4b71Sopenharmony_ci}
897e41f4b71Sopenharmony_ci```
898