Dart learning notes List Set Map details

List Set Map details Properties and methods commonly used in List Common attributes: lengthreversed flip (note that this...
List Set Map details

Properties and methods commonly used in List

Common attributes:
  • length
  • reversed flip (note that this is an attribute whose value is a string)
  • Is isEmpty empty
  • Is isNotEmpty not empty
Common methods:
  • List.filled(num,value) (static method, create a new list with num values)
  • add adds an array element
  • addAll(List) splice array
  • indexOf finds, returns index if found, and returns - 1 if not
  • remove deletes the specific value passed in
  • removeAt deletes the incoming index value
  • fillRange(start, end, value) replaces the elements of the specified range (including the start index and excluding the end index) with value, and the return value is void
  • insert(index,value) inserts after the specified element
  • insertAll(index,List) inserts a List after the specified element
  • toList() convert other types to List
  • The join(separator) List is converted into a string, and each element is separated by a separator
  • The split(separator) string is converted into a List. By default, each element is separated by a separator
  • forEach traversal
  • map
  • where
  • any
  • every
Detailed explanation of difficulties

map()

The map method accepts an anonymous function as a parameter, executes the anonymous method with value as a parameter each iteration, puts the return value into a new iteratable object, and finally returns an iteratable object
In the map object of the list, the passed in parameter value defaults to the value of the element

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

where()

Similar to the map method, but returns all values that meet the conditions

List myList = [1,3,4,5,7,9]; var newList=myList.where((value){ return value>4; });
a sudden whim to

What happens if you replace where with map in the code of the where example?

List myList = [1,3,4,5,7,9]; var newList=myList.map((value){ return value>4; }); print(newList);

The return value we get is:

(false, false, false, true, true, true)

It is concluded that the return value of map is the collection of return values of each anonymous method
where the return value is a collection of elements whose anonymous method return value is true

any()

Check whether there are qualified elements in the collection. If so, return true

List myList = [1,3,4,5,7,9]; var flag=myList.any((value){ return value>7; });

every()

Checks whether each element in the collection meets the conditions. If it meets the conditions, it returns true

Code slightly

Set

  • Its main function is to remove the duplicate contents of the array

  • Set is a collection without book order and cannot be repeated. It cannot obtain values through index

Using set to remove duplication

List myList=["1","2","3","1","2","3"]; var set = new Set(); set.addAll(myList);

Prints all elements in the set

var s = new Set(); s.addAll([1,2,3]); s.forEach((element)=>print(element));

Map maps unordered key value pairs

Two ways to initialize a map

var person={ "name":"bob", "age":20 }; var m = new Map(); m["name"]="jenny"; m["age"]=21
Common attributes:
  • keys gets all key values, often followed by a toList method
  • Values gets all the value values
  • Is isEmpty empty
  • Is isNotEmpty not empty
Common methods:
  • remove(key) deletes the specified k-v
  • addAll({...}) merges the mapping and adds the parameter data to the map
  • containsValue view the value in the map and return true/false
  • forEach
  • map
  • where
  • any
  • every

Loop printing key value pairs in map

var person={ "name":"bob", "age":20, "sex":"male" }; person.forEach((key, value) { print("$key: $value"); });

Supplement: advanced cycle

Enhanced for loop
for(var item in list){ print(item); }
.forEach
list.forEach((value) { print("$value"); });

For more details, please refer to the api documentation (English)
https://api.dart.cn/stable/2.14.2/dart-core/List-class.html

29 September 2021, 16:58 | Views: 7499

Add new comment

For adding a comment, please log in
or create account

0 comments