欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dart语言学习(六)— Map

程序员文章站 2022-05-02 13:19:19
...

创建Map:var language={“first”:“Dart”,“two”:“java”};

创建不可变Map:var language=const{“first”:“Dart”,“two”:“java”};

构造创建:var language=new Map();

常用操作:

[],length;isEmpty(),isNotEmpty();keys,values;containsKey(),containsValue();

 remove();forEach();asMap();


void main() {
  var map1 = {"first": "Dart", "two": "java", 1: true};
  print(map1);
  print(map1["first"]);
  print(map1[1]);

  map1[1] = false;
  print(map1);

  var language = const {"first": "Dart", "two": "java"};
  var language1 = new Map();

  //常用操作
  var map = {"first": "Dart", "two": "java", "three": "js"};
  print(map.length);

  print(map.keys);
  print(map.values);

  print(map.containsKey("first"));
  print(map.containsValue("python"));

  map.remove("two");
  print(map);

  map.forEach(f);

  var list=["1","2","3"];
  print(list.asMap());
}

void f(key, value) {
  print("key=$key,value=$value");
}


{first: Dart, two: java, 1: true}
Dart
true
{first: Dart, two: java, 1: false}
3
(first, two, three)
(Dart, java, js)
true
false
{first: Dart, three: js}
key=first,value=Dart
key=three,value=js
{0: 1, 1: 2, 2: 3}


相关标签: dart