W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
List底層通過(guò)單向鏈表實(shí)現(xiàn),每個(gè)節(jié)點(diǎn)有一個(gè)指向后一個(gè)元素的引用。當(dāng)需要查詢?cè)貢r(shí),必須從頭遍歷,插入、刪除效率高,查詢效率低。List允許元素為null。
List和LinkedList相比,LinkedList是雙向鏈表,可以快速地在頭尾進(jìn)行增刪,而List是單向鏈表,無(wú)法雙向操作。
推薦使用場(chǎng)景: 當(dāng)需要頻繁的插入刪除時(shí),推薦使用List高效操作。
文檔中存在泛型的使用,涉及以下泛型標(biāo)記符:
本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。
constructor()
List的構(gòu)造函數(shù)。
系統(tǒng)能力: SystemCapability.Utils.Lang
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200012 | The List's constructor cannot be directly invoked. |
示例:
- let list = new List();
add(element: T): boolean
在List尾部插入元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
element | T | 是 | 添加進(jìn)去的元素。 |
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 插入成功返回true,否則返回false。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The add method cannot be bound. |
示例:
- let list = new List();
- let result1 = list.add("a");
- let result2 = list.add(1);
- let b = [1, 2, 3];
- let result3 = list.add(b);
- let c = {name : "Dylon", age : "13"};
- let result4 = list.add(c);
- let result5 = list.add(false);
insert(element: T, index: number): void
在長(zhǎng)度范圍內(nèi)任意位置插入指定元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
element | T | 是 | 插入元素。 |
index | number | 是 | 插入的位置索引。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The insert method cannot be bound. |
10200001 | The value of index is out of range. |
示例:
- let list = new List();
- list.insert("A", 0);
- list.insert(0, 1);
- list.insert(true, 2);
has(element: T): boolean
判斷此List中是否含有該指定元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 包含指定元素返回true,否則返回false。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The has method cannot be bound. |
示例:
- let list = new List();
- let result = list.has("squirrel");
- list.add("squirrel");
- let result1 = list.has("squirrel");
get(index: number): T
根據(jù)下標(biāo)獲取List中的元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
index | number | 是 | 要查找的下標(biāo)。 |
返回值:
類型 | 說(shuō)明 |
---|---|
T | 根據(jù)下標(biāo)查找到的元素。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The get method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(2);
- list.add(1);
- list.add(2);
- list.add(4);
- let result = list.get(2);
getLastIndexOf(element: T): number
查找指定元素最后一次出現(xiàn)的下標(biāo)值,查找失敗返回-1。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
類型 | 說(shuō)明 |
---|---|
number | 返回指定元素最后一次出現(xiàn)的下標(biāo)值,沒(méi)有找到返回-1。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The getLastIndexOf method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(2);
- list.add(1);
- list.add(2);
- list.add(4);
- let result = list.getLastIndexOf(2);
getIndexOf(element: T): number
查找指定元素第一次出現(xiàn)的下標(biāo)值,查找失敗返回-1。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
類型 | 說(shuō)明 |
---|---|
number | 返回第一次找到指定元素的下標(biāo),沒(méi)有找到返回-1。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The getIndexOf method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(2);
- list.add(1);
- list.add(2);
- list.add(4);
- list.getIndexOf(2);
- let result = list.getIndexOf(2);
equal(obj: Object): boolean
比較指定對(duì)象與此List是否相等。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
obj | Object | 是 | 用來(lái)比較的對(duì)象。 |
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 如果對(duì)象與此列表相同返回true,否則返回false。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The equal method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(2);
- let obj1 = new List();
- obj1.add(2);
- obj1.add(4);
- obj1.add(5);
- list.equal(obj1);
- let obj2 = {name : "Dylon", age : "13"};
- let result = list.equal(obj2);
removeByIndex(index: number): T
根據(jù)元素的下標(biāo)值查找元素,返回元素后將其刪除。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
index | number | 是 | 指定元素的下標(biāo)值。 |
返回值:
類型 | 說(shuō)明 |
---|---|
T | 返回刪除的元素。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The removeByIndex method cannot be bound. |
10200001 | The value of index is out of range. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(2);
- list.add(4);
- let result = list.removeByIndex(2);
remove(element: T): boolean
刪除查找到的第一個(gè)指定的元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
element | T | 是 | 指定元素。 |
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 刪除成功返回true,否則返回false。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The remove method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- let result = list.remove(2);
replaceAllElements(callbackFn: (value: T, index?: number, list?: List<T>) => T,
thisArg?: Object): void
用戶操作List中的元素,用操作后的元素替換原元素并返回操作后的元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
callbackFn | function | 是 | 回調(diào)函數(shù)。 |
thisArg | Object | 否 | callbackfn被調(diào)用時(shí)用作this值。 |
callbackfn的參數(shù)說(shuō)明:
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
value | T | 是 | 當(dāng)前遍歷到的元素。 |
index | number | 否 | 當(dāng)前遍歷到的下標(biāo)值。 |
list | List<T> | 否 | 當(dāng)前調(diào)用replaceAllElements方法的實(shí)例對(duì)象。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The replaceAllElements method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- list.replaceAllElements((value: number, index: number) => {
- return value = 2 * value;
- });
- list.replaceAllElements((value: number, index: number) => {
- return value = value - 2;
- });
forEach(callbackFn: (value: T, index?: number, List?: List<T>) => void,
thisArg?: Object): void
通過(guò)回調(diào)函數(shù)來(lái)遍歷List實(shí)例對(duì)象上的元素以及元素對(duì)應(yīng)的下標(biāo)。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
callbackFn | function | 是 | 回調(diào)函數(shù)。 |
thisArg | Object | 否 | callbackfn被調(diào)用時(shí)用作this值。 |
callbackfn的參數(shù)說(shuō)明:
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
value | T | 是 | 當(dāng)前遍歷到的元素。 |
index | number | 否 | 當(dāng)前遍歷到的下標(biāo)值。 |
List | List<T> | 否 | 當(dāng)前調(diào)用forEach方法的實(shí)例對(duì)象。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The forEach method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- list.forEach((value, index) => {
- console.log("value:" + value, "index:" + index);
- });
sort(comparator: (firstValue: T, secondValue: T) => number): void
對(duì)List中的元素進(jìn)行一個(gè)排序操作。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
comparator | function | 是 | 回調(diào)函數(shù)。 |
comparator的參數(shù)說(shuō)明:
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
firstValue | T | 是 | 前一項(xiàng)元素。 |
secondValue | T | 是 | 后一項(xiàng)元素。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The sort method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- list.sort((a: number, b: number) => a - b); // 結(jié)果為升序排列
- list.sort((a: number, b: number) => b - a); // 結(jié)果為降序排列
getSubList(fromIndex: number, toIndex: number): List<T>
根據(jù)下標(biāo)截取List中的一段元素,并返回這一段List實(shí)例,包括起始值但不包括終止值。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
fromIndex | number | 是 | 起始下標(biāo)。 |
toIndex | number | 是 | 終止下標(biāo)。 |
返回值:
類型 | 說(shuō)明 |
---|---|
List<T> | 返回List對(duì)象實(shí)例。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The getSubList method cannot be bound. |
10200001 | The value of fromIndex or toIndex is out of range. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- let result = list.getSubList(2, 4);
- let result1 = list.getSubList(4, 3);
- let result2 = list.getSubList(2, 6);
clear(): void
清除List中的所有元素,并把length置為0。
系統(tǒng)能力: SystemCapability.Utils.Lang
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The clear method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- list.clear();
set(index: number, element: T): T
將此 List 中指定位置的元素替換為指定元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
參數(shù):
參數(shù)名 | 類型 | 必填 | 說(shuō)明 |
---|---|---|---|
index | number | 是 | 查找的下標(biāo)值。 |
element | T | 是 | 用來(lái)替換的元素。 |
返回值:
類型 | 說(shuō)明 |
---|---|
T | 返回替換后的元素 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The set method cannot be bound. |
10200001 | The value of index is out of range. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- list.set(2, "b");
convertToArray(): Array<T>
把當(dāng)前List實(shí)例轉(zhuǎn)換成數(shù)組,并返回轉(zhuǎn)換后的數(shù)組。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說(shuō)明 |
---|---|
Array<T> | 返回轉(zhuǎn)換后的數(shù)組。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The convertToArray method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- let result = list.convertToArray();
isEmpty(): boolean
判斷該List是否為空。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說(shuō)明 |
---|---|
boolean | 為空返回true,不為空返回false。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The isEmpty method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- let result = list.isEmpty();
getFirst(): T
獲取List實(shí)例中的第一個(gè)元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說(shuō)明 |
---|---|
T | 返回實(shí)例的第一個(gè)元素。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The getFirst method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- let result = list.getFirst();
getLast(): T
獲取List實(shí)例中的最后一個(gè)元素。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說(shuō)明 |
---|---|
T | 返回實(shí)例的最后一個(gè)元素。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The getLast method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- let result = list.getLast();
[Symbol.iterator](): IterableIterator<T>
返回一個(gè)迭代器,迭代器的每一項(xiàng)都是一個(gè) JavaScript 對(duì)象,并返回該對(duì)象。
系統(tǒng)能力: SystemCapability.Utils.Lang
返回值:
類型 | 說(shuō)明 |
---|---|
IterableIterator<T> | 返回一個(gè)迭代器。 |
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語(yǔ)言基礎(chǔ)類庫(kù)錯(cuò)誤碼。
錯(cuò)誤碼ID | 錯(cuò)誤信息 |
---|---|
10200011 | The Symbol.iterator method cannot be bound. |
示例:
- let list = new List();
- list.add(2);
- list.add(4);
- list.add(5);
- list.add(4);
- // 使用方法一:
- for (let item of list) {
- console.log("value: " + item);
- }
- // 使用方法二:
- let iter = list[Symbol.iterator]();
- let temp = iter.next().value;
- while(temp != undefined) {
- console.log("value: " + temp);
- temp = iter.next().value;
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: