5.Dart collection types List Set Map Detailed and loop forEach map where any every

1.List

List which common attributes and methods:
 

    Common attributes:

        length length

        Flip reversed

        Whether isEmpty empty

        Whether isNotEmpty not empty

    Common methods:  

        Add to add

        addAll splicing array

        indexOf to find specific values ​​passed

        remove delete specific values ​​passed

        removeAt delete incoming index value

        fillRange modification   

        insert (index, value); inserting specified position    

        insertAll (index, list) specified position inserted List

        toList () is converted into other types of List  

        join () List into a string

        split () is converted into String List

        forEach   

        map

        where

        any

        every

 

1) List the inside of the property

main void () { 

// List no properties: 
    List myList = [ 'banana', 'apple', 'watermelon']; 
    Print (myList.length); 
    Print (myList.isEmpty); 
    Print (myList.isNotEmpty); 
    print (myList.reversed); // reverse sort the list, output tuples 
    var newMyList myList.reversed.toList = (); 
    Print (newMyList); 

}

 

2) List inside approach:

main void () { 

    List myList = [ 'banana', 'apple', 'watermelon']; 
    myList.add ( 'peach'); // data increases by increasing a 

    myList.addAll ([ 'peach', 'grapes'] ); // array of splicing 

    Print (myList); 

    Print (myList.indexOf ( 'apple fruit x')); // Find the indexOf lookup data is less than -1 returns the index to find 


    myList.remove ( 'watermelon'); 

    myList.removeAt (. 1); 

    Print (myList); 

}

 

main void () { 

    List myList = [ 'banana', 'apple', 'watermelon']; 

    myList.fillRange (. 1,. 3 'AAA'); // Modify 
    
    Print (myList) 

}

 

main void () { 

    List myList = [ 'banana', 'apple', 'watermelon']; 

    myList.insert (. 1, 'AAA'); // insert a 

    myList.insertAll (1, [ 'aaa' , 'bbb ']); // insert plurality 

    Print (myList); 


}

 

main void () { 

    List myList = [ 'banana', 'apple', 'watermelon']; 

    var = myList.join STR ( '-'); // List converted to a string 

    Print (STR); 

    Print (STR IS String); // to true 

}

 

main void () { 

    var STR = 'Banana - Apple - watermelon'; 

    var = str.split List ( '-'); // convert the string into List 

    Print (List); 

    Print (List List IS); // Ture 

}

 

2.Set

With its main function is to remove duplicate content array

Set sequence is not set and can not be repeated, it is not feasible to obtain an index value
main void () { 

  var new new the Set S = (); 
  s.add ( 'banana'); 
  s.add ( 'apple'); 
  s.add ( 'apple'); 

  Print (S); // {bananas, Apple} 

  Print (s.toList ()); 

}
main void () { 

  List myList = [ 'banana', 'apple', 'watermelon', 'banana', 'apple', 'banana', 'apple']; 

  var new new the Set S = (); 

  s.addAll ( myList); 

  Print (S); 

  Print (s.toList ()); 

}

 

3.Maps

 Map (Maps) are unordered key-value pairs:

    Common attributes:

        keys to get all the key values

        Get all values ​​of the value value

        Whether isEmpty empty

        Whether isNotEmpty not empty

    Common methods:

        remove (key) to delete the specified key data

        addAll ({...}) mapped to the combined properties of increased mapping

        Value in the map view containsValue Returns true / false

        forEach   

        map

        where

        any

        every

 

1). Common Properties

void main(){

    Map person={
      "name":"张三",
      "age":20,
      "sex":"男"
    };

    print(person.keys.toList());   //[name, age, sex]
    print(person.values.toList()); //[张三, 20, 男]
    print(person.isEmpty);         //false
    print(person.isNotEmpty);      //true


}

 

2). The method used

void main(){

    Map person={
      "name":"张三",
      "age":20,
      "sex":"男"
    };

    person.addAll({
      "work":['敲代码','送外卖'],
      "height":160
    });

    print(person);

    person.remove("sex");
    print(person);

    print(person.containsValue('张三'));
   }

}

 

  

3.forEach  map   where   any  every

1)forEach

Loop element

main void () { 
       List myList = [ 'banana', 'apple', 'watermelon']; 
             for (var I = 0; I <myList.length; I ++) { 
        Print (myList [I]); 
      } 
}
main void () { 
      List myList = [ 'banana', 'apple', 'watermelon'];       
      for (var Item in myList) { 
        Print (Item); 
      } 

}
main void () { 
       List myList = [ 'banana', 'apple', 'watermelon']; 
      // myList.forEach ((Key, value) { 
      // Print ( 'Key $ --- $ value'); 
      / /}); 

      myList.forEach ((value) { 
          Print ( "$ value"); 
      }); 

}

2)map  

Modify the array of data map

void main(){
   List myList=[1,3,4];

   List newList=new List();

   for(var i=0;i<myList.length;i++){

      newList.add(myList[i]*2);
   }
   print(newList);   //[2, 6, 8]

   

   List myList=[1,3,4];      
   var newList=myList.map((value){
       return value*2;
   });
   print(newList.toList());     //[2, 6, 8]

}

 

3)where

Find Array

void main(){
     List myList=[1,3,4,5,7,8,9];

    var newList=myList.where((value){
        return value>5;
    });
    print(newList.toList());  //[7, 8, 9]

}

 

4)any

As long as the condition returns true

main void () { 
   List myList = [1,3,4,5,7,8,9]; 

   var myList.any F = ((value) {// set as long as there are satisfied the conditions return to true 
        return value> . 5; 
   }); 
   Print (F); // to true 

}

 

5)every

Each of which satisfies the condition returns true, false otherwise

main void () { 
    List myList = [1,3,4,5,7,8,9]; 
    each var f = myList.every ((value) {// returns true condition is satisfied otherwise to false 
       return value> . 5; 
    }); 
    Print (F); // to false 

}

  

For Set

void main(){
      var s=new Set();

      s.addAll([1,222,333]);

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

}

 

For map

void main(){
       Map person={
          "name":"张三",
          "age":20
        };

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

}  

  

  

  

  

  

  

  

  

  

  

  

  

  

 

 

 

Guess you like

Origin www.cnblogs.com/The-Chao/p/11906198.html