2014年3月17日 星期一

List/Array 放大鏡

在 Dart 中是將 List(列表)和 Array(陣列) 合併為同一資料型別 List 來看待,而針對 List 的操作主要多是:

  • 初始化
  • 索引值及取得指定位置上的元素
  • 插入元素
  • 移除元素


一、初始化
List 基本的宣告方式,在內建資料型別中的 lists 列表/Array陣列 中已有基本介紹,在此將介紹更進階的初始化應用。

指定資料型別的 List:
在宣告一般的 List 時通常可以不用特別指定加入 List 中元素的資料型別,例如:

var colorList = ["red", "green", "blue", "white", "black"];
var numList = [10, 20, 30, 40, 80, 120];

甚至還可以將不同資料型別的元素放在同一個 List 中:

var elementList = ["red", 50, false, 0.321]; //資料型別依序是:字串、整數、布林值、浮點數

但在某型情況下,希望 List 只能接受指定資料型別的元素才能被加入時,則需要如下的宣告:

var 列表變數名稱= new List<指定的資料型別>();
例:


var colorList = new List<String>();//只能接受 String 資料型別的元素加入在 colorList 中
colorList.addAll(["red", "green", "blue", "white", "black"]);
colorList.add(12); //加入整數,執行時會發生資料型別錯誤


除了單純的資料外, List 也可以加入較複雜的資料,例如 Map:

 var studentList = [
//每一筆 Map 相當於是一個元素
                  {"name":"John", "score":80},
                  {"name":"Marry", "score":87},
                  {"name":"Sean", "score":75},
                  {"name":"Lucy", "score":95}                  
                  ];

 int total = 0;
//列出所有學生的分數,並計算分數總合。
 for(Map student in studentList ){
   print("${student ['name']}'s score is ${student ['score']}");
   total+=student ["score"];
 }
//印出平均值
 print(">> Average is ${total/studentList .length}");

執行後輸出:
John's score is 80
Marry's score is 87
Sean's score is 75
Lucy's score is 95
>> Average is 84.25


二、索引值及取得指定位置上的元素

List 的特色就是在其中的元素是依序排列,而且可以依照每個元素所相對應的一個索引值(index),對列表中的元素進行取值、設值、插入、刪除等動作。而列表的索列值是從 0 為始一直到列表長度值-1 為止。

在針對 List 的索引操作中,最常發生問題的是索引值的溢位,而正確的索引值應該是:

0<= Index <= 列表長度-1

列表索引值使用語法:
列表名稱[index]

列表長度值:
列表名稱.length

例:
//宣告有總共有5個元素的顏色列表
var colorList = ["red", "green", "blue", "black", "white"];
//印出 colorList 目前的長度
print("The length of colorList is ${colorList.length}");
print(colorList[0]); //輸出==> red
print(colorList[1]); //輸出==> green
print(colorList[2]); //輸出==> blue
print(colorList[3]); //輸出==> black
print(colorList[4]); //輸出==> white
print(colorList[5]); //執行時報錯,索引值5相對於在 colorList 的第6個元素,但 colorList 只有總共5個元素。

若要重新設定在某個索引值上的元素,只要需設定如下:

列表名稱[index]=新元素值

例:
colorList[1]="gold";

三、加入/插入元素

加入元素:
除了在宣告列表時就設定的初始值,也能在宣告之後另外加入新的元素:

語法:
  • 列表名稱.add(元素); //加入一個元素到列表最後
  • 列表名稱.addAll([元素1, 元素2, ...元素n]); //加入多個元素到列表最後
  • 列表名稱.addAll(另一個列表); 將另一個列表內容加入原列表最後
例:


//宣告一個空列表

var colorList1 = [];

//第一次印出目前列表長度

print("1> cololist.length=${colorList1.length}");

//一次加入多個元素(5個)
colorList1.addAll(["red", "green", "blue", "black", "white"]);
//第二次印出目前列表長度
print("2> cololist.length=${colorList1.length}");

