本文主要介绍Dart 中的几种基本数据类型:数值型-Number、布尔型-boolean、字符串-String、列表-List、键值对-Map,其他类型-Runes、Symbols,以及类型间的转换。
-
数值型
(int、double)
num n1 = 50;
num n2 = 20.3;
int a = 30;
double b = 30.3;
运算符: +、 - 、* 、/ 、 ~/ 、 %
常用属性: isNaN、isEven、isOdd
常用方法:abs()、round()、floorl()、ceil()、toInt()、toDouble()
int a = 40;
double b = 2.2;
var c = a + b; //加
var d = a - b; //减
var e = a * b; //乘
var f = a / b; //除
var g = 8 % 5; //求余
var h = 8 ~/ 3; //取整
print("${c}, ${d}, ${e}, ${f}, ${g}, ${h}");
//42.2, 37.8, 88.0, 18.18181818181818, 3, 2
var l1 = (-1).abs(); // 取绝对值
var l2 = 4.3.round(); // 取整 四舍五入
var l3 = 4.6.round(); // 取整 四舍五入
var l4 = 4.9.floor(); // 向下取整
var l5 = 4.2.ceil(); // 向上取整
var l6 = 3.toDouble(); // 转为Double
var l7 = 3.3333.toInt(); //转为 Int
print("${l1}, ${l2}, ${l3}, ${l4}, ${l5}, ${l6}, ${l7}");
//1, 4, 5, 4, 5, 3.0, 3
-
布尔型
(bool)
bool bt = true;
bool bf = false;
// 条件语句
if (bt) {
print("bt is true");
} else {
print("bt is false");
}
-
字符串
(String)
var st1 = '你好';
String str2 = "少年";
常见操作:字符串拼接,字符串截取,字符串分割等等
//字符串拼接
var pjStr = "你好" + "少年";
print(pjStr);
var aa = "aa,";
var bb = "bb,";
var cc = "cc,";
var total = "aa,${bb}cc";
print(total);
print(aa + bb + cc);
//字符串截取
var a1 = total.substring(2);
print(a1);
var a2 = total.substring(0, 3);
print(a2);
//字符串分割 字符串 转 数组
var a3 = total.split(",");
print(a3);
// 数组 转 字符串
var arr = ["a", "b", "c"];
print(arr.join(","));
-
数组
(List)
数组的定义及初始化:
如果不指定数组的存储类型,则可以添加任意类型(有点像swift里的元组),如果指定了数组的类型,则只能添加该中类型。
// 创建一个空数组
var emptyArr = new List();//emptyArr 类型为: List<dynamic>
var emptyArr2 = new List<String>();//emptyArr2 类型为: List<String>
// 不指定类型,则可以添加任意类型(有点像swift里的元组)
List arr = [1, 2, 3, 4, 5];
arr.add(6);
arr.add("value");
print(arr);
//[1, 2, 3, 4, 5, 6, value]
List<int> intArr = [1, 2, 3];
intArr.add(4);
// intArr.add("1"); 如果指定了数组的类型,则不能再添加其他类型
print(intArr);
//[1, 2, 3, 4]
注意:var arr2 = [1, 2, 3];
与List arr = [1, 2, 3];
的区别
List arr = [1, 2, 3];
//arr 类型为:List<dynamic>
var arr2 = [1, 2, 3];
//dart语言特性会推断 arr2 为List<int>
类型
数组的常见操作:
判断数组是否为空:isEmpty
获取数组长度:length
- 增删改查遍历等操作:
//新增
var names = new List<String>();
names.add("Uzi");
names.add("JackLove");
names.addAll(["Ming", "ClearLove"]);
print(names);//[Uzi, JackLove, Ming, ClearLove]
//删除
names.removeAt(1);
print(names);//[Uzi, Ming, ClearLove]
//修改
names[2] = "ClearLove7";
print(names);//[Uzi, Ming, ClearLove7]
//查询
var name = names[1];
var index = names.indexOf(name);
print("${name} at ${index}");//Ming at 1
//逆序
var revNames = names.reversed;
print(revNames);//(ClearLove7, Ming, Uzi)
//遍历
names.forEach((element) {
print(element);
});/*Uzi Ming ClearLove7 */
for (var obj in names) {
print(obj);
}/*Uzi Ming ClearLove7 */
-
字典
(Map)
var dict = {"name": "zhangsan", "age": 22, "address": "shanghai"};
print(dict);
print(dict.keys);//(name, age, address)
print(dict.values);//(zhangsan, 22, shanghai)
var name = dict["name"];
print(name);//zhangsan
dict["name"] = "lisi"; //已有的key,为修改
dict["sex"] = "男"; // 没有的key,为新增
print(dict);//{name: lisi, age: 22, address: shanghai, sex: 男}
ps:
更多字符串操作可参考: Dart之字符串(String)的相关方法总结
更多数组操作可参考: Dart之数组(List)的相关方法总结
更多字典操作可参考: Dart之字典(Map)的相关方法总结
网友评论