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

dart语言学习(五)— List列表

程序员文章站 2022-05-02 13:12:31
...
  • List(数组)创建
创建List:var list=[1,2,3];

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

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


常用操作:[],length;add(),insert();remove(),clear();indexOf(),lastIndexOf();

	  sort(),sublist();shuffle(),asMap(),forEach();

		void main() {
		  var list1 = [1, 2, 3, "Dart", true];
		  print(list1);
		  print(list1[1]);
		  list1[1] = "huangxiaoguo";
		  print(list1);
		
		  //不可变的List
		  var list2 = const [1, 2, 3];
		  var list3 = new List();
		
		  //常用操作
		  var list = ["Hello", "Dart"];
		  print(list[1]);
		  print(list.length);
		
		  list.add("New");
		  print(list);
		
		  list.insert(1, "aaa");
		  print(list);
		
		  list.remove("aaa");
		  print(list);
		
		  print(list.indexOf("Dart"));
		
		  list.sort();
		  print(list);
		
		  print(list.sublist(2));
		
		  list.forEach(print);
		
		  list.clear();
		  print(list);
		}
[1, 2, 3, Dart, true]
2
[1, huangxiaoguo, 3, Dart, true]
Dart
2
[Hello, Dart, New]
[Hello, aaa, Dart, New]
[Hello, Dart, New]
1
[Dart, Hello, New]
[New]
Dart
Hello
New
[]


相关标签: dart