Flutter List数组避免插入重复数据的实现

 更新时间:2020年09月04日 14:24:49   作者:唇红白齿  
这篇文章主要介绍了Flutter List数组避免插入重复数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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);
//改变固定数组长度
fixedLengthList.length=30;

Unsupported operation: Cannot change the length of a fixed-length list

大概意思:无法更改固定长度数组的长度

List<int> fixedLengthList = new List(2);
///执行添加数据操作
fixedLengthList.add(0);
fixedLengthList.add(1);
List<int> fixedLengthList = new List(2);
  fixedLengthList[0]=1;
  fixedLengthList[1]=2;
///添加数据
fixedLengthList.addAll([3,4]);

Unsupported operation: Cannot add to a fixed-length list
大概以上: 不能添加数据到固定长度数组 

List<int> fixedLengthList = new List(2);
//执行插入数据
fixedLengthList.insert(0, 0);

Unsupported operation: Cannot add to a fixed-length list
大概意思: 不能添加数据到固定长度数组  

 List<int> fixedLengthList = new List(2);
  ///执行删除操作
  fixedLengthList.removeLast();
List<int> fixedLengthList = new List(2);
  ///执行删除操作
  fixedLengthList.removeAt(0);
 List<int> fixedLengthList = new List(2);
  fixedLengthList[0]=1;
  fixedLengthList[1]=2;
///删除包含索引0和1范围内数据
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
大概意思:不能删除固定长度数组数据

List<int> fixedLengthList = new List(2);
///执行清除数据操作
fixedLengthList.clear();

Unsupported operation: Cannot clear a fixed-length list
大概意思:不能清理固定长度数组数据

可排序、替换、截取

List<int> fixedLengthList = new List(2);
fixedLengthList[0]=1;
fixedLengthList[1]=2;

///执行截取指定范围的数组
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、可增长列表 

可改变数组长度、 可执行添加、删除、可排序、可替换、可截取

.可增长列表[]保留了内部缓冲区

.缓冲区可增长

.添加数据操作在固定时间内执行 (设置固定长度会花费与新长度成比例的时间,修改容量,添加操作将需要立即增加缓冲区容量)

.列表是可以迭代的

.在执行列表操作时,例如在调用forEachsort期间,通常不允许修改列表的长度(添加或删除元素)

.通过直接迭代列表或通过迭代由列表支持的Iterable更改列表的长度,可以中断迭代

List<int> fixedLengthList = [];

  //改变数组长度
  fixedLengthList.length=2;
  ///执行添加数据操作
  fixedLengthList.add(0);
  fixedLengthList.add(1);
  fixedLengthList[0]=1;
  fixedLengthList[1]=2;
  ///添加数据
  fixedLengthList.addAll([3,4]);
  //执行插入数据
  fixedLengthList.insert(0, 0);
  ///执行删除操作
  fixedLengthList.removeLast();
  ///执行删除操作
  fixedLengthList.removeAt(0);
  ///删除包含索引0和1范围内数据
  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);

  ///执行清除数据操作
  fixedLengthList.clear();

3、contains 过滤重复 添加(int、double、bool、String)类型数据

1、int类型数组中插入重复数据 

List<int> listInts = [];

 void addIntData(int addValue){
  bool isContainer=listInts.contains(addValue);
  if(!isContainer){
   listInts.add(addValue);
  }
  print("数组长度${listInts.length}");
 }

I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1 

 2、double类型数组中插入重复数据

List<double> listDouble = [];

 void addDoubleData(double addValue){
  bool isContainer=listDouble.contains(addValue);
  if(!isContainer){
   listDouble.add(addValue);
  }
  print("数组长度${listDouble.length}");
 }

I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1

3、String类型数组中插入重复数据

 List<String> listStrings = [];

 void addStringData(String addValue){
  bool isContainer=listStrings.contains(addValue);
  if(!isContainer){
   listStrings.add(addValue);
  }
  print("数组长度${listStrings.length}");
 }

I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1 

4、boolean类型数组插入重复数据

 List<bool> listBool = [];

 void addBoolData(bool addValue){
  bool isContainer=listBool.contains(addValue);
  if(!isContainer){
   listBool.add(addValue);
  }
  print("数组长度${listBool.length}");
 }

I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1
I/flutter (28028): 数组长度1

4、List对象去重

class A{
 String a;
 int b;

 A(this.a, this.b);
}

1、要添加的对象A的每个值和数组里面存在的每个对象的值做比较 (效率低、适合少量数据去重)

 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("数组长度${listAs.length}");
 }

2、List配合Set去除重复对象

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("数组长度${list.length}");
}
addASData(new A("a", 0));

I/flutter (10386): 数组长度1
I/flutter (10386): 数组长度1
I/flutter (10386): 数组长度1
I/flutter (10386): 数组长度1
I/flutter (10386): 数组长度1
I/flutter (10386): 数组长度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 

到此这篇关于Flutter List数组避免插入重复数据的实现的文章就介绍到这了,更多相关Flutter List 重复插入内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Android中实现多线程的几种方式小结

    Android中实现多线程的几种方式小结

    在 Android 中,实现多线程编程主要有7种方式,每种方式都有其适用场景和优缺点,本文将详细介绍一下具体实现方式,大家可以根据需要自行选择
    2025-03-03
  • Android10自动连接WiFi问题的解决

    Android10自动连接WiFi问题的解决

    这篇文章主要介绍了Android10自动连接WiFi问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • Android studio报: java.lang.ExceptionInInitializerError 错误

    Android studio报: java.lang.ExceptionInInitializerError 错误

    本篇文章主要介绍了Android studio报: java.lang.ExceptionInInitializerError错误的解决方法,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • Android百度定位导航之基于百度地图移动获取位置和自动定位

    Android百度定位导航之基于百度地图移动获取位置和自动定位

    项目需求是这样的,首先定位我当前的起始位置,并跟随移动不断自动定位我的当前位置,下面通过本文给大家介绍android百度定位导航之基于百度地图移动获取位置和自动定位,需要的朋友参考下
    2016-01-01
  • Android实现精美的聊天界面

    Android实现精美的聊天界面

    这篇文章主要为大家详细介绍了Android实现精美的聊天界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android帧率监测与优化技巧

    Android帧率监测与优化技巧

    Android 应用的性能优化是开发过程中至关重要的一环,而帧率(Frame Rate)是评估应用性能的一个关键指标,在本文中,我们将深入探讨如何监测 Android 应用的帧率,以及如何通过代码示例来优化应用的性能,需要的朋友可以参考下
    2023-10-10
  • Android动态加载布局

    Android动态加载布局

    这篇文章主要介绍了Android动态加载布局,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • Android实现图片一边的三角形边框效果

    Android实现图片一边的三角形边框效果

    这篇文章主要介绍了Android实现图片一边的三角形边框效果,本文图文并茂通过实例代码讲解的非常详细,需要的朋友可以参考下
    2019-12-12
  • android与asp.net服务端共享session的方法详解

    android与asp.net服务端共享session的方法详解

    这篇文章主要给大家介绍了关于android与asp.net服务端如何共享session的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋们下面随着小编来一起学习学习下吧。
    2017-09-09
  • Android中Service实时向Activity传递数据实例分析

    Android中Service实时向Activity传递数据实例分析

    这篇文章主要介绍了Android中Service实时向Activity传递数据的方法,实例分析了Service组件基于线程操作实现数值实时传递的相关技巧,需要的朋友可以参考下
    2015-09-09

最新评论