一、List
main() { // 定义一个int类型数组 List list1 = <int>[1, 2]; print(list1); //[1, 2] // length长度 print(list1.length); //2 // reversed翻转 print(list1.reversed.toList()); //[2, 1] // isEmpty 是否为空 print(list1.isEmpty); //false //isNotEmpty 是否不为空 print(list1.isNotEmpty); //true //add添加 list1.add(3); print(list1); //[1, 2, 3] //addAll 添加一个数据 list1.addAll(<int>[4, 5]); print(list1); //[1, 2, 3, 4, 5] //indexOf 查找 传入具体指 找到返回索引,否则返回-1 print(list1.indexOf(1)); //0 print(list1.indexOf(90)); //-1 //remove 删除具体值 list1.remove(4); //[1, 2, 3, 5] print(list1); //removeAt 删除 传入索引 list1.removeAt(0); print(list1); //[2, 3, 5] //fillRange 修改 list1.fillRange(1, 2, 89); //[2, 89, 5] print(list1); //insert 插入值 list1.insert(2, 99); //[2, 89, 99, 5] 如果插入位置大于现在拥有的下标-》Invalid value: Not in inclusive range 0..3: 7 print(list1); //insertAll 插入数组 list1.insertAll(2, <int>[33, 44]); print(list1); //[2, 89, 33, 44, 99, 5] //toList 其它类型转换为List var str = list1.toString(); print(str); //[2, 89, 33, 44, 99, 5] print(str.length); //22 //join() List转换为字符串 print(list1.join(":")); //2:89:33:44:99:5 //split()字符串转换为list print("1,2,3".split(",") is List); //true //forEach list1.forEach((element) {print(element);});//2 89 33 44 99 5 //map 遍历每个元素,返回准换后的结果 list1=list1.map((e) => e*2).toList(); print(list1); //[4, 178, 66, 88, 198, 10] //where 过滤元素,保留满足条件的元素 list1=list1.where((element) => element>87).toList(); print(list1);//[178, 88, 198] //any 判断集合中是否有一个元素满足条件 element>188 var condition_1= list1.any((element) => element>188); print(condition_1); //true //every 判断集合中是否有所有元素满足条件 element>188 var condition_2= list1.every((element) => element>188); print(condition_2); //false }
二、map
main() { // 键-》值 Map person = {"name": "zhangsan", 'age': 20}; // 获取长度 print(person.length); //2 // 判断是否为空 print(person.isNotEmpty); //true // 获取运行是类型 print(person.runtimeType); //_InternalLinkedHashMap<dynamic, dynamic> // 获取hashCode print(person.hashCode); //1018109981 // 获取所有键 print(person.keys.toList()); //[name, age] // 获取所有值 print(person.values.toList()); //[zhangsan, 20] //添加键值对 person.addAll({"work": "coding"}); print(person); //{name: zhangsan, age: 20, work: coding} // 产出一个键 person.remove('age'); print(person); //{name: zhangsan, work: coding} // 判断是否包含值 print(person.containsValue("coding")); //true // 判断是否包含键 print(person.containsKey("work")); //true }
三、set
重复方法见List
main(){ var mySet=new Set(); mySet.add(1); mySet.add(2); mySet.add(1); print(mySet);//{1, 2} print(mySet.toList());//[1, 2] }
四、遍历List
main() { List myList = ["a", "b", "c"]; // for in for (var value in myList) { print(value); } // for for (int i = 0; i < myList.length; i++) { print(myList[i]); } // forEach myList.forEach((element) { print(element); }); myList.forEach((element) => print(element)); }