1e41f4b71Sopenharmony_ci# Linear Containers 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciLinear 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**. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciTo accelerate data access, linear containers support Create, Read, Update, and Delete (CRUD) through a bytecode instruction at runtime. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## ArrayList 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci[ArrayList](../reference/apis-arkts/js-apis-arraylist.md) is a dynamic array that can be used to construct a global array object. You are advised to use **ArrayList** when elements in a container need to be frequently read. 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci**ArrayList** uses generics and must be stored in a contiguous memory space. Its initial capacity is 10, and it increases capacity 1.5-fold in each dynamic expansion. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci**ArrayList** provides the following CRUD APIs. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci| Operation| Description| 19e41f4b71Sopenharmony_ci| --------- | ------- | 20e41f4b71Sopenharmony_ci| Create| Use **add(element: T)** to add an element at the end of this container.| 21e41f4b71Sopenharmony_ci| Create| Use **insert(element: T, index: number)** to insert an element at a given position (specified by **index**).| 22e41f4b71Sopenharmony_ci| Read| Use **arr\[index]** to obtain the value at a given position (specified by **index**).| 23e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, thisArg?: Object): void** to traverse the elements in this container.| 24e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 25e41f4b71Sopenharmony_ci| Update| Use **arr\[index] = xxx** to change the value at a given position (specified by **index**).| 26e41f4b71Sopenharmony_ci| Delete| Use **remove(element: T)** to remove the first occurrence of the specified element.| 27e41f4b71Sopenharmony_ci| Delete| Use **removeByRange(fromIndex: number, toIndex: number)** to remove all of the elements within a range.| 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci## Vector 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci> **NOTE** 33e41f4b71Sopenharmony_ci> 34e41f4b71Sopenharmony_ci> The APIs provided by **Vector** are deprecated since API version 9. You are advised to use [ArrayList](../reference/apis-arkts/js-apis-arraylist.md). 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci[Vector](../reference/apis-arkts/js-apis-vector.md) is a continuous storage structure that can be used to construct a global array object. **Vector** uses generics and must be stored in a contiguous memory space. Its initial capacity is 10, and it has capacity doubled in each dynamic expansion. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciBoth **Vector** and [ArrayList](../reference/apis-arkts/js-apis-arraylist.md) are implemented based on arrays, but **Vector** provides more interfaces for operating the arrays. In addition to operator access, **Vector** provides the getter and setter to provide a more complete verification and error tolerance mechanism. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**Vector** provides the following CRUD APIs. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| Operation| Description| 43e41f4b71Sopenharmony_ci| --------- | ------- | 44e41f4b71Sopenharmony_ci| Create| Use **add(element: T)** to add an element at the end of this container.| 45e41f4b71Sopenharmony_ci| Create| Use **insert(element: T, index: number)** to insert an element at a given position (specified by **index**).| 46e41f4b71Sopenharmony_ci| Read| Use **vec\[index]** to obtain the value at a given position (specified by **index**).| 47e41f4b71Sopenharmony_ci| Read| Use **get(index: number)** to obtain the element at a given position (specified by **index**).| 48e41f4b71Sopenharmony_ci| Read| Use **getLastElement()** to obtain the last element in this container.| 49e41f4b71Sopenharmony_ci| Read| Use **getIndexOf(element: T)** to obtain the index of the first occurrence of the specified element.| 50e41f4b71Sopenharmony_ci| Read| Use **getLastIndexOf(element: T)** to obtain the index of the last occurrence of the specified element.| 51e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackFn: (value: T, index?: number, Vector?: Vector<T>) => void, thisArg?: Object)** to traverse the elements in this container.| 52e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 53e41f4b71Sopenharmony_ci| Update| Use **vec\[index]=xxx** to change the value at a given position (specified by **index**).| 54e41f4b71Sopenharmony_ci| Update| Use **set(index: number, element: T)** to replace an element at a given position (specified by **index**) with a given element.| 55e41f4b71Sopenharmony_ci| Update| Use **setLength(newSize: number)** to set the size of this container.| 56e41f4b71Sopenharmony_ci| Delete| Use **removeByIndex(index: number)** to remove the value at a given position (specified by **index**).| 57e41f4b71Sopenharmony_ci| Delete| Use **remove(element: T)** to remove the first occurrence of the specified element.| 58e41f4b71Sopenharmony_ci| Delete| Use **removeByRange(fromIndex: number, toIndex: number)** to remove all of the elements within a range.| 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci## List 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci[List](../reference/apis-arkts/js-apis-list.md) can be used to construct a singly linked list, which supports access only through the head node to the tail node. **List** uses generics and can be stored in a non-contiguous memory space. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciUnlike [LinkedList](../reference/apis-arkts/js-apis-linkedlist.md), which is a doubly linked list, **List** does not support insertion or removal at both ends. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ciYou are advised to use **List** for frequent insertion and removal operations. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**List** provides the following CRUD APIs. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci| Operation| Description| 72e41f4b71Sopenharmony_ci| --------- | ------ | 73e41f4b71Sopenharmony_ci| Create| Use **add(element: T)** to add an element at the end of this container.| 74e41f4b71Sopenharmony_ci| Create| Use **insert(element: T, index: number)** to insert an element at a given position (specified by **index**).| 75e41f4b71Sopenharmony_ci| Read| Use **list\[index]** to obtain the value at a given position (specified by **index**).| 76e41f4b71Sopenharmony_ci| Read| Use **get(index: number)** to obtain the element at a given position (specified by **index**).| 77e41f4b71Sopenharmony_ci| Read| Use **getFirst()** to obtain the first element in this container.| 78e41f4b71Sopenharmony_ci| Read| Use **getLast()** to obtain the last element in this container.| 79e41f4b71Sopenharmony_ci| Read| Use **getIndexOf(element: T)** to obtain the index of the first occurrence of the specified element.| 80e41f4b71Sopenharmony_ci| Read| Use **getLastIndexOf(element: T)** to obtain the index of the last occurrence of the specified element.| 81e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackfn: (value: T, index?: number, list?: List<T>)=> void, thisArg?: Object)** to traverse the elements in this container.| 82e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 83e41f4b71Sopenharmony_ci| Update| Use **list\[index] = xxx** to change the value at a given position (specified by **index**).| 84e41f4b71Sopenharmony_ci| Update| Use **set(index: number, element: T)** to replace an element at a given position (specified by **index**) with a given element.| 85e41f4b71Sopenharmony_ci| Update| Use **replaceAllElements(callbackFn:(value: T,index?: number,list?: List<T>)=>T,thisArg?: Object)** to replace all elements in this container with new elements.| 86e41f4b71Sopenharmony_ci| Delete| Use **removeByIndex(index: number)** to remove the value at a given position (specified by **index**).| 87e41f4b71Sopenharmony_ci| Delete| Use **remove(element: T)** to remove the first occurrence of the specified element.| 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci## LinkedList 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci[LinkedList](../reference/apis-arkts/js-apis-linkedlist.md) can be used to construct a doubly linked list, which can be traversed at both ends. **LinkedList** uses generics and can be stored in a non-contiguous memory space. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciUnlike [List](../reference/apis-arkts/js-apis-list.md), which is a singly linked list, **LinkedList** supports insertion and removal at both ends. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**LinkedList** is more efficient in data insertion than [ArrayList](../reference/apis-arkts/js-apis-arraylist.md), but less efficient in data access. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ciYou are advised to use **LinkedList** for frequent insertion and removal operations. 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci**LinkedList** provides the following CRUD APIs. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci| Operation| Description| 103e41f4b71Sopenharmony_ci| ---------- | ------ | 104e41f4b71Sopenharmony_ci| Create| Use **add(element: T)** to add an element at the end of this container.| 105e41f4b71Sopenharmony_ci| Create| Use **insert(index: number, element: T)** to insert an element at a given position (specified by **index**).| 106e41f4b71Sopenharmony_ci| Read| Use **list\[index]** to obtain the value at a given position (specified by **index**).| 107e41f4b71Sopenharmony_ci| Read| Use **get(index: number)** to obtain the element at a given position (specified by **index**).| 108e41f4b71Sopenharmony_ci| Read| Use **getFirst()** to obtain the first element in this container.| 109e41f4b71Sopenharmony_ci| Read| Use **getLast()** to obtain the last element in this container.| 110e41f4b71Sopenharmony_ci| Read| Use **getIndexOf(element: T)** to obtain the index of the first occurrence of the specified element.| 111e41f4b71Sopenharmony_ci| Read| Use **getLastIndexOf(element: T)** to obtain the index of the last occurrence of the specified element.| 112e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackFn: (value: T, index?: number, list?: LinkedList<T>) => void, thisArg?: Object)** to traverse the elements in this container.| 113e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 114e41f4b71Sopenharmony_ci| Update| Use **list\[index]=xxx** to change the value at a given position (specified by **index**).| 115e41f4b71Sopenharmony_ci| Update| Use **set(index: number, element: T)** to replace an element at a given position (specified by **index**) with a given element.| 116e41f4b71Sopenharmony_ci| Delete| Use **removeByIndex(index: number)** to remove the value at a given position (specified by **index**).| 117e41f4b71Sopenharmony_ci| Delete| Use **remove(element: T)** to remove the first occurrence of the specified element.| 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci## Deque 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci[Deque](../reference/apis-arkts/js-apis-deque.md) can be used to construct a double-ended queue (deque) that follows the principles of First In First Out (FIFO) and Last In First Out (LIFO). It allows insertion and removal of elements at both ends. 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci**Deque** uses generics and must be stored in a contiguous memory space. Its initial capacity is 8, and it has capacity doubled in each dynamic expansion. The bottom layer of **Deque** is implemented by cyclic queues, delivering a high efficiency in enqueuing and dequeuing. 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ciDifferent from **Deque**, [Queue](../reference/apis-arkts/js-apis-queue.md) follows the principle of FIFO only and allows element removal at the front and insertion at the rear. 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ciUnlike [Vector](../reference/apis-arkts/js-apis-vector.md), **Deque** does not support insertion and deletion of elements in between. When compared with **Vector**, **Deque** is more efficient in inserting and removing header elements, but less efficient in accessing elements. 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ciYou are advised to use **Deque** when you need to frequently insert or remove elements at both the ends of a container. 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**Deque** provides the following CRUD APIs. 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci| Operation| Description| 135e41f4b71Sopenharmony_ci| ---------- | ------ | 136e41f4b71Sopenharmony_ci| Create| Use **insertFront(element: T)** to insert an element at the front of this container.| 137e41f4b71Sopenharmony_ci| Create| Use **insertEnd(element: T)** to insert an element at the end of this container.| 138e41f4b71Sopenharmony_ci| Read| Use **getFirst()** to obtain the value of the first element in this container, without removing it from the container.| 139e41f4b71Sopenharmony_ci| Read| Use **getLast()** to obtain the value of the last element in this container, without removing it from the container.| 140e41f4b71Sopenharmony_ci| Read| Use **popFirst()** to obtain the value of the first element in this container and remove it from the container.| 141e41f4b71Sopenharmony_ci| Read| Use **popLast()** to obtain the value of the last element in this container and remove it from the container.| 142e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>) => void, thisArg?: Object)** to traverse the elements in this container.| 143e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 144e41f4b71Sopenharmony_ci| Update| Use **forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>)=> void, thisArg?: Object)** to modify an element in this container.| 145e41f4b71Sopenharmony_ci| Delete| Use **popFirst()** to remove the first element from this container.| 146e41f4b71Sopenharmony_ci| Delete| Use **popLast()** to remove the last element from this container.| 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci## Queue 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci[Queue](../reference/apis-arkts/js-apis-queue.md) can be used to construct a queue that follows the FIFO principle. 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci**Queue** uses generics and must be stored in a contiguous memory space. Its initial capacity is 8, and it has capacity doubled in each dynamic expansion. 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ciThe bottom layer of **Queue** is implemented by cyclic queues, delivering a high efficiency in enqueuing and dequeuing. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ciUnlike [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. 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ciYou are advised to use **Queue** in FIFO scenarios. 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**Queue** provides the following CRUD APIs. 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci| Operation| Description| 164e41f4b71Sopenharmony_ci| ---------- | ------ | 165e41f4b71Sopenharmony_ci| Create| Use **add(element: T)** to add an element at the end of this container.| 166e41f4b71Sopenharmony_ci| Read| Use **getFirst()** to obtain the value of the first element in this container, without removing it from the container.| 167e41f4b71Sopenharmony_ci| Read| Use **pop()** to obtain the value of the first element in this container and remove it from the container.| 168e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackFn: (value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object)** to traverse the elements in this container.| 169e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 170e41f4b71Sopenharmony_ci| Update| Use **forEach(callbackFn:(value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object)** to modify an element in this container.| 171e41f4b71Sopenharmony_ci| Delete| Use **pop()** to remove the first element from this container.| 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci## Stack 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci[Stack](../reference/apis-arkts/js-apis-stack.md) can be used to construct a stack that follows the Last Out First In (LOFI) principle. 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci**Stack** uses generics and must be stored in a contiguous memory space. Its initial capacity is 8, and it increases capacity 1.5-fold in each dynamic expansion. The bottom layer of **Stack** is implemented based on arrays. It supports data insertion and removal at one end. 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ciUnlike [Queue](../reference/apis-arkts/js-apis-queue.md), which supports insertion at one end and removal at the other, **Stack** supports insertion and removal at the same end. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ciYou are advised to use **Stack** in LOFI scenarios. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci**Stack** provides the following CRUD APIs. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci| Operation| Description| 187e41f4b71Sopenharmony_ci| ---------- | ------ | 188e41f4b71Sopenharmony_ci| Create| Use **push(item: T)** to add an element at the top of this container.| 189e41f4b71Sopenharmony_ci| Read| Use **peek()** to obtain the value of the top element in this container, without removing it from the container.| 190e41f4b71Sopenharmony_ci| Read| Use **pop()** to obtain the value of the top element in this container and remove it from the container.| 191e41f4b71Sopenharmony_ci| Read| Use **forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object)** to traverse the elements in this container.| 192e41f4b71Sopenharmony_ci| Read| Use **\[Symbol.iterator]():IterableIterator<T>** for data access.| 193e41f4b71Sopenharmony_ci| Read| Use **locate(element: T)** to obtain the index of the first occurrence of the specified element.| 194e41f4b71Sopenharmony_ci| Update| Use **forEach(callbackFn:(value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object)** to modify an element in this container.| 195e41f4b71Sopenharmony_ci| Delete| Use **pop()** to remove the top element from this container.| 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci## Use of Linear Containers 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ciRefer to the code snippet below to add, access, and modify elements in **ArrayList**, **Deque**, **Stack**, and **List**. 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci```ts 204e41f4b71Sopenharmony_ci// ArrayList 205e41f4b71Sopenharmony_ciimport { ArrayList } from '@kit.ArkTS'; // Import the ArrayList module. 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_cilet arrayList1: ArrayList<string> = new ArrayList(); 208e41f4b71Sopenharmony_ciarrayList1.add('a'); 209e41f4b71Sopenharmony_cilet arrayList2: ArrayList<number> = new ArrayList(); 210e41f4b71Sopenharmony_ciarrayList2.add(1); // Add an element. 211e41f4b71Sopenharmony_ciconsole.info(`result: ${arrayList2[0]}`); // Access an element. 212e41f4b71Sopenharmony_ciarrayList1[0] = 'one'; // Modify an element. 213e41f4b71Sopenharmony_ciconsole.info(`result: ${arrayList1[0]}`); 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci// Deque 216e41f4b71Sopenharmony_ciimport { Deque } from '@kit.ArkTS'; // Import the Deque module. 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_cilet deque1: Deque<string> = new Deque(); 219e41f4b71Sopenharmony_cideque1.insertFront('a'); 220e41f4b71Sopenharmony_cilet deque2: Deque<number> = new Deque(); 221e41f4b71Sopenharmony_cideque2.insertFront(1); // Add an element. 222e41f4b71Sopenharmony_ciconsole.info(`result: ${deque1[0]}`); // Access an element. 223e41f4b71Sopenharmony_cideque1[0] = 'one'; // Modify an element. 224e41f4b71Sopenharmony_ciconsole.info(`result: ${deque2[0]}`); 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci// Stack 227e41f4b71Sopenharmony_ciimport { Stack } from '@kit.ArkTS'; // Import the Stack module. 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_cilet stack1: Stack<string> = new Stack(); 230e41f4b71Sopenharmony_cistack1.push('a'); 231e41f4b71Sopenharmony_cilet stack2: Stack<number> = new Stack(); 232e41f4b71Sopenharmony_cistack2.push(1); // Add an element. 233e41f4b71Sopenharmony_ciconsole.info(`result: ${stack1[0]}`); // Access an element. 234e41f4b71Sopenharmony_cistack2.pop(); // Remove the top element from this container. 235e41f4b71Sopenharmony_ciconsole.info(`result: ${stack2.length}`); 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci// List 238e41f4b71Sopenharmony_ciimport { List } from '@kit.ArkTS'; // Import the List module. 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_cilet list1: List<string> = new List(); 241e41f4b71Sopenharmony_cilist1.add('a'); 242e41f4b71Sopenharmony_cilet list2: List<number> = new List(); 243e41f4b71Sopenharmony_cilist2.add(1); 244e41f4b71Sopenharmony_cilet list3: List<Array<number>> = new List(); 245e41f4b71Sopenharmony_cilet b2 = [1, 2, 3]; 246e41f4b71Sopenharmony_cilist3.add(b2); // Add an element. 247e41f4b71Sopenharmony_ciconsole.info(`result: ${list1[0]}`); // Access an element. 248e41f4b71Sopenharmony_ciconsole.info(`result: ${list3.get(0)}`); // Access an element. 249e41f4b71Sopenharmony_ci``` 250