swift基础学习(03)[数组、集合、字典]
//数组 //空数组 var arr = [Int]() print(arr) //带有默认值s数组 var shoppingList: [String] = ["Eggs","Milk"] var number :[Int] = [1,2,3] print(shoppingList,number) //数组追加创建新的数组 var number2 = [5,6,7] var add = number2 + number print(add[0]) //快速遍历数组 for index in add { print(index) } //判断数组是否为空 if add.isEmpty { print("数组为空") }else{ print("数组不为空") } //为数组追加元素 add.append(9) print(add) //取出某个索引的值index print(add[2]) //插入某个数 add.insert(11,atIndex: 0) //删除某个元素 add.removeAtIndex(0) //采用元组遍历获得 对应的索引和值 for (index,value) in add.enumerate() {
print("index is (index),value is (value)")
} //集合集合(Set)用来存储相同类型并且没有确定顺序的值。当集合元素顺序不重要时或者希望确保每个元素只出现一次 时可以使用集合而不是数组 //创建一个空的集合 var emptySet = Set<Character>() print(emptySet.count) //增 emptySet.insert("d") print(emptySet) //创建一个集合,这是一个string集合,其他类似 var combine: Set<String> = ["1","2"] print(combine) //遍历集合 for index in combine { print(index) } //其他类似数组 //字典 //key-value //创建一个空字典 var emptyDic = [Int:String]() print(emptyDic) //直接 var contentDic = ["1":"nihao"] print(contentDic) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |