• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Linear Containers
2
3
4Linear containers, underpinned by arrays, implement a data structure that enables sequential access. There are several types of linear containers: ArrayList, Vector, List, LinkedList, Deque, Queue, and Stack.
5
6
7Linear containers prioritize data access speed, enabling operations such as adding, removing, modifying, and accessing elements with a single bytecode instruction at runtime.
8
9## Comparison of Linear Container Types
10
11| Type| Characteristics and Recommended Use Cases|
12| --------- | ------- |
13| ArrayList | Dynamic array, which occupies a contiguous block of memory. This type is recommended for frequent element access.|
14| List | Singly linked list, where memory can be non-contiguous. This type is recommended for frequent insertions and deletions when using a singly linked list.|
15| LinkedList | Doubly linked list, where memory can be non-contiguous. This type is recommended for frequent insertions and deletions when using a doubly linked list.|
16| Deque | Double-ended queue, which allows element operations at both ends, and occupies a contiguous block of memory. This type is recommended for frequent access and manipulation of head and tail elements.|
17| Queue | Queue, which inserts elements at the tail and removes them from the head, and occupies a contiguous block of memory. This type is suitable for First In First Out (FIFO) scenarios.|
18| Stack | Stack, which allows insertions and deletions only at one end, and occupies a contiguous block of memory. This type is suitable for Last In First Out (LIFO) scenarios.|
19| Vector | Dynamic array, which occupies a contiguous block of memory. This type is no longer maintained; use ArrayList instead.|
20
21## ArrayList
22
23[ArrayList](../reference/apis-arkts/js-apis-arraylist.md) is a dynamic array used to construct a global array object. It is recommended for frequent element access.
24
25Defined by generics, ArrayList requires a contiguous block of memory for storage, with an initial capacity of 10 and supports dynamic resizing, increasing its size by 1.5 times the original capacity each time.
26
27Common APIs for adding, removing, modifying, and accessing elements in ArrayList are as follows:
28
29| Operation| API| Description|
30| --------- | ------- | ------- |
31| Adding elements| add(element: T) | Adds an element to the end of the array.|
32| Adding elements| insert(element: T, index: number) | Inserts an element at the specified index.|
33| Accessing elements| arr[index: number] | Obtains the value at the specified index.|
34| Accessing elements| forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, thisArg?: Object) | Iterates over all elements in the ArrayList. **callbackFn** is a callback function used to process each element in the **forEach** method. It receives the current element, index, and original list as parameters.|
35| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
36| Modifying elements| arr[index] = xxx | Modifies the value at the specified index.|
37| Removing elements| remove(element: T) | Removes the first matching element.|
38| Removing elements| removeByRange(fromIndex: number, toIndex:number) | Removes elements within the specified range.|
39
40## List
41
42[List](../reference/apis-arkts/js-apis-list.md) is used to construct a singly linked list, which supports access only through the head node to the tail node. Defined by generics, List's storage locations in memory can be non-contiguous.
43
44Unlike [LinkedList](../reference/apis-arkts/js-apis-linkedlist.md), which is a doubly linked list and allows quick insertions and deletions at both ends, List is a singly linked list and does not support bidirectional operations.
45
46If elements need to be frequently inserted and deleted and a singly linked list is required, you are advised to use List.
47
48Common APIs for adding, removing, modifying, and accessing elements in List are as follows:
49
50| Operation| API| Description|
51| --------- | ------- | ------- |
52| Adding elements| add(element: T) | Adds an element to the end of the array.|
53| Adding elements| insert(element: T, index: number) | Adds an element at the specified index.|
54| Accessing elements| get(index: number) | Obtains the element at the specified index.|
55| Accessing elements| list[index: number] | Obtains the element at the specified index. However, this will result in undefined behavior.|
56| Accessing elements| getFirst() | Obtains the first element.|
57| Accessing elements| getLast() | Obtains the last element.|
58| Accessing elements| getIndexOf(element: T) | Obtains the index of the first matching element.|
59| Accessing elements| getLastIndexOf(element: T) | Obtains the index of the last matching element.|
60| Accessing elements| forEach(callbackfn: (value:T, index?: number, list?: List<T>)=> void,thisArg?: Object) | Iterates over all elements in the List.|
61| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
62| Modifying elements| set(index:number, element: T) | Modifies the element at the specified index.|
63| Modifying elements| list[index] = element | Modifies the element at the specified index. This API does not make any actual changes to the nodes in the linked list. Instead, it simply adds a property to the object. This can cause the program's state to become inconsistent with the actual contents of the linked list, leading to undefined behavior.|
64| Modifying elements| replaceAllElements(callbackFn:(value: T,index?: number,list?: List<T>)=>T,thisArg?: Object) | Replaces all elements in the List.|
65| Removing elements| remove(element: T) | Compares each element in the linked list using the === operator and removes the first node that matches. For object types, the element will only be removed if the provided object reference is identical to the node's reference in the linked list.|
66| Removing elements| removeByIndex(index:number) | Removes the element at the specified index. If the index is out of range, an "out of range" error is reported|
67
68## LinkedList
69
70[LinkedList](../reference/apis-arkts/js-apis-linkedlist.md) is used to construct a doubly linked list, which can be traversed in both directions from any node. Defined by generics, LinkedList's storage locations in memory can be non-contiguous.
71
72Unlike [List](../reference/apis-arkts/js-apis-list.md), which is a singly linked list and does not support bidirectional operations, LinkedList is a doubly linked list and allows quick insertions and deletions at both ends.
73
74Compared with [ArrayList](../reference/apis-arkts/js-apis-arraylist.md), LinkedList is more efficient for inserting data, whereas ArrayList is more efficient for querying data.
75
76If elements need to be frequently inserted and deleted and a doubly linked list is required, you are advised to use LinkedList.
77
78Common APIs for adding, removing, modifying, and accessing elements in LinkedList are as follows:
79
80| Operation| API| Description|
81| --------- | ------- | ------- |
82| Adding elements| add(element: T) | Adds an element to the end of the array.|
83| Adding elements| insert(element: T, index: number) | Inserts an element at the specified index.|
84| Accessing elements| get(index: number) | Obtains the element at the specified index.|
85| Accessing elements| list[index: number] | Obtains the element at the specified index. However, this will result in undefined behavior.|
86| Accessing elements| getFirst() | Obtains the first element.|
87| Accessing elements| getLast() | Obtains the last element.|
88| Accessing elements| getIndexOf(element: T) | Obtains the index of the first matching element.|
89| Accessing elements| getLastIndexOf(element: T) | Obtains the index of the last matching element.|
90| Accessing elements| forEach(callbackFn: (value: T, index?: number, list?: LinkedList<T>) => void, thisArg?: Object) | Iterates over all elements in the LinkedList.|
91| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
92| Modifying elements| set(index:number, element: T) | Modifies the element at the specified index.|
93| Modifying elements| list[index] = element | Modifies the element at the specified index. However, this will result in undefined behavior.|
94| Removing elements| remove(element: T) | Removes the first matching element.|
95| Removing elements| removeByIndex(index:number) | Deletes the element at the specified index.|
96
97## Deque
98
99[Deque](../reference/apis-arkts/js-apis-deque.md) is used to construct a double-ended queue (deque) that follows the principles of FIFO and LIFO. It allows insertion and removal of elements at both ends.
100
101Defined by generics, Deque requires a contiguous block of memory for storage, with an initial capacity of 8 and supports dynamic resizing, doubling its size each time. Deque is implemented using a circular queue, ensuring efficient enqueue and dequeue operations.
102
103Unlike [Queue](../reference/apis-arkts/js-apis-queue.md), which only allows element removal at the head and insertion at the tail, Deque allows operations at both ends.
104
105Compared with [Vector](../reference/apis-arkts/js-apis-vector.md), both support element operations at both ends, but Deque does not allow insertions in the middle. Deque is more efficient for inserting and deleting elements at the head, whereas Vector is more efficient for accessing elements.
106
107Deque is recommended for frequent insertions and deletions at both ends of the container.
108
109Common APIs for adding, removing, modifying, and accessing elements in Deque are as follows:
110
111| Operation| API| Description|
112| --------- | ------- | ------- |
113| Adding elements| insertFront(element: T) | Adds an element to the front of the Deque.|
114| Adding elements| insertEnd(element: T) | Adds an element to the end of the Deque.|
115| Accessing elements| getFirst() | Obtains the first element without dequeuing.|
116| Accessing elements| getLast() | Obtains the last element without dequeuing.|
117| Accessing elements| forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>) => void, thisArg?: Object) | Iterates over all elements in the Deque.|
118| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
119| Modifying elements| forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>)=> void, thisArg?: Object) | Modifies all elements in the Deque through iteration.|
120| Removing elements| popFirst() | Removes the first element from the queue and returns it. If the queue is empty, undefined is returned.|
121| Removing elements| popLast() | Removes the last element from the queue and returns it. If the queue is empty, undefined is returned.|
122
123## Queue
124
125[Queue](../reference/apis-arkts/js-apis-queue.md) is used to construct a queue that follows the FIFO principle.
126
127Defined by generics, Queue requires a contiguous block of memory for storage, with an initial capacity of 8 and supports dynamic resizing, doubling its size each time.
128
129Queue is implemented using a circular queue, ensuring efficient enqueue and dequeue operations.
130
131Unlike [Deque](../reference/apis-arkts/js-apis-deque.md), which supports insertion and removal at both the ends, Queue supports insertion at one end and removal at the other.
132
133Queue is suitable for FIFO scenarios.
134
135Common APIs for adding, removing, modifying, and accessing elements in Queue are as follows:
136
137| Operation| API| Description|
138| --------- | ------- | ------- |
139| Adding elements| add(element: T) | Adds an element to the end of the Queue.|
140| Accessing elements| getFirst() | Obtains the first element without dequeuing.|
141| Accessing elements| forEach(callbackFn: (value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object) | Iterates over all elements in the Queue.|
142| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
143| Modifying elements| forEach(callbackFn: (value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object) | Modifies all elements in the Queue through iteration.|
144| Removing elements| pop() | Removes the first element from the queue and returns it.|
145
146## Stack
147
148[Stack](../reference/apis-arkts/js-apis-stack.md) is used to construct a stack that follows the Last Out First In (LOFI) principle.
149
150Defined by generics, Stack requires a contiguous block of memory for storage, with an initial capacity of 8 and supports dynamic resizing, increasing its size by 1.5 times the original capacity each time. Stack is implemented using an array, with all operations performed at one end.
151
152Unlike [Queue](../reference/apis-arkts/js-apis-queue.md), which is implemented using a circular queue and allows removal only from the front and addition only at the rear, Stack supports insertion and removal at one end.
153
154Stack is suitable for LOFI scenarios.
155
156Common APIs for adding, removing, modifying, and accessing elements in Stack are as follows:
157
158| Operation| API| Description|
159| --------- | ------- | ------- |
160| Adding elements| push(item: T) | Adds an element to the top of the Stack.|
161| Accessing elements| peek() | Obtains the top element of the Stack without dequeuing.|
162| Accessing elements| locate(element: T) | Obtains the position of an element.|
163| Accessing elements| forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object) | Iterates over all elements in the Stack.|
164| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
165| Modifying elements| forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object) | Modifies all elements in the Stack through iteration.|
166| Removing elements| pop() | Removes the first element from the stack and returns it.|
167
168## Vector
169
170> **NOTE**
171>
172> Since API version 9, this API is no longer maintained. Use [ArrayList](../reference/apis-arkts/js-apis-arraylist.md) instead.
173
174[Vector](../reference/apis-arkts/js-apis-vector.md) is a continuous storage structure used to construct a global array object. Defined by generics, Vector requires a contiguous block of memory for storage, with an initial capacity of 10 and supports dynamic resizing, doubling its size each time.
175
176Vector, like [ArrayList](../reference/apis-arkts/js-apis-arraylist.md), is based on arrays but provides more array manipulation interfaces. In addition to operator access, Vector provides the getter and setter to provide more comprehensive verification and error tolerance mechanisms.
177
178Common APIs for adding, removing, modifying, and accessing elements in Vector are as follows:
179
180| Operation| API| Description|
181| --------- | ------- | ------- |
182| Adding elements| add(element: T) | Adds an element to the end of the array.|
183| Adding elements| insert(element: T, index: number) | Inserts an element at the specified index.|
184| Accessing elements| get(index: number) | Obtains the element at the specified index.|
185| Accessing elements| vec[index: number] | Obtains the element at the specified index, ensuring fast access.|
186| Accessing elements| getFirst() | Obtains the first element.|
187| Accessing elements| getLastElement() | Obtains the last element.|
188| Accessing elements| getIndexOf(element: T) | Obtains the index of the first matching element.|
189| Accessing elements| getLastIndexOf(element: T) | Obtains the index of the last matching element.|
190| Accessing elements| forEach(callbackFn: (value: T, index?: number, Vector?: Vector<T>) => void, thisArg?: Object) | Iterates over all elements in the Vector.|
191| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
192| Modifying elements| set(index:number, element: T) | Modifies the element at the specified index.|
193| Modifying elements| vec[index] = element | Modifies the element at the specified index.|
194| Modifying elements| replaceAllElements(callbackFn: (value: T, index?: number, vector?: Vector<T>) => T, thisArg?: Object) | Replaces all elements in the Vector.|
195| Modifying elements| setLength(newSize:number) | Sets the length of the Vector.|
196| Removing elements| remove(element: T) | Removes the first matching element.|
197| Removing elements| removeByIndex(index:number) | Removes the element at the specified index.|
198| Removing elements| removeByRange(fromIndex:number,toIndex:number) | Removes elements within the specified range.|
199
200## Use of Linear Containers
201
202This section provides usage examples for common linear containers, including ArrayList, Deque, Stack, and List, covering importing modules, adding elements, accessing elements, and modifying elements. The example code is as follows:
203
204
205```ts
206// ArrayList
207import { ArrayList } from '@kit.ArkTS'; // Import the ArrayList module.
208
209let arrayList1: ArrayList<string> = new ArrayList();
210arrayList1.add('a'); // Add an element with the value 'a'.
211let arrayList2: ArrayList<number> = new ArrayList();
212arrayList2.add(1); // Add an element with the value 1.
213console.info(`result: ${arrayList2[0]}`); // Access the element at index 0. Output: result: 1
214arrayList1[0] = 'one'; // Modify the element at index 0.
215console.info(`result: ${arrayList1[0]}`); // Output: result: one
216
217// Deque
218import { Deque } from '@kit.ArkTS'; // Import the Deque module.
219
220let deque1: Deque<string> = new Deque();
221deque1.insertFront('a'); // Add an element with the value 'a' to the header.
222let deque2: Deque<number> = new Deque();
223deque2.insertFront(1); // Add an element with the value 1 to the header.
224console.info(`result: ${deque2.getFirst()}`); // Access the first element. Output: result: 1
225deque1.insertEnd('one'); // Add an element with the value 'one'.
226console.info(`result: ${deque1.getLast()}`); // Access the last element. Output: result: one
227
228// Stack
229import { Stack } from '@kit.ArkTS'; // Import the Stack module.
230
231let stack1: Stack<string> = new Stack();
232stack1.push('a'); // Add an element with the value 'a' to the Stack.
233let stack2: Stack<number> = new Stack();
234stack2.push(1); // Add an element with the value 1 to the Stack.
235console.info(`result: ${stack1.peek()}`); // Access the top element of the Stack. Output: result: a
236console.info(`result: ${stack2.pop()}`); // Remove and return the top element. Output: result: 1
237console.info(`result: ${stack2.length}`); // Output: result: 0
238
239// List
240import { List } from '@kit.ArkTS'; // Import the List module.
241
242let list1: List<string> = new List();
243list1.add('a'); // Add an element with the value 'a'.
244let list2: List<number> = new List();
245list2.insert(0, 0); // Insert an element with the value 0 at index 0.
246let list3: List<Array<number>> = new List();
247let b2 = [1, 2, 3];
248list3.add(b2); // Add an element of the Array type.
249console.info(`result: ${list1[0]}`); // Access the element at index 0. Output: result: a
250console.info(`result: ${list3.get(0)}`); // Access the element at index 0. Output: result: 1,2,3
251```
252