HarmonyOS linear container features and usage scenarios

Linear containers implement data structures that can be accessed sequentially. The bottom layer is mainly implemented through arrays, including ArrayList, Vector, List, LinkedList, Deque, Queue, and Stack.

Linear containers fully consider the speed of data access. Operations such as addition, deletion, modification, and query can be completed through a single bytecode instruction at runtime.

ArrayList

ArrayList is a dynamic array that can be used to construct global array objects. ArrayList is recommended when you need to frequently read elements in a collection.

According to the generic definition, ArrayList requires the storage location to be a continuous memory space with an initial capacity of 10 and supports dynamic expansion. Each expansion size is 1.5 times the original capacity.

Commonly used APIs for adding, deleting, modifying, and querying operations on ArrayList are as follows:

operate

describe

Add elements

Add one element to the end of the array each time through the add(element: T) function.

Insert an element at the specified position through insert(element: T, index: number).

access element

Get the value corresponding to the specified index through arr[index], and obtain it through instructions to ensure access speed.

Access the elements of the entire ArrayList container through forEach(callbackFn: (value: T, index?: number, arrlist?: ArrayList<T>) => void, thisArg?: Object): void.

Data access via [Symbol.iterator]():IterableIterator<T> iterator.

Modify elements

Modify the value corresponding to the specified index position through arr[index] = xxx.

Delete element

Delete the first matching element through remove(element: T).

Delete elements within the specified range through removeByRange(fromIndex: number, toIndex:number).

Vector

Vector refers to a continuous storage structure that can be used to construct global array objects. Based on the generic definition, Vector requires the storage location to be a continuous memory space with an initial capacity of 10 and supports dynamic expansion, with each expansion being twice the original capacity.

Vector and ArrayList are similar in that they are both implemented based on arrays, but Vector provides more interfaces for operating arrays. In addition to supporting operator access, Vector also adds a get/set interface to provide a more complete verification and fault-tolerance mechanism to meet the needs of users in different scenarios.

Starting from API version 9, this interface is no longer maintained. It is recommended to use ArrayList .

Common APIs used by Vector to perform add, delete, modify, and search operations are as follows:

operate

describe

Add elements

Add one element to the end of the array each time through the add(element: T) function.

Insert an element at the specified position through insert(element: T, index: number).

access element

Get the value corresponding to the specified index through vec[index], and obtain it through instructions to ensure access speed.

Get the element corresponding to the specified index position through get(index: number).

Get the last element through getLastElement().

Get the position of the first matching element through getIndexOf(element:T).

Get the position of the last matching element through getLastIndexOf(element:T).

Access the elements of the entire Vector through forEach(callbackFn: (value: T, index?: number, Vector?: Vector<T>) => void, thisArg?: Object).

Data access via [Symbol.iterator]():IterableIterator<T> iterator.

Modify elements

Modify the value corresponding to the specified index position through vec[index]=xxx.

Modify the element value at the specified index position to element through set(index:number,element:T).

Set the length of Vector through setLength(newSize:number).

Delete element

Delete the value corresponding to the index position through removeByIndex(index:number).

Delete the first matching element through remove(element:T).

Delete elements within the specified range through removeByRange(fromIndex:number,toIndex:number).

List

List can be used to construct a one-way linked list object, that is, it can only be accessed from the head node to the tail node. According to the generic definition, the storage location of List in memory can be discontinuous.

Compared with List and LinkedList , LinkedList is a doubly linked list, which can quickly add and delete items at the beginning and end, while List is a one-way linked list and cannot be operated in both directions.

When frequent insertion and deletion are required, it is recommended to use List for efficient operations.

Stored elements can be modified through interfaces such as get/set. Common APIs for adding, deleting, modifying, and querying operations on List are as follows:

operate

describe

Add elements

Add one element to the end of the array each time through the add(element: T) function.

Insert an element at the specified position through insert(element: T, index: number).

access element

Get the value corresponding to the specified index through list[index], and obtain it through instructions to ensure access speed.

Get the element corresponding to the specified index position through get(index: number).

Get the first element through getFirst().

Get the last element through getLast().

Get the position of the first matching element through getIndexOf(element: T).

