1.Array的初始化
var array1 :Array<Int> = Array<Int>()
var array2 :[Int] = Array<Int>()
var array3 = Array<Int>()
var threeInts = [Int](count:6,repeatedValue:1)
2.Array的長(zhǎng)度與判空
threeInts.count
threeInts.isEmpty
threeInts.count == 0
3.Array的索引
//數(shù)組的索引取值
threeInts[1]
//數(shù)組的索引可以是一個(gè)范圍
threeInts[1...2] //[1, 1]
threeInts[1..<3] //[1, 1]
//可以對(duì)數(shù)組的一個(gè)范圍整體賦值
threeInts //
threeInts[1...3] = [1,2,3]
threeInts //
threeInts[2...3] = [4] //當(dāng)個(gè)數(shù)不足,會(huì)將沒(méi)有值的刪除
threeInts //
4.Array添加與刪除元素
//添加一個(gè)元素
threeInts.append(1)
//添加一堆元素
threeInts.appendContentsOf([3,3,3,3,3])
threeInts
//使用運(yùn)算符來(lái)添加一個(gè)或多個(gè)元素
threeInts+=[4,4,4,4]
//在指定位置添加一個(gè)元素
threeInts.insert(5, atIndex: 1)
//移除第一個(gè)元素
threeInts.removeFirst()
5.Array的遍歷
//遍歷所有的值
for number in threeInts{
print(number)
}
//遍歷索引和值
for (index,number) in threeInts.enumerate(){
print("index:\(index) number:\(number)")
}