import Foundation
//数组 Swift创建数组一定要制定存放元素类型
et arr1 = [Int]()var arr2 = [String]()
//添加元素
arr2.append("张三")print(arr2)
//修改arr2[0] = "lisi"print(arr2)
//字面量创建数组
var arr3 = ["zhangsan","lisi","wanger","mazi"]print(arr3)
//删除
/根据下标删除
arr3.remove(at: 0)print(arr3)
//删除最后一个元素
arr3.removeLast()
//arr3.remove(at: 2)
//删除第一个元素
arr3.removeFirst()
//删除所有元素
arr3.removeAll()
//获取数组元素个数
print(arr3.count)print(arr3)
//数组的遍历
var arr4 = ["huaqiangu","shaqianmo","dongfangyuqin","tangbao","zunshang"]for item in arr4{print(item)}
//将下标遍历出来
for (i,Item) in arr4.enumerated(){print("item:\(Item)index:\(i)")}
//数组排序
arr4.sort()print(arr4)//var numArr = [8, 4, 3, 7, 1, 5]
//冒泡排序(双层for循环:外层控制趟数,内层控制每趟比较的次数)
/比较的趟数等于数组个数-1
//比较的次数等于数组个数-比较的趟数
//外层控制趟数for i in 0..升序
//< 降序
if numArr[j] > numArr[j + 1]{
let temp = numArr[j]
numArr[j] = numArr[j + 1]
numArr[j + 1] = temp
}
}
print(numArr)
}
//元素的插入
var arr5 = ["a","b","c","d"]
//下标值<=数组个数
arr5.insert("f", at: 4)
print(arr5)
//字典 创建字典和创建数组是一样创建同时就要制定key和value值的类型
let dict = [String:Int]() //空字典
let dict2 = ["name":"亚索","age":18,"height":1.8] as [String : Any]
//访问字典的元素
//String? as!解包
print("国服第一亚索"+(dict2["name"] as! String))
var dic3 = ["name":"蛮子","gender":"妖","skill":"蛮三刀"]
//修改字典
//找得到key值就是改,找不到就是加
dic3["gender"] = "汉子"
dic3["hobby"] = "找妹子" //增加键值对
//字典的删除
dic3.removeValue(forKey: "gender")
print(dic3)
//字典的遍历
//遍历的是键值对
for value in dic3{
print(value)
}
//遍历字典key值
for key in dic3.keys {
print(key)
}
//遍历字典的value值
for values in dic3.values{
print(values)
}