# 簡介 陣列可以用來儲存大量資料若是數量很多或是不確定筆數可以使用陣列。 `資料型態` `名稱` `[參數]` ```cpp int arr[10]; double db[20]; bool check[30]; ``` 陣列長度可以是變數,需要注意那個變數必須為正整數,而這樣的陣列被稱為可變長度陣列。 ```cpp= int n = 5; int arr[n]; //arr的大小為5。 ``` :::info :::spoiler 靜態陣列 如果我們在`int main()`之上先宣告了陣列,他會在編譯時就先開好,因此可以開較大。(跟全域變數的宣告方式一樣) ```cpp= int arr[100]; int main() { //code } ``` 例如在DDJ,可變長度陣列無法開到$10^7$,但靜態陣列可以。 ::: # 初始化 初始化時沒定義的元素就跟全域變數的初始值一樣,`int`陣列會初始化成`0`,`float`陣列為`0.0`,`bool`陣列為`false`,`char`陣列為`'\0'`。 ```cpp= int arr1[5] = {1, 2, 3, 4, 5}; int arr2[5] = {1, 2, 3}; //arr2 = {1 2 3 0 0} int arr3[5] = {}; //全部為0 ``` # 存取 可以把陣列像想像成一排櫃子,每個櫃子就放一個東西,而每個櫃子裡的元素可直接使用。 只能對陣列的某一項進行操作,而無法對整個陣列做操作。 :::warning **注意!第一項為arr[0]!(0-based),非arr[1]** 所以 `int arr[10]` 的可使用的範圍為 `arr[0]~arr[9]` ,左閉右開[0, n),包含0不含n。 ::: ```cpp= #include <iostream> using namespace std; int main() { int arr[5] = {5, 3, 4, 2, 1}; } ``` ![](https://hackmd.io/_uploads/Sk1r8Yy23.png =60%x) ## 手動賦值 ```cpp= int arr[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3; int sum = arr[0] + arr[1] + arr[2]; cout << sum; //輸出6 ``` ## 迴圈賦值 ```cpp= #include <iostream> using namespace std; int arr[30]; int main() { int n; cin >> n; //輸入陣列大小 for(int i = 0; i < n; i++){ cin >> arr[i]; } int sum = 0; for(int i = 0; i < n; i++){ sum += arr[i]; } cout << sum; } ``` # 二維陣列 如果說一維陣列是一排櫃子,那二維陣列就是增加了好幾排櫃子。 ```cpp= int arr1[3][4]; ``` `arr[3][4]`也就是有3列、4行的表格,前面是縱坐標,後面是橫座標。 ![](https://hackmd.io/_uploads/rJMOCK5F3.png) :::danger 橫為行( - ),直為列( l ) 。 ```cpp= int arr[2][3] /* * 0 0 0 * 0 0 0 * * 共有兩行,三列。 * / ``` ::: ### 直接初始化 ```cpp= //初始化成指定值。 int arr[2][3] = {1,2,3,4,5,6}; int arr[2][3] = {{1,2,3},{4,5,6}}; //由於code的自由性 推薦打成。 int arr[2][3] = { {1, 2, 3}, {4, 5, 6} } //如果要全都初始化成0。 int arr[2][3] = {}; ``` ### 使用迴圈初始化 ```cpp= int arr[100][100]; for(int i = 0; i < 100; i++){ for(int j = 0; j < 100; j++){ arr[i][j] = 0; } } ``` ### 使用迴圈輸入 ```cpp= int arr[100][100]; for(int i = 0; i < 100; i++){ for(int j = 0; j < 100; j++){ cin >> arr[i][j]; } } ``` # 例題 [a069: I Love You 3000](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a069) [a156: Mercury的願望](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a156) [a244: pD Minecraft 1.15 嗡嗡蜂群更新 上線啦!!!!](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a244) [a439: P1 人力分配](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a439) [a685: D. 拿餐大作戰](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a685) [a699: 奇數項乘積](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a699) [a707: 期中考加油](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a707) [a829: 手機交上來囉!!!](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a829) [a830: 排雷](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a830) [a837: sort?別開玩笑了(1)](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a837) [a848: PainTako](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a848) [a959: P1 交叉比對](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a959) [a984: We Were Both Children0](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=a984) [b017: 遍地錢錢!(easy version)](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=b017) [b052: pB:危機-機器人](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=b052) [b053: pC:分類-零件回收](https://dandanjudge.fdhs.tyc.edu.tw/ShowProblem?problemid=b053) ###### 都看到這了難道不按個愛心支持一下嗎?