Get the position of the last matching element through getLastIndexOf(element: T).

通过forEach(callbackfn: (value:T, index?: number, list?: List<T>)=> void,thisArg?: Object)访问整个List的元素。

通过[Symbol.iterator]():IterableIterator<T>迭代器进行数据访问。

修改元素

通过list[index] = xxx修改指定index位置对应的value值。

通过set(index:number, element: T)修改指定index位置的元素值为element。

通过replaceAllElements(callbackFn:(value: T,index?: number,list?: List<T>)=>T,thisArg?: Object)对List内元素进行替换操作。

删除元素

通过removeByIndex(index:number)删除index位置对应的value值。

通过remove(element:T)删除第一个匹配到的元素。

LinkedList

LinkedList可用来构造一个双向链表对象,可以在某一节点向前或者向后遍历List。LinkedList依据泛型定义,在内存中的存储位置可以是不连续的。

LinkedList和List相比,LinkedList是双向链表,可以快速地在头尾进行增删,而List是单向链表,无法双向操作。

LinkedList和ArrayList相比,插入数据效率LinkedList优于ArrayList,而查询效率ArrayList优于LinkedList。

当需要频繁的插入删除时,推荐使用LinkedList高效操作。

可以通过get/set等接口对存储的元素进行修改,LinkedList进行增、删、改、查操作的常用API如下:

操作

描述

增加元素

通过add(element: T)函数每次在数组尾部增加一个元素。

通过insert(index: number, element: T)在指定位置插入一个元素。

访问元素

通过list[index]获取指定index对应的value值,通过指令获取保证访问速度。

通过get(index: number)获取指定index位置对应的元素。

通过getFirst()获取第一个元素。

通过getLast()获取最后一个元素。

通过getIndexOf(element: T)获取第一个匹配到元素的位置。

通过getLastIndexOf(element: T)获取最后一个匹配到元素的位置。

通过forEach(callbackFn: (value: T, index?: number, list?: LinkedList<T>) => void, thisArg?: Object)访问整个LinkedList的元素。

通过[Symbol.iterator]():IterableIterator<T>迭代器进行数据访问。

修改元素

通过list[index]=xxx修改指定index位置对应的value值。

通过set(index: number,element: T)修改指定index位置的元素值为element。

删除元素

通过removeByIndex(index: number)删除index位置对应的value值。

通过remove(element: T)删除第一个匹配到的元素。

Deque

Deque可用来构造双端队列对象,存储元素遵循先进先出以及先进后出的规则,双端队列可以分别从队头或者队尾进行访问。

Deque依据泛型定义,要求存储位置是一片连续的内存空间,其初始容量大小为8,并支持动态扩容,每次扩容大小为原始容量的2倍。Deque底层采用循环队列实现,入队及出队操作效率都比较高。

Deque和Queue相比,Queue的特点是先进先出,只能在头部删除元素,尾部增加元素。

Deque和Vector相比,它们都支持在两端增删元素,但Deque不能进行中间插入的操作。对头部元素的插入删除效率高于Vector,而Vector访问元素的效率高于Deque。

需要频繁在集合两端进行增删元素的操作时,推荐使用Deque。

Deque进行增、删、改、查操作的常用API如下:

操作

描述

增加元素

通过insertFront(element: T)函数每次在队头增加一个元素。

增加元素

通过insertEnd(element: T)函数每次在队尾增加一个元素。

访问元素

通过getFirst()获取队首元素的value值,但是不进行出队操作。

通过getLast()获取队尾元素的value值,但是不进行出队操作。

通过popFirst()获取队首元素的value值,并进行出队操作。

通过popLast()获取队尾元素的value值,并进行出队操作。

通过forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>) => void, thisArg?: Object)访问整个Deque的元素。

通过[Symbol.iterator]():IterableIterator<T>迭代器进行数据访问。

修改元素

通过forEach(callbackFn:(value: T, index?: number, deque?: Deque<T>)=> void, thisArg?: Object)对队列进行修改操作。

删除元素

通过popFirst()对队首元素进行出队操作并删除。

通过popLast()对队尾元素进行出队操作并删除。

Queue

Queue可用来构造队列对象,存储元素遵循先进先出的规则。

