1.)数组的常见创建方式:
let _ : Array = [1,2,3,4]
let _ : [Int] = [2,3,3]
let _ : Array <Int>= ["1","2"]
若需要数组中放置不同类型的元素,可使用Any类型:
let _ : [Any] = [1,"a"]
2.)数组和Slice
Slice创建方式:
var slice : ArraySlice <Int>= [1,2,3,4]
数组和Slice转换:
var slice : ArraySlice= [1,2,3,4]
var array = Array(slice)
slice = array[0...1]
3).Range
var range : Range= 1...5
range = 1..<11
range = Range(start: 1, end: 10)
4.)数组的批量操作:
var array : Array = [6,7]
array += [8]
array.append(9)
let slice : ArraySlice = [1,2,3]
array += slice
let array1 = [11,12,13,14]
array += array1[2...3]
print(array) //打印结果:[6, 7, 8, 9, 1, 2, 3, 13, 14]
5).数组的遍历
let array : Array = [6,7,11,12,123]
for (index , value) in EnumerateSequence(array) {
print(index)
print(value)
}
或者
let array : Array = [6,7,11,12,123]
for ele in array {
print(ele)
}
6).字典的初始化:
let _ : Dictionary= ["1":1]
let _ : [String : Int] = ["a" : 1]