一、創建list 創建List : var list = [1,2,3,"Dart",true]; 創建不可變List : var list = const [1,2,3,"Dart",true]; 構造創建:var list3 = new List(); 二、常用操作 [],length,add( ...
一、創建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 []