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)類(lèi)型數(shù)據(jù)
1、int類(lèi)型數(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類(lèi)型數(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類(lèi)型數(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類(lèi)型數(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中實(shí)現(xiàn)多線程的幾種方式小結(jié)
在 Android 中,實(shí)現(xiàn)多線程編程主要有7種方式,每種方式都有其適用場(chǎng)景和優(yōu)缺點(diǎn),本文將詳細(xì)介紹一下具體實(shí)現(xiàn)方式,大家可以根據(jù)需要自行選擇2025-03-03
Android10自動(dòng)連接WiFi問(wèn)題的解決
這篇文章主要介紹了Android10自動(dòng)連接WiFi問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android studio報(bào): java.lang.ExceptionInInitializerError 錯(cuò)誤
本篇文章主要介紹了Android studio報(bào): java.lang.ExceptionInInitializerError錯(cuò)誤的解決方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位
項(xiàng)目需求是這樣的,首先定位我當(dāng)前的起始位置,并跟隨移動(dòng)不斷自動(dòng)定位我的當(dāng)前位置,下面通過(guò)本文給大家介紹android百度定位導(dǎo)航之基于百度地圖移動(dòng)獲取位置和自動(dòng)定位,需要的朋友參考下2016-01-01
Android幀率監(jiān)測(cè)與優(yōu)化技巧
Android 應(yīng)用的性能優(yōu)化是開(kāi)發(fā)過(guò)程中至關(guān)重要的一環(huán),而幀率(Frame Rate)是評(píng)估應(yīng)用性能的一個(gè)關(guān)鍵指標(biāo),在本文中,我們將深入探討如何監(jiān)測(cè) Android 應(yīng)用的幀率,以及如何通過(guò)代碼示例來(lái)優(yōu)化應(yīng)用的性能,需要的朋友可以參考下2023-10-10
Android實(shí)現(xiàn)圖片一邊的三角形邊框效果
這篇文章主要介紹了Android實(shí)現(xiàn)圖片一邊的三角形邊框效果,本文圖文并茂通過(guò)實(shí)例代碼講解的非常詳細(xì),需要的朋友可以參考下2019-12-12
android與asp.net服務(wù)端共享session的方法詳解
這篇文章主要給大家介紹了關(guān)于android與asp.net服務(wù)端如何共享session的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
Android中Service實(shí)時(shí)向Activity傳遞數(shù)據(jù)實(shí)例分析
這篇文章主要介紹了Android中Service實(shí)時(shí)向Activity傳遞數(shù)據(jù)的方法,實(shí)例分析了Service組件基于線程操作實(shí)現(xiàn)數(shù)值實(shí)時(shí)傳遞的相關(guān)技巧,需要的朋友可以參考下2015-09-09

