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