定義一個不可變數(shù)
let array : Array<String> = ["lxd", "xiaoming", "xiaohong", "xiaogang"]
或者
let array1 : [String] = ["lxd", "xiaoming", "xiaohong", "xiaogang"]```
let array2 = ["lxd", "xiaoming", "xiaohong", "xiaogang"]
let array3 : [Int] = [10, 20, 30]
let array4 : Array<Any> = ["lxd", "cc", 9.99]
let array5 : [Any] = [18, "why", 1.88]
let array6 = [18, "why", 1.88] as [Any]
定義可變數(shù)組
var names : [String] = ["lxd", "xiaoming", "xiaohong", "xiaogang"]
定義一個空數(shù)組
var names3 = [String]()
或者
var names1 : [String] = Array()
var names2 = Array<String>()
可變數(shù)組
添加元素
names.append("xiaokai")
names.insert("lxd", at: 2)
刪除數(shù)組
names.removeAll()
names.remove(at: 0)
修改元素
names[0] = "dd"
取出某個元素
names[2]
names.firstIndex(of: "xiaoming")
names.lastIndex(of: "xiaohong")
遍歷數(shù)組
for i in 0..<names.count {
print(names[i])
}
for item in names {
print(item)
}
指定區(qū)間遍歷
for i in 0..<2 {
print(names[i])
}
for item in names[0..<2] {
print(item)
}
數(shù)組合并
//兩個數(shù)組中存放的元素類型一致
let name4 = ["qq", "ww"]
var name5 = ["ee", "rr"]
let name6 = name4 + name5