美文网首页Flutter
dart语言中list和map的遍历

dart语言中list和map的遍历

作者: 小迷糊_dcee | 来源:发表于2020-08-26 22:02 被阅读0次

    1、list的遍历

    var list = [1, 2, 3, 4, 5];
      for (var i = 0; i <= list.length; i++) {
        print(i);
      }
    
      for (var item in list) {
        print(item);
      }
    
      list.forEach((element) {
        print(element);
      });
    
      //where判断大于3,返回值为(4,5),toList()组成新的list
      var newList = list.where((element) => element > 3).toList();
      print(newList);
    
      //any 判断list的数据是否有大于3的值,返回值为bool,true
      var checkAny = list.any((element) => element > 3);
      print(checkAny);
    
      //every  判断list里面的数据是否都大于3,返回值为bool, false
      var checkEvery = list.every((element) => element > 3);
      print(checkEvery);
    
    

    2、map的遍历

    var map = {
        "name":"张三",
        "age":22,
        "work":"上班族",
        "sex":"男",
      };
    
    
      for(var item in map.keys){
        print(item);
      }
    
      for(var item in map.values){
        print(item);
      }
    
      map.forEach((key, value) {
        print("$key---$value");
      });
    
      
      var newList = map.values.where((element) => element==22);
      print(newList.toList());
    
      //判断map的值是否有张三,返回值bool
      var checkAny = map.values.any((element) => element=="张三");
      print(checkAny);
    
      //判断map的值是否全部为张三,返回值bool
      var checkEvery = map.values.every((element) => element=="张三");
      print(checkEvery);
    
    

    相关文章

      网友评论

        本文标题:dart语言中list和map的遍历

        本文链接:https://www.haomeiwen.com/subject/pjafsktx.html