Flutter List數(shù)組避免插入重復(fù)數(shù)據(jù)的實現(xiàn)
List
具有一定長度存在索引的對象集合(長度為0不存在索引,長度>0存在索引)
常見列表
1、定長列表
默認值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


固定長度不可修改
List<int> fixedLengthList = new List(2); //改變固定數(shù)組長度 fixedLengthList.length=30;
Unsupported operation: Cannot change the length of a fixed-length list
大概意思:無法更改固定長度數(shù)組的長度
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ù)到固定長度數(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ù)到固定長度數(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,然后在進行替換刪除索引值 fixedLengthList.replaceRange(0, 1, [3,4]);
Unsupported operation: Cannot remove from a fixed-length list
大概意思:不能刪除固定長度數(shù)組數(shù)據(jù)
List<int> fixedLengthList = new List(2); ///執(zhí)行清除數(shù)據(jù)操作 fixedLengthList.clear();
Unsupported operation: Cannot clear a fixed-length list
大概意思:不能清理固定長度數(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、可增長列表
可改變數(shù)組長度、 可執(zhí)行添加、刪除、可排序、可替換、可截取
.可增長列表[]保留了內(nèi)部緩沖區(qū)
.緩沖區(qū)可增長
.添加數(shù)據(jù)操作在固定時間內(nèi)執(zhí)行 (設(shè)置固定長度會花費與新長度成比例的時間,修改容量,添加操作將需要立即增加緩沖區(qū)容量)
.列表是可以迭代的
.在執(zhí)行列表操作時,例如在調(diào)用forEach或sort期間,通常不允許修改列表的長度(添加或刪除元素)
.通過直接迭代列表或通過迭代由列表支持的Iterable更改列表的長度,可以中斷迭代
List<int> fixedLengthList = []; //改變數(shù)組長度 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,然后在進行替換刪除索引值 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 過濾重復(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ù)組長度${listInts.length}");
}
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度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ù)組長度${listDouble.length}");
}
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度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ù)組長度${listStrings.length}");
}
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度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ù)組長度${listBool.length}");
}
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
I/flutter (28028): 數(shù)組長度1
4、List對象去重
class A{
String a;
int b;
A(this.a, this.b);
}
1、要添加的對象A的每個值和數(shù)組里面存在的每個對象的值做比較 (效率低、適合少量數(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ù)組長度${listAs.length}");
}
2、List配合Set去除重復(fù)對象
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ù)組長度${list.length}");
}
addASData(new A("a", 0));
I/flutter (10386): 數(shù)組長度1
I/flutter (10386): 數(shù)組長度1
I/flutter (10386): 數(shù)組長度1
I/flutter (10386): 數(shù)組長度1
I/flutter (10386): 數(shù)組長度1
I/flutter (10386): 數(shù)組長度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ù)的實現(xiàn)的文章就介紹到這了,更多相關(guān)Flutter List 重復(fù)插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中實現(xiàn)多線程的幾種方式小結(jié)
在 Android 中,實現(xiàn)多線程編程主要有7種方式,每種方式都有其適用場景和優(yōu)缺點,本文將詳細介紹一下具體實現(xiàn)方式,大家可以根據(jù)需要自行選擇2025-03-03
Android studio報: java.lang.ExceptionInInitializerError 錯誤
本篇文章主要介紹了Android studio報: java.lang.ExceptionInInitializerError錯誤的解決方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
Android百度定位導(dǎo)航之基于百度地圖移動獲取位置和自動定位
項目需求是這樣的,首先定位我當(dāng)前的起始位置,并跟隨移動不斷自動定位我的當(dāng)前位置,下面通過本文給大家介紹android百度定位導(dǎo)航之基于百度地圖移動獲取位置和自動定位,需要的朋友參考下2016-01-01
android與asp.net服務(wù)端共享session的方法詳解
這篇文章主要給大家介紹了關(guān)于android與asp.net服務(wù)端如何共享session的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。2017-09-09
Android中Service實時向Activity傳遞數(shù)據(jù)實例分析
這篇文章主要介紹了Android中Service實時向Activity傳遞數(shù)據(jù)的方法,實例分析了Service組件基于線程操作實現(xiàn)數(shù)值實時傳遞的相關(guān)技巧,需要的朋友可以參考下2015-09-09

