Flutter——最详细数组List使用教程

Posted 怀君

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter——最详细数组List使用教程相关的知识,希望对你有一定的参考价值。

1,插入元素 insert()

	List<int> leftList = [1, 2, 3, 4, 5, 6]
	leftList.insert(0, 0);
    print(leftList.toString());
    //[0, 1, 2, 3, 4, 5, 6]

2,插入一段素组元素 insertAll()

  List<int> leftList = [1, 2, 3, 4, 5, 6];
  List<int> rightList = [62, 23, 7, 9];
  leftList.insertAll(0, rightList);
  print(leftList.toString());
    //[62, 23, 7, 9, 1, 2, 3, 4, 5, 6]

3,尾部添加元素 add()

  List<int> leftList = [1, 2, 3, 4, 5, 6];
  leftList.add(7);
  print(leftList.toString());  
    //[1, 2, 3, 4, 5, 6, 7]

4,尾部添加素组 addAll

	List<int> leftList = [1, 2, 3, 4, 5, 6];
    leftList.addAll([8]);
    print(leftList.toString());
    //[1, 2, 3, 4, 5, 6, 8]

5,删除素组中的元素 remove

    leftList.remove(3);
    print(leftList.toString());
    //[1, 2, 4, 5, 6]

6,根据索引删除素组中的元素 removeAt

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    leftList.removeAt(4);
    print(leftList.toString());
    //[1, 2, 3, 4, 6]

7,删除索引0到3的元素 removeRange

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    leftList.removeRange(0, 3);
    print(leftList.toString());
    //[4, 5, 6]

8,删除数组最后一位元素 removeLast

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    leftList.removeLast();
    print(leftList.toString());
    //[1, 2, 3, 4, 5]

9,保存数组满足该条件的元素 removeWhere

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    leftList.removeWhere((element) => element.isOdd);
    print(leftList.toString());
    //[2, 4, 6]

10,替换索引1至4的元素 setRange

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    leftList.setRange(1, 4, [1, 11, 2, 2]);
    print(leftList.toString());
    //[1, 1, 11, 2, 5, 6]

11,从索引3的位置,替换老元素, setAll

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    leftList.setAll(3, [1, 11, 54]);
    print(leftList.toString());
    //[1, 2, 3, 1, 11, 54]

12,从索引3至4的位置,替换新素组,并保留尾部的元素 replaceRange

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    leftList.replaceRange(3, 4, [1, 11, 2, 2]);
    print(leftList.toString());
    //[1, 2, 3, 1, 11, 2, 2, 5, 6]

13,从索引3至5的位置,将元素修改成2 fillRange

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    leftList.fillRange(3, 5, 2);
    print(leftList.toString());
    // [1, 2, 3, 2, 2, 6]

14,获取索引1至5的元素 getRange

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    var range = leftList.getRange(1, 5);
    print(range.toString());
    //(2, 3, 4, 5)

15,根据索引值,返回新的数组 sublist

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    var range = leftList.sublist(3);
    print(range.toString());
    //[4, 5, 6]
    var ranges = leftList.sublist(3, 5);
    print(ranges.toString());
    //[4, 5]

16,判断素组是否 存在 满足该条件 any

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    var any = leftList.any((element) => element > 3);
    print(any.toString());

17,判断数组是否 全员 满足该条件 every

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    var any = leftList.every((element) => element > 3);
    print(any.toString());
    // false

18,打印数组最后一位满足改条件的元素,不满足则走 orElse方法 lastWhere

   List<int> leftList = [1, 2, 3, 4, 5, 6];

    /// 获取最后一个大于3的元素
    print(leftList.lastWhere((v) => v > 6));
    // 
    // leftList.firstWhere((element) => element > 6, orElse: () 
    // );

19,从数组中查找是否满足条件,并返回索引值 indexWhere

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());
    //[1, 2, 3, 4, 5, 6]
    var indexWhere = leftList.indexWhere((element) => element > 7);
    print(indexWhere);
    // -1
    var indexWheres = leftList.indexWhere((element) => element > 1, 1);
    print(indexWheres);
    // 1

