dar中list,map常用属性和方法

    科技2024-12-18  9

    list常用属性和方法

    常用属性

    1,length 获取list的长度

    main() { var list = ['red','yellow','pink']; print(list.length); }

    2,判断是否为空

    main() { var list = ['red','yellow','pink']; print(list.isEmpty); }

    3,判断不为空

    main() { var list = ['red','yellow','pink']; print(list.isNotEmpty); }

    4,数组翻转

    main() { var list = ['red','yellow','pink']; print(list.reversed.toList()); } (pink, yellow, red)

    常用方法

    1,增加数据add

    main() { var list = ['red','yellow','pink']; list.add("green"); print(list); } [red, yellow, pink, green]

    2,查找数据indexOf,查找到返回索引,查找不到返回-1

    main() { var list = ['red','yellow','pink']; var res = list.indexOf('yellow'); print(res); } 1

    3,删了数据remove(‘值’),removeAt(索引)

    main() { var list = ['red','yellow','pink']; list.remove('yellow'); list.removeAt(1); print(list); } [red]

    4,修改数据fillRange(start,end,value) 不包含end

    main() { var list = ['red','yellow','pink']; list.fillRange(1, 2,'green'); print(list); } [red, green, pink]

    5, 给指定位置插入数据insert(index,value) 从index开始插入

    main() { var list = ['red','yellow','pink']; list.insert(1, 'green'); print(list); } [red, green, yellow, pink] main() { var list = ['red','yellow','pink']; list.insertAll(1, ['green','white']); print(list); } [red, green, white, yellow, pink]

    6,转换成字符串join(’,’),并不改为原有list

    main() { var list = ['red','yellow','pink']; var str = list.join(','); print(str); } red,yellow,pink

    7,将字符串转换成数组split(’-’),不改为原有字符串

    main() { var str = 'red-yellow-pink'; var list = str.split('-'); print(list); print(str); } [red, yellow, pink] red-yellow-pink

    8,for(var item in list) 循环list

    main() { var list = ['red','yellow','pink']; for(var item in list) { print(item); } } red yellow pink

    9,map循环环list

    main() { var list = [1,2,3,4]; var newList = list.map((value) => value * 2); print(newList.toList()); }

    10,查看集合满足某个条件any

    main() { var list = ['red','yellow','pink']; var f = list.any((value) => value == 'red'); print(f); print(list); }

    11,通过set不能添加重复集合,所以不能通过索引去获取

    main() { var set = new Set(); set.add('red'); set.add('yellow'); set.add('pink'); set.add('red'); print(set.toList()); } [red, yellow, pink]

    maps类型常用属性与方法

    1,获取所有的key

    main() { var person = { 'name':'张三', 'age':20, }; print(person.keys.toList()); } [name, age]

    2,获取所有的value

    main() { var person = { 'name':'张三', 'age':20, }; print(person.values.toList()); }

    3,判断属性是否为空isEmpty

    main() { var person = { 'name':'张三', 'age':20, }; print(person.isEmpty); } false
    常用方法

    1,删除指定key的数据remove(key)

    main() { var person = { 'name':'张三', 'age':20, }; person.remove('name'); print(person); } {age: 20}

    2,给map对像添加属性addAll({‘key’:‘value’})

    main() { var person = { 'name':'张三', 'age':20, }; person.addAll({ 'sex':'男', }); print(person); } {name: 张三, age: 20, sex: 男}

    3,查看个字段是否在map内containsKey(key),返回true/false并不改变原有对像

    main() { var person = { 'name':'张三', 'age':20, }; var boo = person.containsKey('name'); print(boo); } true

    博客地址 相关文档 github文档地址

    Processed: 0.015, SQL: 8