Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實(shí)現(xiàn)
List
具有一定長(zhǎng)度存在索引的對(duì)象集合(長(zhǎng)度為0不存在索引,長(zhǎng)度>0存在索引)
常見(jiàn)列表
1、定長(zhǎng)列表
默認(rèn)值null
例如:List<int> fixedLengthList = new List(2)、List<int> fixedLengthList = new List(8)
List<int> fixedLengthList = new List(2); for(int i=0;i<2;i++){ print("索引為${i}的值${fixedLengthList[i]}"); }
I/flutter ( 9251): 索引為0的值null
I/flutter ( 9251): 索引為1的值null
固定長(zhǎng)度不可修改
List<int> fixedLengthList = new List(2); //改變固定數(shù)組長(zhǎng)度 fixedLengthList.length=30;
Unsupported operation: Cannot change the length of a fixed-length list
大概意思:無(wú)法更改固定長(zhǎng)度數(shù)組的長(zhǎng)度
List<int> fixedLengthList = new List(2); ///執(zhí)行添加數(shù)據(jù)操作 fixedLengthList.add(0); fixedLengthList.add(1);
List<int> fixedLengthList = new List(2); fixedLengthList[0]=1; fixedLengthList[1]=2; ///添加數(shù)據(jù) fixedLengthList.addAll([3,4]);
Unsupported operation: Cannot add to a fixed-length list
大概以上: 不能添加數(shù)據(jù)到固定長(zhǎng)度數(shù)組
List<int> fixedLengthList = new List(2); //執(zhí)行插入數(shù)據(jù) fixedLengthList.insert(0, 0);
Unsupported operation: Cannot add to a fixed-length list
大概意思: 不能添加數(shù)據(jù)到固定長(zhǎng)度數(shù)組
List<int> fixedLengthList = new List(2); ///執(zhí)行刪除操作 fixedLengthList.removeLast();
List<int> fixedLengthList = new List(2); ///執(zhí)行刪除操作 fixedLengthList.removeAt(0);
List<int> fixedLengthList = new List(2); fixedLengthList[0]=1; fixedLengthList[1]=2; ///刪除包含索引0和1范圍內(nèi)數(shù)據(jù) fixedLengthList.removeRange(0, 1);
List<int> fixedLengthList = new List(2); fixedLengthList[0]=1; fixedLengthList[1]=2; ///刪除索引0-1,然后在進(jìn)行替換刪除索引值 fixedLengthList.replaceRange(0, 1, [3,4]);
Unsupported operation: Cannot remove from a fixed-length list
大概意思:不能刪除固定長(zhǎng)度數(shù)組數(shù)據(jù)
List<int> fixedLengthList = new List(2); ///執(zhí)行清除數(shù)據(jù)操作 fixedLengthList.clear();
Unsupported operation: Cannot clear a fixed-length list
大概意思:不能清理固定長(zhǎng)度數(shù)組數(shù)據(jù)
可排序、替換、截取
List<int> fixedLengthList = new List(2); fixedLengthList[0]=1; fixedLengthList[1]=2; ///執(zhí)行截取指定范圍的數(shù)組 fixedLengthList.sublist(0); ///排序 fixedLengthList..sort((a, b) => a.compareTo(b)); /// fixedLengthList.setRange(0, 1, [3,4],0); ///索引0-1范圍的值不包括1,修改成3 fixedLengthList.fillRange(0, 1,3);
2、可增長(zhǎng)列表
可改變數(shù)組長(zhǎng)度、 可執(zhí)行添加、刪除、可排序、可替換、可截取
.可增長(zhǎng)列表[]
保留了內(nèi)部緩沖區(qū)
.緩沖區(qū)可增長(zhǎng)
.添加數(shù)據(jù)操作在固定時(shí)間內(nèi)執(zhí)行 (設(shè)置固定長(zhǎng)度會(huì)花費(fèi)與新長(zhǎng)度成比例的時(shí)間,修改容量,添加操作將需要立即增加緩沖區(qū)容量)
.列表是可以迭代的
.在執(zhí)行列表操作時(shí),例如在調(diào)用forEach或sort期間,通常不允許修改列表的長(zhǎng)度(添加或刪除元素)
.通過(guò)直接迭代列表或通過(guò)迭代由列表支持的Iterable更改列表的長(zhǎng)度,可以中斷迭代
List<int> fixedLengthList = []; //改變數(shù)組長(zhǎng)度 fixedLengthList.length=2; ///執(zhí)行添加數(shù)據(jù)操作 fixedLengthList.add(0); fixedLengthList.add(1); fixedLengthList[0]=1; fixedLengthList[1]=2; ///添加數(shù)據(jù) fixedLengthList.addAll([3,4]); //執(zhí)行插入數(shù)據(jù) fixedLengthList.insert(0, 0); ///執(zhí)行刪除操作 fixedLengthList.removeLast(); ///執(zhí)行刪除操作 fixedLengthList.removeAt(0); ///刪除包含索引0和1范圍內(nèi)數(shù)據(jù) fixedLengthList.removeRange(0, 1); ///刪除索引0-1,然后在進(jìn)行替換刪除索引值 fixedLengthList.replaceRange(0, 1, [3,4]); fixedLengthList.sublist(0); fixedLengthList..sort((a, b) => a.compareTo(b)); fixedLengthList.setRange(0, 1, [3,4],0); fixedLengthList.fillRange(0, 1,3); ///執(zhí)行清除數(shù)據(jù)操作 fixedLengthList.clear();
3、contains 過(guò)濾重復(fù) 添加(int、double、bool、String)類型數(shù)據(jù)
1、int類型數(shù)組中插入重復(fù)數(shù)據(jù)
List<int> listInts = []; void addIntData(int addValue){ bool isContainer=listInts.contains(addValue); if(!isContainer){ listInts.add(addValue); } print("數(shù)組長(zhǎng)度${listInts.length}"); }
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
2、double類型數(shù)組中插入重復(fù)數(shù)據(jù)
List<double> listDouble = []; void addDoubleData(double addValue){ bool isContainer=listDouble.contains(addValue); if(!isContainer){ listDouble.add(addValue); } print("數(shù)組長(zhǎng)度${listDouble.length}"); }
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
3、String類型數(shù)組中插入重復(fù)數(shù)據(jù)
List<String> listStrings = []; void addStringData(String addValue){ bool isContainer=listStrings.contains(addValue); if(!isContainer){ listStrings.add(addValue); } print("數(shù)組長(zhǎng)度${listStrings.length}"); }
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
4、boolean類型數(shù)組插入重復(fù)數(shù)據(jù)
List<bool> listBool = []; void addBoolData(bool addValue){ bool isContainer=listBool.contains(addValue); if(!isContainer){ listBool.add(addValue); } print("數(shù)組長(zhǎng)度${listBool.length}"); }
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
I/flutter (28028): 數(shù)組長(zhǎng)度1
4、List對(duì)象去重
class A{ String a; int b; A(this.a, this.b); }
1、要添加的對(duì)象A的每個(gè)值和數(shù)組里面存在的每個(gè)對(duì)象的值做比較 (效率低、適合少量數(shù)據(jù)去重)
List<A> listAs = []; void addAData(A addValue){ int length=listAs.length; if(length==0){ listAs.add(addValue); }else { for (int i = 0; i < length; i++) { A a = listAs[i]; if (a.a != addValue.a && a.b != addValue.b) { listAs.add(addValue); } } } print("數(shù)組長(zhǎng)度${listAs.length}"); }
2、List配合Set去除重復(fù)對(duì)象
List<A> listAs = []; Set<A> setAs=new Set<A>(); void addASData(A addValue){ if(listAs.length==0) { listAs.add(addValue); setAs.addAll(listAs); }else{ listAs.add(addValue); } List<A> list=setAs.toList(); print("數(shù)組長(zhǎng)度${list.length}"); }
addASData(new A("a", 0));
I/flutter (10386): 數(shù)組長(zhǎng)度1
I/flutter (10386): 數(shù)組長(zhǎng)度1
I/flutter (10386): 數(shù)組長(zhǎng)度1
I/flutter (10386): 數(shù)組長(zhǎng)度1
I/flutter (10386): 數(shù)組長(zhǎng)度1
I/flutter (10386): 數(shù)組長(zhǎng)度1
參考:
list :https://api.dart.dev/stable/2.9.2/dart-core/List-class.html
Set:https://api.dart.dev/stable/2.9.2/dart-core/Set-class.html
到此這篇關(guān)于Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Flutter List 重復(fù)插入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android UI設(shè)計(jì)與開(kāi)發(fā)之ViewPager介紹和簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開(kāi)發(fā)之ViewPager介紹和簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android實(shí)現(xiàn)整理PackageManager獲取所有安裝程序信息
這篇文章主要介紹了Android實(shí)現(xiàn)整理PackageManager獲取所有安裝程序信息的方法,實(shí)例分析了Android使用PackageManager獲取安裝程序信息的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-01-01Android獲取SDcard目錄及創(chuàng)建文件夾的方法
今天小編就為大家分享一篇Android獲取SDcard目錄及創(chuàng)建文件夾的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android調(diào)用OpenCV2.4.10實(shí)現(xiàn)二維碼區(qū)域定位
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用OpenCV 2.4.10實(shí)現(xiàn)二維碼區(qū)域定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Android getSystemService用法實(shí)例總結(jié)
這篇文章主要介紹了Android getSystemService用法,結(jié)合實(shí)例形式總結(jié)分析了getSystemService獲取系統(tǒng)Service的相關(guān)使用方法與注意事項(xiàng),需要的朋友可以參考下2016-01-01Android Drawable必備知識(shí)小結(jié)
這篇文章主要為大家詳細(xì)了Android Drawable必備基礎(chǔ)知識(shí) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android中的Bmob移動(dòng)后端云服務(wù)器功能
這里介紹一個(gè)移動(dòng)后端云服務(wù)器平臺(tái)bmob,這不僅可以實(shí)現(xiàn)云數(shù)據(jù)庫(kù)儲(chǔ)存,還可以獲取手機(jī)驗(yàn)證等,隨時(shí)隨地都很輕松,下面寫一個(gè)小demo,實(shí)現(xiàn)一個(gè)登陸注冊(cè)功能,認(rèn)識(shí)增刪查改2018-01-01