Dart中表示布尔类型的数据用bool声明 它只有true(真)和false(假)两个值。
var numberStr print(numberStr 0); //输出结果为true
bool flag false;//布尔值只有true和false bool flag1 1 2; print(flag); print(flag1); I/flutter ( 2339): false I/flutter ( 2339): false4.List(列表类型)
Dart语言中的数组(Array)就是List(列表)对象。
List list [ 中国 , 日本 , 美国 , 加拿大 print(list.length); //输出 4 print(list[0] list[1]);//输出 中国 日本
List类型常用方法
//定义一个数组 由于数组元素全部是字符串 相当于定义了String类型的数组 //每一个数组会包含0个或多个数组元素 每个数组元素的下标从0开始 //可以根据下标取数组元素的值 List countrys [ 中国 , 美国 , 日本 ]; print(countrys); print(countrys.length); print(countrys[1]);//取下标为1的数组元素 countrys[2] 250;//对数组元素重新赋值 print(countrys); //将 朝鲜 加入到数组中 countrys.add( 朝鲜 ); print(countrys); //从数组中删除 美国 countrys.remove( 美国 ); print(countrys); I/flutter ( 2339): [中国, 美国, 日本] I/flutter ( 2339): 3 I/flutter ( 2339): 美国 I/flutter ( 2339): [中国, 美国, 250] I/flutter ( 2339): [中国, 美国, 250, 朝鲜] I/flutter ( 2339): [中国, 250, 朝鲜]
List countrys [ 中国 , 美国 , 日本 ]; //将 朝鲜 插入到中国与美国之间 countrys.insert(1, 朝鲜 ); print(countrys); //在数组中查找日本 print(countrys.indexOf( 日本 ));//从数组中找到指定元素并返回该元素在指定数组中的下标 print(countrys.contains( 朝鲜 ));//判断数组中是否包含指定元素 print(countrys.join( | ));//将数组元素转化为由指定符号分割的字符串 I/flutter ( 2339): [中国, 朝鲜, 美国, 日本] I/flutter ( 2339): 3 I/flutter ( 2339): true I/flutter ( 2339): 中国|朝鲜|美国|日本5.Set(集合类型)
Dart语言中用Set表示一个元素唯一却无序的集合 不能通过索引下标获取数据元素。
Set set {1,2,3,4,5,6,6,8,1};
print(set); //输出 1 2 3 4 5 6 8 重复的元素不输出
//创建一个可添加任何类型元素的集合
var a new Set();
a.add( java //String类型
a.add(12.9); //double类型
a.add(true); //bool类型
a.add(100); //int类型
a.add([1,2,3,4]); //List类型
print(a);
I/flutter ( 2339): {java, 12.9, true, 100, [1, 2, 3, 4]}
List myList [ 香蕉 , 苹果 , 西瓜 , 香蕉 , 苹果 , 香蕉 , 苹果
print(myList.toSet()); //输出 {香蕉,苹果,西瓜}
var mySet new Set();
mySet.addAll(myList); //数组元素赋值给集合
print(mySet); //输出 {香蕉,苹果,西瓜}
print(mySet.toList); //输出 {香蕉,苹果,西瓜}
//创建一个只能存放String类型的集合
var a Set String ();//泛型
//List也同样可以定义List String lists
//利用集合去除数组中的重复元素
List lists [1,2,3,4,1,5,3];
print( lists:$lists );
Set ss lists.toSet();//将数组转换为集合
print(ss);
lists ss.toList();//将集合转换为数组
print( lists:$lists );
I/flutter ( 2339): lists:[1, 2, 3, 4, 1, 5, 3]
I/flutter ( 2339): {1, 2, 3, 4, 5}
I/flutter ( 2339): lists:[1, 2, 3, 4, 5]
6.Map(映射类型)
Dart语言中Map是一个简单的键值对(key-value) 映射中的键和值可以是任何类型 映射是动态集合 即Map可以在运行时增长和缩短。
//直接在声明中初始化键值对
Map companys { first : 阿里巴巴 , second : 腾讯 , fifth : 百度
print(companys); //输出 {first: 阿里巴巴,second: 腾讯,fifth: 百度}
Map companys new Map();
companys[ first ] 阿里巴巴 //输出键为 first 的值
companys[ second ] 腾讯
companys[ fifth ] 百度
print(companys); //输出 {first: 阿里巴巴, second: 腾讯, fifth: 百度}
final fruitConstantMap const{2: apple ,10: orange ,18: banana
print(fruitConstantMap); //输出 {2: apple, 10: orange, 18: banana}
print(fruitConstantMap[10]); //输出 orange
Map类型常用方法
Map companys { no1 : 华为 , no2 : 腾讯 , no3 : 阿里 };
print(companys);
//输出键为no2的值
print(companys[ no2 ]);
//输出所有键对应的值
print(companys.values);
//输出所有键名
print(companys.keys);
//添加键值对到映射中
Map map { no4 : 中兴 };
companys.addAll(map);
print(companys);
//删除指定键对应的键值对
companys.remove( no2 );
print(companys);
//判断映射中是否包含指定值
print(companys.containsValue( 阿里 ));
//判断映射中是否包含指定键
print(companys.containsKey( no2 ));
I/flutter ( 2339): {no1: 华为, no2: 腾讯, no3: 阿里}
I/flutter ( 2339): 腾讯
I/flutter ( 2339): (华为, 腾讯, 阿里)
I/flutter ( 2339): (no1, no2, no3)
I/flutter ( 2339): {no1: 华为, no2: 腾讯, no3: 阿里, no4: 中兴}
I/flutter ( 2339): {no1: 华为, no3: 阿里, no4: 中兴}
I/flutter ( 2339): true
I/flutter ( 2339): false



