swift基础学习(03)[数组、集合、字典]

来源:互联网 发布:数据库备份语句 编辑:程序博客网 时间:2024/06/05 14:29

//数组

//空数组

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 inadd {

print(index)

}

//判断数组是否为空

if add.isEmpty {

print("数组为空")

}else{

print("数组不为空")

}

//为数组追加元素

 add.append(9)

print(add)

//取出某个索引的值index

print(add[2])

//插入某个数

add.insert(11, atIndex:0)

print(add)

//删除某个元素

add.removeAtIndex(0)

print(add)

//采用元组遍历获得对应的索引和值

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 incombine {

print(index)

}

//其他类似数组  

//字典

//key-value

//创建一个空字典

var emptyDic = [Int:String]()

print(emptyDic)

//直接

var contentDic = ["1":"nihao"]

print(contentDic)


0 0
原创粉丝点击