//===================数组定义===================
let arr1 = ["小二","小三","小四"]//自动推断里面存的是string类型
let arr2 = [1,2,3]//自动推断里面存的是int类型
let p = CGPoint(x: 10, y: 20)
let arr3 = [p]//自动推断里面存的是CGPoint类型
let arr4 = ["小二",1,p] as [Any]//混合数组:自动推断类型为NSObject
print("数组定义:\(arr1,arr2,arr3,arr4)")//(["小二", "小三", "小四"], [1, 2, 3], [(10.0, 20.0)], ["小二", 1, (10.0, 20.0)])
let arr5 = Array(repeating:0.0,count:3)
print("定义了一个double类型的数组,并指定容量为:\(arr5.count),数组为:\(arr5)")//容量为:3,数组为:[0.0, 0.0, 0.0]
//===================数组的操作===================
var arrC = ["张","李","王"]
arrC.append("zhao")//在数组后增加一个数据
print("数组的增加:\(arrC)")//["张", "李", "王", "zhao"]
arrC.insert("康", at: 1)//在第1个元素后面插入一条数据
print("数组的插入:\(arrC)")//["张", "康", "李", "王", "zhao"]
arrC[0] = "吴"//修改第0个元素的值
print("数组的修改:\(arrC)")//["吴", "康", "李", "王", "zhao"]
arrC[2...3] = ["正","周"]//对2和3的数组数据进行替换
print("数组元素替换:\(arrC)")//["吴", "康", "正", "周", "zhao"]
arrC.remove(at: 2)//删除第2个元素
print("数组的删除:\(arrC)")//["吴", "康", "王", "zhao"]
arrC.removeLast()//删除最后一个元素
print("数组删除最后一个元素:\(arrC)")//["吴", "康", "王"]
arrC.removeAll(keepingCapacity: true)//删除所有元素,保存该数组的空间 false清除该数组的空间
print("删除数组所有个元素:\(arrC),\(arrC.capacity)")//[],6
//===================数组的合并===================
var arrHM = ["1","2","3"]
let arrH = ["4","5","6"]
arrHM += arrH
print("类型相同数组合并\(arrHM)")//["1", "2", "3", "4", "5", "6"]
var arrHM2: [NSObject] = ["1" as NSObject,2 as NSObject,"3" as NSObject]
let arrH2: [NSObject] = ["4" as NSObject,5 as NSObject,"6" as NSObject]
arrHM2 += arrH2
print("类型不同数组合并\(arrHM2)")//[1, 2, 3, 4, 5, 6] 一般不会对不同类型数组进行合并
//===================数组的判空===================
let arrK = ["1"]
if arrK.isEmpty {
print("数组是空的")
}else{
print("数组有值")
}
//===================数组的遍历===================
var arrR = [Int]()//定义一个存放Int型的空数组并初始化
print("数组的初始容量:\(arrR.capacity)")
for i in 0..<8 {
arrR.append(i)//在数组后面增加元素
print("增加元素后的数组容量\(arrR.capacity)")//当数组的容量不够时,每次增加之前容量的一倍,这样可以避免每次增加数据时都开辟空间
}
let arrB:[String] = ["hello","world","the","swift"]
for item in arrB {
print("直接遍历的值为:\(item)")
}
for item in arrB[1...2] {
print("遍历指定范围的值为:\(item)")//world、the
}
for (index,value) in arrB.enumerated() {
print("遍历数组下标为\(index)的值为\(value)")
}
//===================数组的排序===================
let arrP = [3,1,4,5,2,8,6,9,10,7]
let downArr = arrP.sorted { (num1, num2) -> Bool in
return num1 > num2
}
print("数组的降序排序:\(downArr)--------\(arrP)")//[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]--------[3, 1, 4, 5, 2, 8, 6, 9, 10, 7]
let upArr = arrP.sorted {$0 < $1}
print("最简便的数组的升序排序:\(upArr)--------\(arrP)")//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]--------[3, 1, 4, 5, 2, 8, 6, 9, 10, 7]
//===================数组的Map方法===================
//map方法,返回的是一个数组,其获取一个闭包表达式作为唯一参数,集合中每个元素调用一次该闭包函数,并返回该元素所映射的值,具体的映射方式和返回值类型由闭包来指定。
//示例1:对一个Int型数组的元素进行2倍放大。
let mapArr1 = [1,2,3]
let mapArr1Double = mapArr1.map {
$0 * 2
}
print("map的示例1:\(mapArr1Double)")//[2, 4, 6]
//示例2:对Int型数组元素后前后加符号,转成字符串数组
let mapArr2 = [2,5,10,20,50,100]
let mapArr2F = mapArr2.map{
"¥\($0) 元"
}
print("map的示例2:\(mapArr2F)")//["¥2 元", "¥5 元", "¥10 元", "¥20 元", "¥50 元", "¥100 元"]
//示例3:将整型数组转换成对应的字符串数组
let numbers = [12,54,521]
let digitNames = [0:"Zero",1:"One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nine"]
let mapArr3 = numbers.map { (number) -> String in
var number = number
var output = ""
repeat {
output = digitNames[number % 10]! + output//取最后一位
number /= 10//取除最后一位外的其他几位
}while number > 0//只要大于0就一直循环
return output
}
print("map的示例3:\(mapArr3)")//["OneTwo", "FiveFour", "FiveTwoOne"]
网友评论