//一次加入單一個元素
colorList1.add("gray");
//第三次印出目前列表長度
print("3> cololist.length=${colorList1.length}");

//一次加入3個顏色
colorList1.addAll(["orange", "purple", "brown"]);
//第四次印出目前列表長度
print("4> cololist.length=${colorList1.length}");

//宣告另一個色彩列表
var colorList2 = ["gold", "indigo", "lime"];
//將 coloList2 的內容加入 colorList1
colorList1.addAll(colorList2);
//第五次印出目前列表長度
print("5> cololist.length=${colorList1.length}");

//印出最後列表內容
print(colorList1);

輸出:
1> cololist.length=0
2> cololist.length=5
3> cololist.length=6
4> cololist.length=9
5> cololist.length=12
[red, green, blue, black, white, gray, orange, purple, brown, gold, indigo, lime]

插入元素:

有別於依序加入元素的方法,列表還可以在指定索引值的地方直接插入一或多個元素,而原本位於指定索引值的元素則會被擠到新加入元素之後。

語法:

  • 列表名稱.insert(index, 元素); //在指定索引值的地方插入一個元素
  • 列表名稱.insertAll(index, [元素1, 元素2, ...元素n]); //在指定索引值的地方插入多個元素
  • 列表名稱.insertAll(index,另一個列表); 在指定索引值的地方插入另一個列表
例:


var numList1 = [0, 1, 2, 3, 4, 5, 6];
print("1>$numList1");

//在index = 5 的地方插入一個元素
numList1.insert(5 , 10 );
print("2>$numList1");

//在 index = 4 的地方插入3個多元
numList1.insertAll(4, [20, 30, 40]);
print("3>$numList1");

//宣告另一個列表
var numList2=[100, 200, 300];
//在 index = 2 的地方插入另一個列表的所有元素
numList1.insertAll(2, numList2);
print("4>$numList1");

輸出:
1>[0, 1, 2, 3, 4, 5, 6]
2>[0, 1, 2, 3, 4, 10, 5, 6]
3>[0, 1, 2, 3, 20, 30, 40, 4, 10, 5, 6]
4>[0, 1, 100, 200, 300, 2, 3, 20, 30, 40, 4, 10, 5, 6]

四、移除元素

語法:
  • 列表名稱.remove(元素); //移除指定的元素,若列表中同時存在多個相同的指定元素,則只移除第一個。
  • 列表名稱.removeAt(index); //移除在指定 index 位置上的元素。
  • 列表名稱.removeLast(); //移除在列表中的最後一個元素。
  • 列表名稱.removeRange(startIndex, endIndex); //移除從 startIndex 開始到 endIndex之前的所有元素,需注意 index 必需在有效的範圍內,否則會被報錯。
例:

var numList1 = [0, 1, 2, 3, 4, 5, 6];
print("1>$numList1");

//移除元素 2
numList1.remove(2);
print("2>$numList1");

//移除在 index=2 的元素
numList1.removeAt(2);
print("3>$numList1");

//移除列表最後的元素
numList1.removeLast();
print("4>$numList1");

//移除從 index=0 開始到 index=3 之前所有的元素
numList1.removeRange(0, 3);
print("5>$numList1");

輸出:
1>[0, 1, 2, 3, 4, 5, 6]
2>[0, 1, 3, 4, 5, 6]
3>[0, 1, 4, 5, 6]
4>[0, 1, 4, 5]
5>[5]

更多有關於 List 的操作可參考:Dart API Reference - List 的 Methods。



1 則留言:

  1. Wynn Palace casino - Travelocity - JamBase
    Book 서산 출장안마 Wynn 서울특별 출장마사지 Palace 계룡 출장안마 and enjoy no hotel booking 거제 출장마사지 fees. Encore at Wynn Las Vegas, the resort that features a full casino, a spa, 보령 출장마사지 a nightclub and a spa.

    回覆刪除