今天回家挺早的,多学习一会儿,呵呵。
集合类型
1、数组
数组的声明,直接上代码:
//Array
//声明空的数组
var arr1 = [Int]()
//添加值
arr1.append(12)
print(arr1.count)
//清空
arr1 = []
带默认值的数组声明:
//带默认值的数组
var arr = [Int](count: 3,repeatedValue: 1)
for i in arr {
print(i)
}
基本操作
var arr = ["a","b","c","d","e"]
print(arr.count)
//在0位插入 f
arr.insert("f",atIndex: 0)
//末尾插入 aa
arr.append("aa")
//添加新的数组
arr += ["1","2"]
//替换
arr[1] = "3"
print(arr)
//替换 4到6位
arr[4...6] = ["22","333","444"]
print(arr)
//移除
arr.removeAtIndex(0)
arr.removeLast()
arr.removeFirst()
arr.removeAll()
2、集合
存放多个相同类型值顺序不一定的集合类型,声明方法 Set<type>
3、字典
//Dictionary
var dict:[String:String] = ["name":"lsw","age":"15","sex":"1"]
print(dict["name"])
dict["country"] = "China"
print(dict)
//遍历
for (key,value) in dict {
print("key = \(key),value = \(value)")
}
//keys
var keys = dict.keys
//values
var values = dict.values