20,从最后一位开始查找,是否满足该条件的索引 lastIndexWhere

    List<int> leftList = [1, 2, 3, 4, 5, 2];
// 获取最后一个大于4的元素索引值
    print(leftList.lastIndexWhere((v) => v > 2)); // 5
// 从索引4开始,查询最后一个大于4的元素索引值
    print(leftList.lastIndexWhere((v) => v > 3, 4)); // 4
// 如果没有,返回-1
    print(leftList.lastIndexWhere((v) => v > 9)); // -1

21,数组是否存在该值 indexOf

    List<int> leftList = [1, 2, 3, 4, 5, 6];

    ///从索引 3 开始查找,获取第一次出现2的索引值,如果不存在,返回 -1
    print(leftList.indexOf(2, 3)); 
    //-1
    print(leftList.indexOf(5));
    //4

22,从最后面开始查找是否存在该值 lastIndexOf

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.toString());

    ///从索引 3 开始查找,倒序获取第一次出现2的索引值,如果不存在,返回 -1
    print(leftList.lastIndexOf(2, 3));
    //1
    print(leftList.lastIndexOf(6));
    //5
    print(leftList.lastIndexOf(9));
    //-1

23,判断是否存在该条件的元素 singleWhere

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    // 获取等于2的唯一元素,存在,返回2
    print(leftList.singleWhere((v) => v == 2));
//2
    // 获取等于6的唯一元素,存在该元素,但是出现次数不唯一,不会执行orElse,直接抛出错误,进入catch
    print(leftList.singleWhere((v) => v == 6));
//6
    // 获取大于6的唯一元素,不存在该元素,执行orElse
    print(leftList.singleWhere((v) => v > 6, orElse: () 
      return orElse(1);
    ));

24,将数组拼接成字符串 join

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    print(leftList.join('&'));
    //1&2&3&4&5&6

25,数组去重 toSet

   List<int> leftList = [1, 2, 3, 4,3, 5, 6];
    leftList.add(3);
    print(leftList.toSet());
    //[1, 2, 3, 4, 5, 6]

26,数组循环遍历 forEach

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    leftList.forEach((element) 
      print(element);
    );

    for (var element in leftList) 
      print(element);
    

27,数组参数遍历 map

    List<int> leftList = [1, 2, 3, 4, 5, 6];
    var map = leftList.map((e) 
      return e + 2;
    );
    print(map.toString());
    // (3, 4, 5, 6, 7, 8)
    var map2 = leftList.map((e) 
      return e > 3;
    );
    print(map2.toString());
    // (false, false, false, true, true, true)

28,数组累加 reduce

   List<int> leftList = [1, 2, 3, 4, 5, 6];
    var reduce = leftList.reduce((value, element) 
      print('value = $value ; element = $element');
      return value + element;
    );
    print(reduce.toString());
 //   value = 1 ; element = 2
//value = 3 ; element = 3
//value = 6 ; element = 4
//value = 10 ; element = 5
//value = 15 ; element = 6
//21

29,数组 a-b 升序 ,b-a降序 sort

 List<int> leftList = [1, 2, 3, 4, 5, 6];
    rightList.sort((a, b) 
      return b - a;
    );
    print(rightList.toString());
    //b-a =  [6, 5, 4, 3, 2, 1]
    //a-b = [1, 2, 3, 4, 5, 6]

以上是关于Flutter——最详细数组List使用教程的主要内容,如果未能解决你的问题,请参考以下文章

Flutter——最详细数组List使用教程

Flutter——最详细的Text(文本)使用教程

Flutter——最详细的Text(文本)使用教程

Flutter——最详细的Row(横向)布局使用教程

Flutter——最详细的Row(横向)布局使用教程

Flutter——最详细Stack(堆叠布局)使用教程