本節(jié)對(duì)循環(huán)語(yǔ)句進(jìn)行總結(jié)柴淘,包括以下幾種:
for循環(huán)
List myList = [1, 2, 3, 4];
List newList = new List();
for(var i=0; i<myList.length;i++){
newList.add(myList[i]*2);
}
for..in..
List myList = [1, 2, 3, 4];
List newList = new List();
for(var i in myList){
newList.add(myList[i]*2);
}
forEach
List myList = [1, 2, 3, 4];
myList.forEach((v) {
print(v);
});
map
返回一個(gè)數(shù)組
var newList = myList.map((v) {
return v * 2;
});
print(newList.toList());
where
返回滿足條件的值
List myList2 = [1, 2, 3, 4, 5, 6, 7];
var newList2 = myList.where((v) {
return v > 5;
});
print(newList2.toList()); // 6,7
any
只要有一個(gè)滿足條件就返回true
var f = myList.any((v) {
return v > 5;
});
print(f); // true
every
條件都滿足了才能返回true 否則返回false
var f2 = myList.every((v) {
return v > 5;
});
print(f2); // fasle