DAY 3
2
Software Development

## 專業知識 - 多維陣列 Multidimensional Arrays

### 一維陣列 One-Dimensional Array

``````  var box = new string[3] = {"維他命D","葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C"};
``````

### 二維陣列 Two-Dimensional Array

• 吃保健食品有`3 個時段`：上午、中午與下午
• 一星期有`7 天`
• `3 個時段 * 7 天 = 21 格`
所以我們可以得知，小美必須購買21格的盒子才符合他的需求

• 需要 3 列 Row (每天有 3 個時段)
• 需要 7 行 Column (一週 7 天)
• 會得到`3 * 7 的二維陣列`，得到 21 個記憶體空間

``````var box = new string[3,7] { //會得到 3 * 7 共 21 個記憶體空間
//↓週一~週日
{"維他命D","葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C"}, //上午
{"葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C","維他命D"}, //中午
{"蔓越莓錠","B群","綜合維他命","益生菌","維他命C","維他命D","葉黃素"}}; //下午
``````

`存取變數：變數名稱[列索引號, 行索引號]`
`存取變數：變數名稱[列索引號, 行索引號]`
`存取變數：變數名稱[列索引號, 行索引號]`

``````var cards = new int[4,13]
``````

### 三維陣列 Three-Dimensional Array

• 一個月有 4 週
• 每天有 3 個時段
• 一週 7 天
• `會得到 4 * 3 * 7 的三維陣列`
``````var box = new string[4,3,7] {
{//第一週，索引0
{"維他命D","葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C"}, //早上
{"葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C","維他命D"}, //中午
{"蔓越莓錠","B群","綜合維他命","益生菌","維他命C","維他命D","葉黃素"}  //下午
},
{//第二週，索引1
{"維他命D","益生菌","蔓越莓錠","B群","綜合維他命","葉黃素","維他命C"}, //早上
{"維他命C","蔓越莓錠","B群","綜合維他命","益生菌","葉黃素","維他命D"}, //中午
{"維他命D","綜合維他命","B群","益生菌","維他命C","蔓越莓錠","葉黃素"}  //下午
},
{//第三週，索引2
{"維他命D","葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C"}, //早上
{"葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C","維他命D"}, //中午
{"蔓越莓錠","B群","綜合維他命","益生菌","維他命C","維他命D","葉黃素"}  //下午
},
{//第四週，索引3
//週一索引0, 週2索引1，以此類推，週日索引6
{"維他命D","葉黃素","蔓越莓錠","B群","綜合維他命","益生菌","維他命C"}, //早上 0
{"葉黃素","益生菌","B群","綜合維他命","蔓越莓錠","維他命C","維他命D"}, //中午 1
{"蔓越莓錠","B群","葉黃素","益生菌","維他命C","維他命D","綜合維他命"}  //下午 2
}};
``````

1. 多位學生
2. 多個考試科目
3. 多個月份

1. 多位學生
2. 多個考試科目
3. 多個月份
4. 多個年度
這樣就是 4 維陣列囉

## 今日謎題

``````Maps[2,2]
Maps[0,1]
Maps[3,0]
Maps[1,3]
Maps[4,0]
Maps[5,0]
Maps[5,3]
Maps[4,1]
Maps[7,1]
``````

## 昨日謎題

1. 陣列索引初始位置從 0 開始
2. 取值：變數名稱[索引位置]
3. 更新數值：變數名稱[索引位置] = 要存放的值