數(shù)組初始化
可以隱式初始化甚负,也可以隱式初始化
<pre>
`
//隱式初始化
var vowels = ["A","E","I","O","U"]
//顯式初始化
var numbers:[Int] = [0,1,2,3,4,5]
var numbers1:Array<Int> = [0,1,2,3,4,5]
//申明空數(shù)組
var emptyArray1: [Int] = []
var emptyArray2: Array<Int> = []
var emptyArray3 = Int
//申明特定數(shù)組個數(shù)揍堕,并賦同一個初值
var allZeros = [Int](count: 5 , repeatedValue:0)
注意:
var array = []//聲明的array類型是NSArray
`
</pre>
數(shù)組的基本用法
<pre>
`
//定義3個數(shù)組(整型數(shù)組竞慢,字符串?dāng)?shù)組,空數(shù)組)
var numbers = [1,2,3,4,5]
var vowels = ["A","B","C","D","E"]
var emptyArray = Int
//獲取數(shù)組個數(shù)
vowels.count
//判斷空數(shù)組
numbers.isEmpty //返回false
emptyArray.isEmpty //返回true
//通過索引來獲取數(shù)組元素
vowels[0] //A
//獲取數(shù)組第一個元素和最后一個元素
vowels.first //A
vowels.last //E
emptyArray.first //nil 如果是空數(shù)組织狐,通過這種方法獲取元素暂幼,返回nil
注意:既然能返回nil,說明通過這種方法返回的是個可選型移迫,為此不能直接使用旺嬉,必須要進(jìn)行解包
if let firestVowel = vowels.first{
print("The first vowel is " + firestVowel)
}
//獲取數(shù)組最小值的元素
vowels.minElement() //A
numbers.minElement() //1
//獲取元素最大值的元素
vowels.maxElement() //E
numbers.maxElement() //5
//通過范圍獲取一個子數(shù)組
numbers[2..<4] //[3,4]
numbers[2..<numbers.count] //[3,4,5]
//判斷數(shù)組是否包含某個元素,返回一個布爾量
vowels.contains("A") //true
vowels.contains("P") //false
//判斷數(shù)組是否包含某個元素厨埋,同時也知道元素所在索引位置邪媳,返回一個Int量,沒有的話返回nil
vowels.indexOf("A") //1
vowels.indexOf("P") //nil
**********遍歷數(shù)組**********
//方法1
for index in 0..<numbers.count{
numbers[index]
}
//方法2,不需要獲取索引
for number in numbers{
print(numbers)
}
//方法3雨效,通過元組進(jìn)行遍歷迅涮,可以獲取元素和索引
for (index , vowel) in vowels.enumerate(){
print("(index+1):(vowel)")
}
***********數(shù)組比較************
var oneToFive = [1,2,3,4,5]
numbers == oneToFive //返回一個true
oneToFive = [2,1,3,4,5]
numbers == oneToFive //返回一個false
//數(shù)組是有序的
`
</pre>
數(shù)組的更多操作
-
增
<pre>
var courese = ["swift" , "swift2" , "swift3"];
<p>//增加一個數(shù)組元素 courese.append("swift4") 等同于.. courese += ("swift5")
<p>//在數(shù)組中間插入元素 courese.insert("swift6", atIndex: 2)//注:atIndex參數(shù)必須要在數(shù)組索引中
</pre>
-
刪
<pre>
var courese = ["swift" , "swift2" , "swift3"];
<p>//刪除最后一個元素 courese.removeLast()
<p>//刪除第一個元素 courese.removeFirst()
<p>//刪除指定一個元素 courese.removeAtIndex(2)
<p>//刪除一個范圍元素 courese.removeRange(0..<2)
<p>刪除所有元素 courese.removeAll()
</pre>
-
修改
<pre>
var courese = ["swift" , "swift2" , "swift3"];
<p>//通過索引獲取到元素直接修改 courese[0] = "Object-C"
<p>//通過索引范圍獲取到一個子數(shù)組進(jìn)行修改值 courese[1..2] = ["Object-C2","Object-C3"]
<p>//通過索引范圍獲取到的一個子數(shù)組,被賦值為一個元素的數(shù)組徽龟,那么相應(yīng)了叮姑,就從有3個元素的數(shù)組變成1個元素的數(shù)組,對于的courese數(shù)組就變成2個元素?cái)?shù)組了 courese[0..<4] = ["swift"]//數(shù)組只剩下一個元素
</pre>
字典初始化
<pre>
`
//隱式初始化
var dict = ["swift":"雨燕顿肺;快速" , "python":"大蟒" , "java":"爪洼島(位于印尼)" , "groovy":"絕妙的戏溺,時髦的"]
//顯式字典key-value類型初始化
var dict1: [String:String] = ["swift":"雨燕;快速" , "python":"大蟒" , "java":"爪洼島(位于印尼)" , "groovy":"絕妙的屠尊,時髦的"]
//顯式字典類型以及key-value類型初始化
var dict2:Dictionary<String,String> = ["swift":"雨燕旷祸;快速" , "python":"大蟒" , "java":"爪洼島(位于印尼)" , "groovy":"絕妙的,時髦的"]
****聲明空字典4種方式****
var emptyDict:[String:Int] = [:]
var emptyDict1:Dictionary<Int,String> = [:]
var emptyDict2 = String:String
var emptyDict4 = Dictionary<Int,Int>()
//通過key取出value
dict["swift"] //取出來的是可選型讼昆,必須要進(jìn)行解包
if let value = dict["swift"]{//解包
print(value)
}
//字典集合個數(shù)
dict.count
//判斷字典是否為空字典
dict.isEmpty //false
emptyDict.isEmpty //true
//取出字典所有的key和value
dict.keys
dict.values
******遍歷字典*******
//遍歷key
for key in dict.keys{
print(key)
}
//遍歷value
for value in dict.values{
print(value)
}
//遍歷字典(利用元組)
for (key,value) in dict{
print("(key):(value)")
}
******字典對比********
let dic1 = [1:"A" , 2:"B"]
let dic2 = [1:"A" , 2:"B"]
let dic3 = [2:"B" , 1:"A"]
dic1 == dic2 //true
dic1 == dic3 //true
結(jié)論:字典是無序的托享。
`
</pre>
字典的操作
-
增
<pre>
只需要傳入一個字典沒有的key并賦值個value
<p>var user = ["name":"bobobo","password":"liuyubo","occupation":"programmer"]
<p>//方法1 user.updateValue("4399.com", forKey: "website")
<p>//方法2 user["email"] = "524297115@qq.com"
</pre>
-
修改
<pre>
只需要通過key獲取到value,修改即可浸赫。
<p>var user = ["name":"bobobo","password":"liuyubo","occupation":"programmer"]
<p>//方法1 user["occupation"] = "freelancer"
<p>//方法2 user.updateValue("imooc", forKey: "password")
</pre>
-
刪除
<pre>
<p>//方法1 user["website"] = nil
<p>//方法2 user.removeValueForKey("website")//返回的是可選型 if let website = user.removeValueForKey("email"){ print("\(website)刪除成功") }
<p>//刪除所有元素 user.removeAll()
</pre>
集合初始化
<pre>
`
集合需要顯示申明出來
//集合可以去重
var skillOfA:Set<String> = ["swift","OC","OC"]
//申明一個空集合
var emptySet1:Set<Int> = []
var emptySet2 = Set<Double>()
//數(shù)組強(qiáng)制轉(zhuǎn)成集合
var vowels = Set(["A","E","I","O","U","U"])
var skillOfB: Set = ["HTML","CSS","Java"]
//集合元素個數(shù)
skillOfB.count
//判斷集合是否為空集合
skillOfB.isEmpty //false
emptySet1.isEmpty //true
//因?yàn)榧鲜菬o須的闰围,所以這里的first看作隨機(jī)取出集合的一個元素
let e = skillOfA.first
//判斷某個集合是否包含某個元素,返回一個布爾量
skillOfA.contains("swift") //true
skillOfB.contains("swift") //false
****遍歷集合******
for skill in skillOfB{
print(skill)
}
****集合比較******
let setA: Set = [1,2,3]
let setB: Set = [1,2,3]
let setC: Set = [3,2,1]
let setD: Set = [1,2,3,1,1,1,1,2,2,3,3]
setA == setB //true
setA == setC //true
setA == setD //true
結(jié)論:集合是無序既峡、去重的
`
</pre>
集合的操作
<pre>
`
//初始化6個集合
var skillsOfA: Set<String> = ["swift","OC"]
var skillsOfB: Set<String> = ["HTML","CSS","Javascript"]
var skillsOfC: Set<String> = []
var skillsOfAA: Set<String> = ["swift","OC"]
var skillsOfBB: Set<String> = ["HTML","CSS","Javascript"]
var skillsOfCC: Set<String> = ["swift","HTML","CSS"]
`
</pre>
-
增
<pre>
skillsOfC.insert("swift") skillsOfC.insert("HTML") skillsOfC.insert("CSS") //注:如果再添加相同的元素羡榴,沒有任何意義
</pre>
-
刪
<pre>
skillsOfC.remove("CSS") skillsOfC skillsOfC.removeAll()
</pre>
-
并集
<pre>
union //不改變調(diào)用者集合元素 unionInPlace //改變調(diào)用者集合元素
<p>skillsOfAA.union(skillsOfCC) //輸出"swift,CSS,OC,HTML" skillsOfAA //沒有變化
<p>skillsOfAA.unionInPlace(skillsOfCC) //輸出"swift,CSS,OC,HTML" skillsOfAA //輸出"swift,CSS,OC,HTML"
</pre>
-
交集
<pre>
intersect //不改變調(diào)用者集合元素 intersectInPlace //改變調(diào)用者集合元素
<p>skillsOfAA.intersect(skillsOfCC) //輸出"swift" skillsOfAA //沒有變化
<p>skillsOfAA.intersectInPlace(skillsOfCC) //輸出"swift" skillsOfAA //輸出"swift"
</pre>
-
減法
<pre>
subtract //不改變調(diào)用者集合元素 subtractInPlace //改變調(diào)用者集合元素
<p>skillsOfAA.subtract(skillsOfCC) //輸出"OC" skillsOfAA //沒有變化
<p>skillsOfAA.subtractInPlace(skillsOfCC) //輸出"OC" skillsOfAA //輸出"OC"
</pre>
-
亦或
<pre>
exclusiveOr //不改變調(diào)用者集合元素 exclusiveOrInPlace //改變調(diào)用者集合元素
<p>skillsOfAA.exclusiveOr(skillsOfCC) //輸出"OC,HTML,CSS" skillsOfAA //沒有變化
<p>skillsOfAA.exclusiveOrInPlace(skillsOfCC) //輸出"OC,HTML,CSS" skillsOfAA //輸出"OC,HTML,CSS"
</pre>
-
其他操作
<pre>
var skillsOfD: Set = ["swift"]
<p>//子集、真子集 skillsOfD.isSubsetOf(skillsOfAA) //true skillsOfD.isStrictSubsetOf(skillsOfAA) //true
<p>//超集运敢、真超集 skillsOfAA.isSupersetOf(skillsOfD) //true skillsOfAA.isStrictSupersetOf(skillsOfD) //true
<p>//判斷2個集合是否相離校仑,沒有公共元素 skillsOfAA.isDisjointWith(skillsOfBB) //true skillsOfAA.isDisjointWith(skillsOfCC) //false
</pre>