Dart语言学习日记(二)

基本语法

数据类型

3.Boolean(布尔类型)

Dart中表示布尔类型的数据用bool声明,它只有true(真)和false(假)两个值。

var numberStr = 0;
print(numberStr <= 0);
//输出结果为true
bool flag = false;//布尔值只有true和false
bool flag1 = 1>2;
print(flag);
print(flag1);

输出:
I/flutter ( 2339): false
I/flutter ( 2339): false

4.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuoYaNiiii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值