陣列是有序列的集合,集合內的元素必須是同一種類型的。
var arrayNumber = [1, 2, 5, 10]
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
arrayString[2] = "MoMo"
print(arrayString)
印出結果:
["Kevin", "Yoyo", "MoMo", "Sam"]
append
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
arrayString.append("MoMo")
print(arrayString)
印出結果:
["Kevin", "Yoyo", "Stan", "Sam", "MoMo"]
remove
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
arrayString.remove(at: 1)
print(arrayString)
印出結果:
["Kevin", "Stan", "Sam"]
count
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
print(arrayString.count)
印出結果:
4
1. 使用 forEach
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
arrayString.forEach { item in
print(item)
}
印出結果:
Kevin
Yoyo
Stan
Sam
2. 使用 for-in
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
for item in arrayString {
print(item)
}
印出結果:
Kevin
Yoyo
Stan
Sam
如果想取得該元素在陣列中的 index,可以使用 enumerated()
,它會回傳 index 及該元素的序列資料。
我們分別在 forEach 及 for-in 中使用 enumerated():
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
arrayString.enumerated().forEach { index, item in
print("[forEach] \(index): \(item)")
}
for (index, item) in arrayString.enumerated() {
print("[for loop] \(index): \(item)")
}
印出結果:
[forEach] 0: Kevin
[forEach] 1: Yoyo
[forEach] 2: Stan
[forEach] 3: Sam
[for loop] 0: Kevin
[for loop] 1: Yoyo
[for loop] 2: Stan
[for loop] 3: Sam
1. sort()
sort()
,會由小到大排序:var arrayNumber = [1, 2, 5, 10, 3, 9]
arrayNumber.sort()
print(arrayNumber)
印出結果:
[1, 2, 3, 5, 9, 10]
2. 自訂排序方式
var arrayNumber = [1, 2, 5, 10, 3, 9]
arrayNumber.sort{ a, b in
a > b
}
print(arrayNumber)
印出結果:
[10, 9, 5, 3, 2, 1]
$0
及$1
分別代表第一個參數及第二個參數,我們用以下寫法可以達到同樣的效果:var arrayNumber = [1, 2, 5, 10, 3, 9]
arrayNumber.sort{ $0 > $1 }
print(arrayNumber)
印出結果:
[10, 9, 5, 3, 2, 1]
sort(by:)
>
、<
符號排序,例如帶入 >
,可以由大到小排序:var arrayNumber = [1, 2, 5, 10, 3, 9]
arrayNumber.sort(by: >)
print(arrayNumber)
印出結果:
[10, 9, 5, 3, 2, 1]
randomElement()
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
print(arrayString.randomElement())
印出結果:
Optional("Yoyo")
由於 randomElement() 回傳的資料型態是 Optional,如果想強制取值,可以加上!
。
關於 Optional 會在之後詳細介紹。
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
print(arrayString.randomElement()!)
印出結果:
Yoyo
filter
var arrayNumber = [1, 2, 5, 10, 3, 9]
print(arrayNumber.filter{$0 >= 5})
印出結果:
[5, 10, 9]
map
var arrayString: [String] = ["Kevin", "Yoyo", "Stan", "Sam"]
print(arrayString.map{"My name is \($0)"})
印出結果:
["My name is Kevin", "My name is Yoyo", "My name is Stan", "My name is Sam"]
字典的內容是 key-value
的集合,可以透過 key
來取得對應的值。
var dicNumber = ["good": 80, "excellent": 95, "pass": 60]
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
key
來改變對應的值var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
dicString["age"] = "10"
print(dicString)
印出結果:
["name": "Yoyo", "age": "10", "hobby": "hiking"]
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
dicString["gender"] = "female"
print(dicString)
印出結果:
["hobby": "hiking", "age": "18", "name": "Yoyo", "gender": "female"]
removeValue
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
dicString.removeValue(forKey: "age")
print(dicString)
印出結果:
["name": "Yoyo", "hobby": "hiking"]
count
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
print(dicString.count)
印出結果:
3
1. 使用 forEach
可以使用 .key
及 .value
來分別取得 key 及值
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
dicString.forEach { item in
print(item)
print(item.key)
print(item.value)
}
印出結果:
(key: "name", value: "Yoyo")
name
Yoyo
(key: "age", value: "18")
age
18
(key: "hobby", value: "hiking")
hobby
hiking
2. 使用 for-in
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
for item in dicString {
print(item)
print(item.key)
print(item.value)
}
印出結果:
(key: "name", value: "Yoyo")
name
Yoyo
(key: "age", value: "18")
age
18
(key: "hobby", value: "hiking")
hobby
hiking
randomElement()
var dicString: [String : String] = ["name": "Yoyo", "age": "18", "hobby": "hiking"]
print(dicString.randomElement())
print(dicString.randomElement()!)
印出結果:
Optional((key: "age", value: "18"))
(key: "age", value: "18")
filter
var dicNumber = ["good": 80, "excellent": 95, "pass": 60]
print(dicNumber.filter{$0.value >= 80})
印出結果:
["excellent": 95, "good": 80]
mapValues
var dicNumber = ["good": 80, "excellent": 95, "pass": 60]
print(dicNumber.mapValues{$0 + 5})
印出結果:
["good": 85, "excellent": 100, "pass": 65]
Set 是沒有序列的集合,包含的值都是不重複的
var setNumber: Set = [0, 2, 5, 3]
var setString: Set<String> = ["orange", "yellow", "black"]
insert
var setString: Set<String> = ["orange", "yellow", "black"]
setString.insert("white")
print(setString)
印出結果:
["white", "orange", "yellow", "black"]
remove
var setString: Set<String> = ["orange", "yellow", "black"]
setString.remove("orange")
print(setString)
印出結果:
["yellow", "black"]
count
var setString: Set<String> = ["orange", "yellow", "black"]
print(setString.count)
印出結果:
3
1. 使用 forEach
var setString: Set<String> = ["orange", "yellow", "black"]
setString.forEach { item in
print(item)
}
印出結果:
orange
yellow
black
2. 使用 for-in
var setString: Set<String> = ["orange", "yellow", "black"]
for item in setString {
print(item)
}
印出結果:
orange
yellow
black
randomElement()
var setString: Set<String> = ["orange", "yellow", "black"]
print(setString.randomElement())
print(setString.randomElement()!)
印出結果:
Optional("black")
black
filter
var setNumber: Set = [0, 2, 5, 3]
print(setNumber.filter{$0 > 2})
印出結果:
[3, 5]
map
var setNumber: Set = [0, 2, 5, 3]
print(setNumber.map{$0 * 10})
印出結果:
[0, 20, 50, 30]
contains
var setString: Set<String> = ["orange", "yellow", "black"]
print(setString.contains("yellow"))
print(setString.contains("red"))
印出結果:
true
false
intersection
var setString: Set<String> = ["orange", "yellow", "black"]
var setString2: Set<String> = ["red", "yellow", "white"]
print(setString.intersection(setString2))
印出結果:
["yellow"]
symmetricDifference
var setString: Set<String> = ["orange", "yellow", "black"]
var setString2: Set<String> = ["red", "yellow", "white"]
print(setString.symmetricDifference(setString2))
印出結果:
["red", "black", "orange", "white"]
union
var setString: Set<String> = ["orange", "yellow", "black"]
var setString2: Set<String> = ["red", "yellow", "white"]
print(setString.union(setString2))
印出結果:
["black", "orange", "yellow", "red", "white"]
subtracting
var setString: Set<String> = ["orange", "yellow", "black"]
var setString2: Set<String> = ["red", "yellow", "white"]
print(setString.subtracting(setString2))
print(setString2.subtracting(setString))
印出結果:
["black", "orange"]
["red", "white"]
今日小結:
今天認識了不同種類的集合類型,我們可以善用他們不同的特性,選擇適合的類型來使用。
如果需要處理的是有排序的資料,較適合使用陣列 (Array)。
如果要處理的資料不在乎排序,需要 key-value 配對資料,較適合使用字典 (Dictionary)。
若是要處理數學集合運算,則集合 (Set) 是個不錯的選擇。
參考來源:
Array
Dictionary
Set