以下內(nèi)容由個(gè)人博客 JsShou摘抄dart中的list屬性與方法
List([int length]) 創(chuàng)建一個(gè)空數(shù)組或者 length 長度的數(shù)組
List.filled(int length,E fill,{bool growable:false}) 創(chuàng)建一個(gè) list纬朝,每個(gè)元素共享相同的值,growable 表示是 list 長度是否可變,默認(rèn) false 固定長度
List a = new List.filled(10, 1);
print(a);//[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
- List.from(iterable elements,{bool growable:true})
List a = new List.from([1, "2", 3, 4]);
print(a);//[1, 2, 3, 4]
- List.generate(int length, E generator(int index) 創(chuàng)建一個(gè)元素昙篙,每個(gè)位置創(chuàng)建一個(gè)新對(duì)象
<!-- more -->
List a = new List.generate(10, (value) => value + 1);
print(a);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- List.unmodifiable(Iterable elements) 創(chuàng)建一個(gè)不可修改的 List
- first List 中第一個(gè)元素
- last List 中最后一個(gè)元素
- length List 的長度
- reversed 返回翻轉(zhuǎn) List 后的值枉长,不改變?cè)?List
- add() 向數(shù)組中添加一個(gè)元素
- addAll() 向數(shù)組中添加一個(gè)帶有 Iterable 接口的類
List a = new List.from([1, "2", 3, 4]);
a.addAll([5, 6]);
print(a);// [1, 2, 3, 4, 5, 6]
asMap() 返回一個(gè)不可修改的 Map,keys 為 List 順序
List a = new List.from([1, "2", 3, 4]);
Map b = a.asMap();
print(b);
print(a);
CONSOLE:
{0: 1, 1: 2, 2: 3, 3: 4}
[1, 2, 3, 4]
- cast() 如果 List 中所有類型滿足 R,正常工作并返回 List玉转,如果有一個(gè)不滿足连茧,將會(huì)拋出錯(cuò)誤
List a = new List.from([1,"2", 3, 4]);
print(a.cast<int>());//拋出錯(cuò)誤:type 'String' is not a subtype of type 'int' in type cas
- clear() 刪除所有元素
- fillRange(int start,int end,[E fillValue]) 在選擇的范圍插入一個(gè)新的元素
List a = [1, 2, 3, 4];
print(a.getRange(1, 3));//(2, 3)
print(a);//[1, 2, 3, 4]
- indexOf() 找到第一個(gè)滿足條件的下標(biāo)
List a = [1, 2, 3, 4];
print(a.indexOf(3));//2
- indexWhere(bool test(E element),[int start=0]) 迭代 list腹侣,返回第一個(gè)滿足條件的下標(biāo)
List a = [1, 2, 3, 4];
print(a.indexWhere((value) => value % 2 == 0));//1
- insert(int index,E element) 在 index 處插入一個(gè)元素
List a = [1, 2, 3, 4];
a.insert(2, 'jsshou');
print(a);//[1,2,jsshou,3,4]
- nsertAll(int index,iterable iterable) 在 index 處插入一個(gè)數(shù)組
List a = [1, 2, 3, 4];
a.insertAll(2, ['jsshou', 'good']);
print(a);//[1, 2, [jsshou, good], 3, 4]
- lastIndexOf(E element, [ int start ]) 類似 indexOf乏德,迭代從后往前
- lastIndexWhere(bool test(E element), [ int start ]) 類似 indexWhere撤奸,迭代從后往前
- remove(Object value) 刪除第一個(gè)匹配到的 value
List a = [1, 2, 3, 4, 3];
a.remove(3);
print(a);// [1,2,4,3]
- removeAt(int index) 刪除 index 處的數(shù)據(jù)
List a = [1, 2, 3, 4, 3];
a.removeAt(3);
print(a);//[1, 2, 3, 3]
- removeLast() 刪除最后一個(gè)數(shù)據(jù)
- removeRange(int start,int end) 刪除 start 到 end 處的數(shù)據(jù)
List a = [1, 2, 3, 4, 3];
a.removeRange(2, 3);
print(a);//[1,2,4,3]
- removeWhere() 迭代數(shù)據(jù)吠昭,刪除回調(diào)中返回 true 的數(shù)據(jù)
List a = [1, 2, 3, 4, 3];
a.removeWhere((value) => value % 2 == 0);
print(a);//[1,3,3]
- replaceRange(int start,int end,iterable replacement) 刪除 start 到 end 的數(shù)據(jù)插入 replacement
List a = [1, 2, 3, 4, 3];
a.replaceRange(2, 4, [10, 11, 12, 13]);
print(a);//[1, 2, 10, 11, 12, 13, 3]
- retainWhere(bool test(E element)) 刪除不滿足條件的數(shù)據(jù)
List a = [1, 2, 3, 4, 3];
a.retainWhere((value) => value % 2 == 0);
print(a);//[2, 4]
- setAll(int index,iterable iterable) 從 index 開始插入一個(gè) iterable 數(shù)據(jù)(對(duì)比 insertAll)
- 它與 insertAll 的區(qū)別是 insertAll 不會(huì)刪除原數(shù)組的值,setAll 會(huì)刪除插入處一個(gè)或多個(gè)值
List a = [1, 2, 3, 4, 5];
a.setAll(2, ['jsshou', 'good']);
print(a); //[1, 2, jsshou, good, 5]
- setRange(int start,int end,Iterable iterable,[int skipCount=0]) 從原數(shù)組 start 處開始到 end 結(jié)束替換一個(gè) iterable胧瓜,可選參數(shù) skipCount 表示從 iterable 第 skipCount 處開始復(fù)制
List<int> list1 = [1, 2, 3, 4];
List<int> list2 = [5, 6, 7, 8, 9];
list1.setRange(1, 3, list2, 2);
print(list1.join(', ')); // 1, 7, 8, 4
- shuffle([Random random]) 隨機(jī)排列 list
- sort(int compare(E a,E b)) 排序,類似 js 中的 sort
List<int> list = [1, 2, 3, 4, 9, 8, 7, 6];
list.sort((a, b) => a - b);
///時(shí)間排序
[{'createTime' : '2020-09-03 15:03'},{'createTime':'2020-09-03 18:00'}].sort((left,right) => right['createTime'].compareTo(left['createTime']));///結(jié)果自己試一試
print(list);//[1, 2, 3, 4, 6, 7, 8, 9]
- sublist(int start,[int end]) 提取 list 數(shù)據(jù)
List<int> list = [1, 2, 3, 4, 9, 8, 7, 6];
print(list.sublist(2, 5));
- contains(Object element) 比較原數(shù)組中是否包含 element
List<int> list = [1, 2, 3, 4];
List<int> list2 = [1, 2, 3, 4];
print(list.contains(1));//true
print(list2.contains(5));//false
- elementAt(int index) 返回 index 處的數(shù)據(jù)
List<int> list = [1, 2, 3, 4];
print(list.elementAt(2));//3
- every(bool test(E element)) list 每一項(xiàng)是否滿足條件
List<int> list = [1, 2, 3, 4];
List<int> list1 = [0, 2, 4, 6];
print(list.every((item) => item % 2 == 0));//false
print(list1.every((item) => item % 2 == 0));//true
- expand(Iterable f(E element)) 展開 list (類似 js 中的 flat)
var pairs = [[1, 2], [3, 4]];
var flattened = pairs.expand((pair) => pair).toList();
print(flattened); // => [1, 2, 3, 4];
var input = [1, 2, 3];
var duplicated = input.expand((i) => [i, i]).toList();
print(duplicated); // => [1, 1, 2, 2, 3, 3]
- firstWhere(bool test(E element),{E orElse()}) 迭代元素返回第一個(gè)滿足 test矢棚,如果沒有,返回 orElse 調(diào)用結(jié)果
var list = [1, 2, 2, 4];
var res = list.firstWhere((pair) => pair == 2);
print(res);//2
- fold(T initialValue,T combine(T previousValue,E element)) 把 initialValue 作為初始值府喳,迭代原數(shù)組蒲肋,返回新的 value,類似 js 中的 reduce钝满,eg:
var value = initialValue;
for (E element in this) {
value = combine(value, element);
}
return value;
- followedBy(iterable other) 將 other 數(shù)據(jù)添加到原數(shù)組兜粘,返回 Iterable 類型
List a = new List.from([1, 2, 3, 4]);
Iterable b = a.followedBy([1, 2, 3]);
print(b);//(1, 2, 3, 4, 1, 2, 3)
- forEach(void f(E element)) 迭代數(shù)組
List list = [1, 2, 3, 4];
list.forEach((item) {
print(item);
});
//1,2,3,4
- join([String separator='']) 把數(shù)組轉(zhuǎn)成字符串中間用 separator 隔開
var list = [1, 2, 2, 4];
var res = list.join('*');
print(res);//1*2*3*4
- lastWhere(bool test(E element),{E orElse})
var list = [1, 2, 2, 4];
var res = list.lastWhere((pair) => pair == 2);
print(res);//3
- map(T f(E e)) 迭代數(shù)組并返回一個(gè)新的數(shù)組抛寝,新數(shù)組長度跟原數(shù)組長度一樣
var list = [1, 2, 2, 4];
var res = list.map((pair) => pair * 2);
print(res);//(2, 4, 4, 8)
- noSuchMethod(Invocation invocation) 訪問不存在的方法或?qū)傩哉{(diào)用
- reduce(E combine(E value,E element)) value 為初始值為原始數(shù)組第一項(xiàng)耽装,后面是 combine 返回的項(xiàng)
List a = new List.from([1, 2, 3, 4]);
var b = a.reduce((a, b) {
print(a.toString() + ',' + b.toString());
return a + b;
});
print(b);
//1,2
//3,3
//6,4
//10
- singleWhere(bool test(E element),{E orElse()}) 檢查元素以查看是否 test(element)返回 true。如果只滿足一個(gè)元素 test唇礁,則返回該元素碎捺。如果找到多個(gè)匹配元素路鹰,則拋出 StateError。如果未找到匹配元素收厨,則返回結(jié)果 orElse晋柱。如果 orElse 省略,則默認(rèn)為拋出 StateError
- skip(int count) 返回原數(shù)組 count 后面的元素
List a = new List.from([1, 2, 3, 4]);
print(a.skip(2));//(3, 4) //返回Iterable類型
print(a);//[1, 2, 3, 4]
- skipWhile(bool test(E value))
List a = new List.from([1, 2, 3, 4]);
print(a.skipWhile((value) {
print(value);
if (value == 3) {
return false;
}
return true;
}));
print(a);
輸出:
1
2
3
(3, 4)
[1, 2, 3, 4]
- take(int count)
List a = new List.from([1, 2, 3, 4]);
print(a.take(2));//(1,2)
- takeWhile(bool test(E value)) 遍歷迭代器诵叁,直到找到 test 返回為 false 的元素雁竞,停止遍歷返回找到的 iterable
List a = new List.from([1, 2, 3, 4]);
print(a.takeWhile((value) => value != 2));
//(1)
- toList({bool growable:true}) 將 Iterable 類轉(zhuǎn)為 List
- toSet() 將 Iterable 類及其子類轉(zhuǎn)為 Set
- toString()
- where(bool test(E element)) 遍歷元素,返回所有滿足條件的元素,類似 js 中的 filter
List a = new List.from([1, 2, 3, 4]);
print(a.where((value) => value != 2));//(1拧额,3碑诉,4)
- whereType() 找到原 list 中帶有 T 類型的元素
List a = new List.from([1, "2", 3, 4]);
print(a.whereType<String>());//(2)
個(gè)人github博客轉(zhuǎn)載18487115313.github.io