###### tags: `語法` # 集合 集合 1.陣列(Array) 使用整數索引值(index)來存取內容,索引值從0開始到陣列數量減1為止。 2.合集(Set) 只能判斷某個值是否在集合內,且無法透過索引值(index)或鍵值(Key)來存取集合內容。但可以進行集合運算,例如:交集、聯集、對稱差集、差集。 3.字典(Dictionary) 透過鍵(Key)值來存取內容,鍵值不限定整數,可以是任何型態。 ![](https://i.imgur.com/DftH1Xs.png) # Array 陣列 宣告 ```kotlin= var someInts = [Int]() //宣吿一個array var someInts: [Int] = [1,2] //宣告一個array並初始化 var someInts = [1,2] //可省略型態 ``` 一些array的方法 ```kotlin= 新增 someInts.append(3) //在array尾端加入int 3 someInts += [5] //在array尾端加入int5 插物 someInts.insert(3, 5) // 3==>要插的東西,5==>要插的位置 //要把元素插入到特定的索引位置 var threeDoubles = Array(repeating: 0.0, count: 3) //repeating:放入集合內容, count: 要放幾個 移除 someInts.remove(at: 0) //移除array第0個資料 someInts.removeLast() //移除最後一筆資料 包含 someInts.contains(0) // 上面的都謝會是Bool,如果array裡有該資料會是true,否則是false 將兩個array合併 var sixDoubles = threeDoubles + anotherThreeDoubles // sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5] someInts[0] = 8 // 把第0個值改為8 ``` 以用 for-in循環來遍歷整個array中值的合集 ```kotlin= for item in someInts { print(item) // 印出所有數字 } for (index, value) in shoppingList.enumerated() { print("Item \(index + 1): \(value)") } //如果你需要每个元素以及值的整数索引,使用 enumerated()方法来遍历数组。 // Item 1: Six eggs // Item 2: Milk // Item 3: Flour // Item 4: Baking Powder // Item 5: Bananas ``` # set 合集 Set 維護一組不重複的資料,資料不依序儲存。 宣告 ```kotlin= var set1 = Set<Int>() var set2: Set<Int> = Set() var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"] ``` 方法 intersect()傳回兩組 Set 交集的元素。 union()傳回兩組 Set 聯集的元素。 exclusiveOr()傳回兩組 Set 不交集的元素。 subtract()傳回set1內不含set2的元素。 ![](https://i.imgur.com/q9dldKn.png) 方法的使用 ```kotlin= let oddDigits: Set = [1, 3, 5, 7, 9] let evenDigits: Set = [0, 2, 4, 6, 8] let singleDigitPrimeNumbers: Set = [2, 3, 5, 7] sorted()//小到大 oddDigits.union(evenDigits).sorted() // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 沒有sort oddDigits.intersection(evenDigits).sorted() // [] oddDigits.subtracting(singleDigitPrimeNumbers).sorted() // [1, 9] oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted() // [1, 2, 9] ``` ![](https://i.imgur.com/hN4A6uU.png) # Dictionary 字典 宣告 ```kotlin= var namesOfIntegers = [Int: String]() var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"] ``` 添加新資料 ```kotlin= airports["LHR"] = "London" airports.updateValue("Dublin Airport", forKey: "DUB") ``` 可以通过访问字典的 keys和 values属性来取回可遍历的字典的键或值的集合: ```kotlin= for airportCode in airports.keys { print("Airport code: \(airportCode)") } // Airport code: YYZ // Airport code: LHR for airportName in airports.values { print("Airport name: \(airportName)") } // Airport name: Toronto Pearson // Airport name: London Heathrow for airportCode in airports.keys { print("Airport code: \(airportCode)") } // Airport code: YYZ // Airport code: LHR ``` 以keys或 values来初始化一個新的array: ```kotlin= let airportCodes = [String](airports.keys) // airportCodes is ["YYZ", "LHR"] let airportNames = [String](airports.values) // airportNames is ["Toronto Pearson", "London Heathrow"] ``` Swift 的 Dictionary類型是無序的。要以特定的顺序遍历字典的键或值,使用键或值的 sorted()方法。