Dart语言学习(六) Dart 列表List数组

Dart语言学习(六) Dart 列表List数组

一、创建list

创建List : var list = [1,2,3,"Dart",true];

创建不可变List : var list = const [1,2,3,"Dart",true];

构造创建:var list3 = new List();

二、常用操作

[],length,add(),insert(),remove(),clear()
indexOf(),lastIndexOf()
排序sort(),子数组 sublist()
打乱shuffle(),转为Map: asMap(),遍历forEach()
实例代码如下:
  //创建list
  var list1 = [1,2,3,"Dart",true];
  print(list1);
  //下标索引打印对于元素
  print(list1[2]);
  list1[1] = "Hello";
  print(list1);

  //创建不可变元素
  var list2 = const [1,2,3];
  //  list2[0] = 5;  The value of the local variable "list2" isn"t used.

  //构造方法创建
  var list3 = new List();
  print(list3);

  var list = ["hello","dart"];
  print(list.length);
  list.add("New");
  print(list);

  //插入在index为1的位置
  list.insert(1, "Java");
  print(list);

  //移除元素
  list.remove("Java");
  print(list);

  //元素对应的位置
  print(list.indexOf("dart1"));
  //排序
  list.sort();
  print(list);
  //截取从1开始
  print(list.sublist(1));

  list.forEach(print);

  list.clear();
  print(list);
输出:
[1, 2, 3, Dart, true]
3
[1, Hello, 3, Dart, true]
[]
2
[hello, dart, New]
[hello, Java, dart, New]
[hello, dart, New]
-1
[New, dart, hello]
[dart, hello]
New
dart
hello
[]