• 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, ensuring fast access.|
34| Accessing elements| forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, thisArg?: Object) | Iterates over all elements in the ArrayList.|
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) | Inserts 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. However, this will result in 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) | Removes the first matching element.|
66| Removing elements| removeByIndex(index:number) | Removes the element at the specified index.|
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| popFirst() | Obtains and removes the first element.|
118| Accessing elements| popLast() | Obtains and removes the last element.|
119| Accessing elements| forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>) => void, thisArg?: Object) | Iterates over all elements in the Deque.|
120| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
121| Modifying elements| forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>)=> void, thisArg?: Object) | Modifies all elements in the Deque through iteration.|
122| Removing elements| popFirst() | Removes and returns the first element.|
123| Removing elements| popLast() | Removes and returns the last element.|
124
125## Queue
126
127[Queue](../reference/apis-arkts/js-apis-queue.md) is used to construct a queue that follows the FIFO principle.
128
129Defined 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.
130
131Queue is implemented using a circular queue, ensuring efficient enqueue and dequeue operations.
132
133Unlike [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.
134
135Queue is suitable for FIFO scenarios.
136
137Common APIs for adding, removing, modifying, and accessing elements in Queue are as follows:
138
139| Operation| API| Description|
140| --------- | ------- | ------- |
141| Adding elements| add(element: T) | Adds an element to the end of the Queue.|
142| Accessing elements| getFirst() | Obtains the first element without dequeuing.|
143| Accessing elements| pop() | Obtains and removes the first element.|
144| Accessing elements| forEach(callbackFn: (value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object) | Iterates over all elements in the Queue.|
145| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
146| Modifying elements| forEach(callbackFn: (value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object) | Modifies all elements in the Queue through iteration.|
147| Removing elements| pop() | Removes and returns the first element.|
148
149## Stack
150
151[Stack](../reference/apis-arkts/js-apis-stack.md) is used to construct a stack that follows the Last Out First In (LOFI) principle.
152
153Defined 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.
154
155Unlike [Queue](../reference/apis-arkts/js-apis-queue.md), which is implemented using a circular queue and allows insertion at one end and removal at the other, Stack supports insertion and removal at one end.
156
157Stack is suitable for LOFI scenarios.
158
159Common APIs for adding, removing, modifying, and accessing elements in Stack are as follows:
160
161| Operation| API| Description|
162| --------- | ------- | ------- |
163| Adding elements| push(item: T) | Adds an element to the top of the Stack.|
164| Accessing elements| peek() | Obtains the top element of the Stack without dequeuing.|
165| Accessing elements| pop() | Obtains and removes the top element of the Stack.|
166| Accessing elements| locate(element: T) | Obtains the position of an element.|
167| Accessing elements| forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object) | Iterates over all elements in the Stack.|
168| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
169| Modifying elements| forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object) | Modifies all elements in the Stack through iteration.|
170| Removing elements| pop() | Removes and returns the top element.|
171
172## Vector
173
174> **NOTE**
175>
176> Since API version 9, this API is no longer maintained. Use [ArrayList](../reference/apis-arkts/js-apis-arraylist.md) instead.
177
178[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.
179
180Vector, 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.
181
182Common APIs for adding, removing, modifying, and accessing elements in Vector are as follows:
183
184| Operation| API| Description|
185| --------- | ------- | ------- |
186| Adding elements| add(element: T) | Adds an element to the end of the array.|
187| Adding elements| insert(element: T, index: number) | Inserts an element at the specified index.|
188| Accessing elements| get(index: number) | Obtains the element at the specified index.|
189| Accessing elements| vec\[index: number] | Obtains the element at the specified index, ensuring fast access.|
190| Accessing elements| getFirst() | Obtains the first element.|
191| Accessing elements| getLastElement() | Obtains the last element.|
192| Accessing elements| getIndexOf(element: T) | Obtains the index of the first matching element.|
193| Accessing elements| getLastIndexOf(element: T) | Obtains the index of the last matching element.|
194| Accessing elements| forEach(callbackFn: (value: T, index?: number, Vector?: Vector<T>) => void, thisArg?: Object) | Iterates over all elements in the Vector.|
195| Accessing elements| \[Symbol.iterator]():IterableIterator<T> | Creates an iterator for data access.|
196| Modifying elements| set(index:number, element: T) | Modifies the element at the specified index.|
197| Modifying elements| vec\[index] = element | Modifies the element at the specified index.|
198| Modifying elements| replaceAllElements(callbackFn:(value: T,index?: number,list?: List<T>)=>T,thisArg?: Object) | Replaces all elements in the Vector.|
199| Modifying elements| setLength(newSize:number) | Sets the length of the Vector.|
200| Removing elements| remove(element: T) | Removes the first matching element.|
201| Removing elements| removeByIndex(index:number) | Removes the element at the specified index.|
202| Removing elements| removeByRange(fromIndex:number,toIndex:number) | Removes elements within the specified range.|
203
204## Use of Linear Containers
205
206This 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:
207
208
209```ts
210// ArrayList
211import { ArrayList } from '@kit.ArkTS'; // Import the ArrayList module.
212
213let arrayList1: ArrayList<string> = new ArrayList();
214arrayList1.add('a'); // Add an element with the value 'a'.
215let arrayList2: ArrayList<number> = new ArrayList();
216arrayList2.add(1); // Add an element with the value 1.
217console.info(`result: ${arrayList2[0]}`); // Access the element at index 0. Output: result: 1
218arrayList1[0] = 'one'; // Modify the element at index 0.
219console.info(`result: ${arrayList1[0]}`); // Output: result: one
220
221// Deque
222import { Deque } from '@kit.ArkTS'; // Import the Deque module.
223
224let deque1: Deque<string> = new Deque();
225deque1.insertFront('a'); // Add an element with the value 'a' to the header.
226let deque2: Deque<number> = new Deque();
227deque2.insertFront(1); // Add an element with the value 1 to the header.
228console.info(`result: ${deque2.getFirst()}`); // Access the first element. Output: result: 1
229deque1.insertEnd('one'); // Add an element with the value 'one'.
230console.info(`result: ${deque1.getLast()}`); // Access the last element. Output: result: one
231
232// Stack
233import { Stack } from '@kit.ArkTS'; // Import the Stack module.
234
235let stack1: Stack<string> = new Stack();
236stack1.push('a'); // Add an element with the value 'a' to the Stack.
237let stack2: Stack<number> = new Stack();
238stack2.push(1); // Add an element with the value 1 to the Stack.
239console.info(`result: ${stack1.peek()}`); // Access the top element of the Stack. Output: result: a
240console.info(`result: ${stack2.pop()}`); // Remove and return the top element. Output: result: 1
241console.info(`result: ${stack2.length}`); // Output: result: 0
242
243// List
244import { List } from '@kit.ArkTS'; // Import the List module.
245
246let list1: List<string> = new List();
247list1.add('a'); // Add an element with the value 'a'.
248let list2: List<number> = new List();
249list2.insert(0, 0); // Insert an element with the value 0 at index 0.
250let list3: List<Array<number>> = new List();
251let b2 = [1, 2, 3];
252list3.add(b2); // Add an element of the Array type.
253console.info(`result: ${list1[0]}`); // Access the element at index 0. Output: result: a
254console.info(`result: ${list3.get(0)}`); // Access the element at index 0. Output: result: 1,2,3
255```
256