Dart (2) - Common properties and methods of loop expressions, List, Set and Map

Dart (1) - variables, constants, basic types, operators, conditional judgments and type conversions

Dart's loop expression

for loop

for (int i = 1; i<=100; i++) {   
  print(i);
}

It can also be written as:

for (var i = 1; i<=10; i++) {
  print(i);
}

For Listtraversal we can do:

var list = <String>["张三","李四","王五"];
for (var element in list) {
  print(element);
}

For Mapiteration we can also use a forloop statement:

var person={
  "name":"小明",
  "age":28,
  "work":["程序员","Android开发"]
};

person.forEach((key, value) {
  print(value);
});

while statement

whileThere are two statement formats:

while(表达式/循环条件){    
 
}  
    
do{
 语句/循环体
 
}while(表达式/循环条件);

Notice:

  • 1. Don't forget the last semicolon
  • 2. Variables used in loop conditions need to be initialized
  • 3. In the loop body, there should be conditions to end the loop, otherwise it will cause an infinite loop.

See the code below:

int i = 1;
while (i <= 10) {
  print(i);
  i++;
}

do...while()The biggest difference is that it will be executed at least once regardless of whether the condition is true or not:

var i = 2;
do{
  print('执行代码');
}while(i < 2);

break and continue statements

break statement function:

  • Make the flow out of the structure in the switchstatement .switch
  • In the loop statement, the process jumps out of the current loop. When the breakloop is terminated, the following code will not be executed.

It should be emphasized that:

  • If the statement has already been executed in the loop, the breakstatement after break in the loop body will not be executed.
  • In a multi-layer loop, a breakstatement can only jump out one level

breakCan be used in switch caseas well as in forloops and whileloops.

The function of continue statement:

It can only be used in a loop statement to end the current loop, that is, skip the unexecuted statement below the loop body, and then judge whether to execute the loop next time.

continueIt can be used in forloops and whileloops, but it is not recommended to be used in whileloops, and it is easy to die if you are not careful.

break use:

//如果 i等于4的话跳出循环
for(var i=1;i<=10;i++){
  if(i==4){
    break;  /*跳出循环体*/
  }
  print(i);
}
//break语句只能向外跳出一层
 for(var i = 0;i < 5;i++){
    for(var j = 0;j< 3;j++){
      if(j == 1){
        break;
      }
    }  
 }

The while loop jumps out:

//while循环 break跳出循环

var i = 1;

while(i< =10){
  if(i == 4){
    break;
  }
  print(i);
  i++;
}

continue using:

//如果i等于4的话跳过

for(var i=1;i<=5;i++){
  if(i == 2){
    continue;  //跳过当前循环体 然后循环还会继续执行
  }
  print(i);
}

List common properties and methods

Common properties:

  • lengthlength
  • reversedflip
  • isEmptyis empty
  • isNotEmptyis not empty

Common method:

  • addIncrease
  • addAllconcatenated array
  • indexOfFind incoming specific value
  • removeDelete incoming specific value
  • removeAtdelete the incoming index value
  • fillRangeRevise
  • insert(index,value)Insert at specified location
  • insertAll(index,list)Insert a List at a specified location
  • toList()Convert other types to List
  • join()Convert List to String
  • split()Convert string to List
  • forEach
  • map
  • where
  • any

Some common properties and methods use examples:

var list=['张三','李四','王五',"小明"];
print(list.length);
print(list.isEmpty);
print(list.isNotEmpty);
print(list.reversed);  //对列表倒序排序

print(list.indexOf('李四'));    //indexOf查找数据 查找不到返回-1  查找到返回索引值

list.remove('王五');

list.removeAt(2);

list.fillRange(1, 2,'a');  //修改 1是开始的位置 2二是结束的位置

print(list);

list.insert(1,'a');

print(list);

list.insertAll(1, ['a','b']); //插入多个

Set

The main function of Set is to remove the repeated content of the array. It is a collection that has no order and cannot be repeated, so the value cannot be obtained by index.

var s = new Set();
s.add('A');
s.add('B');
s.add('B');

print(s); //{A, B}

When the addsame content cannot be added.

A Set can add one via addmethods List, and clear elements with the same value:

var list = ['香蕉','苹果','西瓜','香蕉','苹果','香蕉','苹果'];
var s = new Set();
s.addAll(list);
print(s);
print(s.toList());

Map common properties and methods

Map is an unordered key-value pair. Its common properties are as follows:

Common properties:

  • keysGet all key values
  • valuesget all values
  • isEmptyis empty
  • isNotEmptyis not empty

Common method:

  • remove(key)Delete the data of the specified key
  • addAll({...})Merge maps to add attributes to the map
  • containsValueView the value inside the map returns true/false
  • forEach
  • map
  • where
  • any
  • every

map transformation:

List list = [1, 3, 4];
//map转换,根据返回值返回新的元素列表
var newList = list.map((value) {
  return value * 2;
});
print(newList.toList());

where: Get the elements that meet the conditions:

List list = [1,3,4,5,7,8,9];

var newList = list.where((value){
    return value > 5;
});
print(newList.toList());

any: whether there is an element that matches the condition

List list = [1, 3, 4, 5, 7, 8, 9];
//只要集合里面有满足条件的就返回true
var isContain = list.any((value) {
  return value > 5;
});
print(isContain);

every: every one of them needs to meet the condition

List myList=[1,3,4,5,7,8,9];
//每一个都满足条件返回true  否则返回false
var flag = myList.every((value){
  
    return value > 5;
});
print(flag);

Set is traversed using forEach:

var s=new Set();

s.addAll([11,22,33]);

s.forEach((value) => print(value));

Map is traversed using forEach:

Map person={
  "name":"张三",
  "age":28
};

person.forEach((key,value){
  print("$key -> $value");
});

Guess you like

Origin juejin.im/post/7116159285054144526