List里面常用的属性和方法:
常用属性:
length 长度
reversed 翻转
isEmpty 是否为空
isNotEmpty 是否不为空
常用方法:
add 增加
addA11 拼接数组
indexof 查找 传入具体值
remove 删除 传入具体值
removeAt 删除 传入索引值
fillRange 修改
insert(index,value); 指定位置插入
insertAll(index,list) 指定位置插入List
tolist() 其它类型转换成List
join() List转换成字符串
split() 字符串转化成List
List里面常见的属性:
List myList=["香蕉","苹果","车厘子"];
print(myList.length);//长度(3)
print(myList.isEmpty);//是否为空(false)
print(myList.isNotEmpty);//是否不为空(true)
print(myList.reversed);//对列表倒序排序((车厘子, 苹果, 香蕉))
var newMyList=myList.reversed.toList();
print(newMyList);//对列表倒序排序之后还要将类型转换成List[车厘子, 苹果, 香蕉]
List里面常见的方法:
//add
List myList1=["香蕉","苹果","车厘子"];
myList1.add("桃子");//增加数据 增加一个
print(myList1);//[香蕉, 苹果, 车厘子, 桃子]
//addAll
myList1.addAll(["西瓜","芒果"]);//拼接数组 增加多个
print(myList1);//[香蕉, 苹果, 车厘子, 桃子, 西瓜, 芒果]
//indexOf
print(myList1.indexOf("苹*果"));//indexOf查找数据 查找不到返回-1 查找到返回1
//remove
myList1.remove("西瓜");//删除数据,删除传入具体值
print(myList1);//[香蕉, 苹果, 车厘子, 桃子, 芒果]
//removeAt
myList1.removeAt(2);//删除数据, 删除传入索引值
print(myList1);//[香蕉, 苹果, 桃子, 芒果]
//fillRange
myList1.fillRange(1, 2,"aaa");//修改数据
print(myList1);//[香蕉, aaa, 桃子, 芒果]
//insert
myList1.insert(1, "bbb");//指定位置插入
print(myList1);//[香蕉, bbb, aaa, 桃子, 芒果]
//insertAll
myList1.insertAll(3, ["ccc","ddd"]);//指定位置插入List
print(myList1);//[香蕉, bbb, aaa, ccc, ddd, 桃子, 芒果]
//join
var str=myList1.join(",");//List转换成字符串以逗号分割
print(str);//香蕉, bbb, aaa, ccc, ddd, 桃子, 芒果
//split
var str1="苹果-葡萄-西瓜";
var list=str1.split("-");//字符串转换成List以横杠分割
print(list);//[苹果, 葡萄, 西瓜]
Set
用它最主要的功能就是去除数组重复内容
Set是没有顺序且不能重复的集合,所以不能通过索引去获取值
var s=new Set();
s.add("芒果");
s.add("西瓜");
s.add("西瓜");
print(s);//只返回去重后的数据{芒果, 西瓜}
print(s.toList());//将Set转换成List
//用Set给集合去重
var str=["西瓜","芒果","香蕉","芒果","西瓜"];
var s1=new Set();
s1.addAll(str);
print(s1.toList());//[西瓜, 芒果, 香蕉]
映射(Maps)是无序的键值对:
keys 获取所有的key值
values 获取所有的value值
isEmpty 是否为空
isNotEmpty 是否不为空
常用方法:
remove(key) 删除指定key的数据
addA11({...})合并映射 给映射内增加属性
containsValue 查看映射的值 返回true/false
/常用方法:
var person1={
"name":"张三",
"age":20,
"sex":"男"
};
//增加键值对
person1.addAll({
"height":"175",
});
print(person1);//{name: 张三, age: 20, sex: 男, height: 175}
//删除指定key的数据
person1.remove("sex");
print(person1);//{name: 张三, age: 20, height: 175}
//查看映射的值 返回true/false
print(person1.containsValue("张三"));//true
循环语句forEach map where any every
ar str=["西瓜","芒果","香蕉","芒果","西瓜"];
//用for循环去遍历
for(var i=0;i<str.length;i++){
print(str[i]);
}
print("---------------");
//遍历2
for(var item in str){
print(item);
}
print("---------------");
//用forEach()遍历
str.forEach((value){
print("$value");
});
//map主要用于修改集合里面的数据
List myList=[1,2,3];
var newList=myList.map((value){
return value*2;
});
print(newList.toList());//[2, 4, 6]
//where遍历满足条件的数组
List myList1=[1,2,3,4,5,6,7,8];
var newList1=myList1.where((value){
return value>3;
});
print(newList1.toList());//[4, 5, 6, 7, 8]
//any和every表示判断List里面有没有满足条件的数据 返回true/false
List myList2=[1,2,3,4,5,6,7,8];
var f=myList2.any((value){//只要集合里面有满足条件的就返回true
return value>5;
});
print(f);//true
var f1=myList2.every((value){//每一个都满足条件返回true 否则返回false
return value>5;
});
print(f1);//false
//循环set
var s=new Set();
s.addAll([1,2,3445]);
s.forEach((value){
return print("$value");
});
//循环map
var person={
"name":"张三",
"age":20,
"sex":"男"
};
person.forEach((key,value){
return print("$key-----$value");
});