Queue依据泛型定义,要求存储位置是一片连续的内存空间,初始容量大小为8,并支持动态扩容,每次扩容大小为原始容量的2倍。

Queue底层采用循环队列实现,入队及出队操作效率都比较高。

Queue和Deque相比,Queue只能在一端删除一端增加,Deque可以两端增删。

一般符合先进先出的场景可以使用Queue。

Queue进行增、删、改、查操作的常用API如下:

操作

描述

增加元素

通过add(element: T)函数每次在队尾增加一个元素。

访问元素

通过getFirst()获取队首元素的value值,但是不进行出队操作。

通过pop()获取队首元素的value值,并进行出队操作。

通过forEach(callbackFn: (value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object)访问整个Queue的元素。

通过[Symbol.iterator]():IterableIterator<T>迭代器进行数据访问。

修改元素

通过forEach(callbackFn:(value: T, index?: number, queue?: Queue<T>) => void,thisArg?: Object)对队列进行修改操作。

删除元素

通过pop()对队首进行出队操作并删除。

Stack

Stack可用来构造栈对象,存储元素遵循先进后出的规则。

Based on the generic definition, Stack requires the storage location to be a continuous memory space with an initial capacity of 8 and supports dynamic expansion. Each expansion is 1.5 times the original capacity. The bottom layer of Stack is implemented based on arrays. Pushing and popping out of the stack are operated from one end of the array.

Compared with Queue , Queue is based on a circular queue and can only be deleted at one end and inserted at the other end, while Stack operates at one end.

Generally, Stack can be used in scenarios that are first in, last out.

Stack’s common APIs for adding, deleting, modifying, and querying operations are as follows:

operate

describe

Add elements

Add one element to the top of the stack each time through the push(item: T) function.

access element

Obtain the value of the top element of the stack through peek(), but do not pop the stack.

Get the value at the top of the stack through pop() and pop it out of the stack.

Access the elements of the entire Stack through forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object).

Data access via [Symbol.iterator]():IterableIterator<T> iterator.

Get the position corresponding to the element through locate(element: T).

Modify elements

Modify elements in the stack through forEach(callbackFn:(value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object).

Delete element

Use pop() to pop the top of the stack and delete it.

Use of linear containers

Here are examples of the use of commonly used linear containers ArrayList, Vector, Deque, Stack, and List, including operations such as importing modules, adding elements, accessing elements, and modifying them. Sample code looks like this:

// ArrayList
import ArrayList from '@ohos.util.ArrayList'; // 导入ArrayList模块

let arrayList = new ArrayList();
arrayList.add('a');
arrayList.add(1); // 增加元素
console.info(`result: ${arrayList[0]}`); // 访问元素
arrayList[0] = 'one'; // 修改元素
console.info(`result: ${arrayList[0]}`);

// Vector
import Vector from '@ohos.util.Vector'; // 导入Vector模块

let vector = new Vector();
vector.add('a');
let b1 = [1, 2, 3];
vector.add(b1);
vector.add(false); // 增加元素
console.info(`result: ${vector[0]}`); // 访问元素
console.info(`result: ${vector.getFirstElement()}`); // 访问元素

// Deque
import Deque from '@ohos.util.Deque'; // 导入Deque模块

let deque = new Deque;
deque.insertFront('a');
deque.insertFront(1); // 增加元素
console.info(`result: ${deque[0]}`); // 访问元素
deque[0] = 'one'; // 修改元素
console.info(`result: ${deque[0]}`);

// Stack
import Stack from '@ohos.util.Stack'; // 导入Stack模块 

let stack = new Stack();
stack.push('a');
stack.push(1); // 增加元素
console.info(`result: ${stack[0]}`); // 访问元素
stack.pop(); // 弹出元素
console.info(`result: ${stack.length}`);

// List
import List from '@ohos.util.List'; // 导入List模块

let list = new List;
list.add('a');
list.add(1);
let b2 = [1, 2, 3];
list.add(b2); // 增加元素
console.info(`result: ${list[0]}`); // 访问元素
console.info(`result: ${list.get(0)}`); // 访问元素

Guess you like

Origin blog.csdn.net/HarmonyOSDev/article/details/133312266