目录
0 绪论
1 数组的创建和初始化
2.数组遍历
2.1通过键值对遍历
2.2 通过forEach遍历
2.3 通过for in遍历
2.3.1 for in 搭配 enumerated
2.3.2 for in的另一种形式
2.3.2 for in 搭配 indices
2.4 通过Iterator遍历器遍历
3 数组的操作
3.1 contains 判断数组包含元素
3.2 allSatisfy(_:) 判断数组的每一个元素都符合给定的条件
4.查找比较
4.1 数组查找
4.1 数组比较大小 min ,max
4.2在参数多个的时候对数组查找
5 数组添加删除操作
5.1append插入元素
5.2 insert 插入元素
5.3 remove移除元素
5.4 移除多个元素(ArraySlice)
5.4.1 通过 Drop 得到 ArraySlice
5.4.2 通过 prefix 得到 ArraySlice
5.4.3 通过 suffix 得到 ArraySlice
5.4.4 通过 Range 得到 ArraySlice
5.5 ArraySlice 转为 Array
5.6 ArraySlice 和原 Array 相互独立
6 数组排序
6.1 随机打乱数组顺序
6.2 数组倒序
6.3数组的分组
6.4 数组的顺序排序
6.5数组元素交换位置
7 数组的拼接
7.1 字符串数组拼接
7.2元素为 Sequence 数组的拼接
8 总结
今天来学习一下swifi的数组,废话不说,直接撸代码吧。
let array = [1,2,3,4]
let array = [Int](1...4)
var array: [String] = []
var array = [String]()
let chars = Array("welcome")
print(chars)
let numbers = Array(1...20)
print(numbers)
let chars = Array(repeating: "AI", count: 5)
print(chars)
/**
输出结果如下:
["w", "e", "l", "c", "o", "m", "e"]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
["AI", "AI", "AI", "AI", "AI"]
*/
let students = ["zhangsan": 15,"lisi": 16,"wangwu": 17]
let names = Array(students.keys)
let ages = Array(students.values)
print(names)
print(ages)
/**
输出结果如下:
["zhangsan", "wangwu", "lisi"]
[15, 17, 16]
*/
let numbers = [Int](0...4)
numbers.forEach{ (num) in
if num == 3{
//此处不能使用 continue ,break关键字,使用return也只能退出当次循环
return
}
print(num)
}
/**
输出结果如下:
0
1
2
4
*/
var numbers = [Int](0...5)
for(index,num)in numbers.enumerated(){
//同时得到索引和值 enumerated()
print("the index is: \(index),the value is \(num)")
if num == 3 {
//此处能使用 break关键字
}
}
/**
输出结果如下:
the index is: 1,the value is 1
the index is: 2,the value is 2
the index is: 3,the value is 3
the index is: 4,the value is 4
the index is: 5,the value is 5
*/
var numbers = [Int](2...6)
for i in 0..
var numbers = [Int](2...6)
for i in numbers.indices{
print("the index is: \(i),the valuse is: \(numbers[i])")
}
/**
输出结果如下:
the index is: 0,the valuse is: 2
the index is: 1,the valuse is: 3
the index is: 2,the valuse is: 4
the index is: 3,the valuse is: 5
the index is: 4,the valuse is: 6
*/
let numbers = [Int](0...6)
var numIterator = numbers.makeIterator()
while let num = numIterator.next(){
print("the value is: \(num)")
}
/**
输出结果如下:
the value is: 2
the value is: 3
the value is: 4
the value is: 5
the value is: 6
*/
let array = [56,42,30,8,63,89,102,6,19,27,34]
print(array.contains(63))
print(array.contains(where: {$0 > 100}))
/**
输出结果如下:
true
true
*/
let array = [Int](0...10)
print(array.allSatisfy({ $0 > 5}))
print(array.allSatisfy({$0 < 10}))
/**
输出结果如下:
false
false
*/
let array = [12,56,48,69,120,3,7,59,25,56,75]
print(array.first)
print(array.last)
print(array.first(where: { $0 > 45}))
print(array.last(where: { $0 > 45}))
/**
输出结果如下:
Optional(12)
Optional(75)
Optional(56)
Optional(75)
*/
let array = [12,56,48,69,120,3,7,59,25,56,75]
print(array.firstIndex(of: 56))
print(array.lastIndex(of: 56))
print(array.firstIndex(where: { $0 > 45}))
print(array.lastIndex(where: { $0 > 45}))
/**
输出结果如下:
Optional(1)
Optional(9)
Optional(1)
Optional(10)
*/
let array = [12,56,48,69,120,3,7,59,25,56,75]
print(array.min())
print(array.max())
/**
输出结果如下:
Optional(3)
Optional(120)
*/
let array = [("zhangsan",26),("lisi",56)]
print(array.min(by: {a,b in a.1 > b.1 }))
print(array.max(by: {a,b in a.1 > b.1}))
/**
输出结果如下:
Optional(("lisi", 56))
Optional(("zhangsan", 26))
*/
let array = [("zhangsan",26),("lisi",56),("wagnwu",32),("niuli",105)]
print(array.min(by: {(a,b) -> Bool in
a.1 < b.1
}))
print(array.max(by: {(a,b) -> Bool in
a.1 < b.1
}))
/**
输出结果如下:
Optional(("zhangsan", 26))
Optional(("niuli", 105))
*/
var array = [Int](1...5)
array.append(15)
print(array)
array.append(contentsOf: 8...15)
print(array)
/**
输出结果如下:
[1, 2, 3, 4, 5, 15]
[1, 2, 3, 4, 5, 15, 8, 9, 10, 11, 12, 13, 14, 15]
*/
var array = [Int](1...5)
array.insert(200, at: 2)
print(array)
array.insert(contentsOf: -9...(-1), at: 0)
print(array)
/**
输出结果如下:
[1, 2, 200, 3, 4, 5]
[-9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 200, 3, 4, 5]
*/
var chars: [Character] = ["a","b","c","d","e"]
chars.insert(contentsOf: "nihao", at: 0)
print(chars)
let removeChar = chars.remove(at: 1)
print(removeChar)
print(chars)
/**
输出结果如下:
["n", "i", "h", "a", "o", "a", "b", "c", "d", "e"]
i
["n", "h", "a", "o", "a", "b", "c", "d", "e"]
*/
var chars: [Character] = ["a","b","c","d","e"]
chars.insert(contentsOf: "welcome", at: 0)
print(chars)
let removeChar2 = chars.removeFirst()
print(removeChar2)
print(chars)
let removeChar = chars.removeLast()
print(removeChar)
print(chars)
chars.removeSubrange(1...3)
print(chars)
let removeNum = chars.popLast()
print(chars)
print(removeNum)
/**
输出结果如下:
["w", "e", "l", "c", "o", "m", "e", "a", "b", "c", "d", "e"]
w
["e", "l", "c", "o", "m", "e", "a", "b", "c", "d", "e"]
e
["e", "l", "c", "o", "m", "e", "a", "b", "c", "d"]
["e", "m", "e", "a", "b", "c", "d"]
["e", "m", "e", "a", "b", "c"]
Optional("d")
*/
var chars: [Character] = ["a","b","c","d","e"]
chars.removeAll()
print(chars)
print(chars.capacity)
var chars2: [Character] = ["a","b","c","d","e"]
chars2.removeAll(keepingCapacity: true)
print(chars2)
print(chars2.capacity)
/**
输出结果如下:
[]
0
[]
5
*/
let array = [5,2,10,1,0,100,46,99]
print(array)
print(array.dropFirst())
print(array.dropFirst(3))
print(array.dropLast())
print(array.dropLast(3))
print(array.drop{ $0 < 15})
/**
输出结果如下:
[5, 2, 10, 1, 0, 100, 46, 99]
[2, 10, 1, 0, 100, 46, 99]
[1, 0, 100, 46, 99]
[5, 2, 10, 1, 0, 100, 46]
[5, 2, 10, 1, 0]
[100, 46, 99]
*/
let array = [5,2,10,1,0,100,46,99]
print(array.prefix(4))
print(array.prefix(upTo: 4))
print(array.prefix(through: 4))
print(array.prefix{ $0 < 10})
/**
输出结果如下:
[5, 2, 10, 1]
[5, 2, 10, 1]
[5, 2, 10, 1, 0]
[5, 2]
*/
let array = [5,2,10,1,0,100,46,99]
print(array.suffix(3))
print(array.suffix(from: 5))
/**
输出结果如下:
[100, 46, 99]
[100, 46, 99]
*/
可以通过对数组下标指定 Range 获取 ArraySlice,可以使用闭区间、半开半闭区间、单侧区 间、甚至可以只使用 ... 来获取整个数组组成的 ArraySlice 。
let array = [5,2,10,1,0,100,46,99]
print(array[3...5])
print(array[3..<5])
print(array[...2])
print(array[..<2])
print(array[6...])
print(array[...])
/**
输出结果如下:
[1, 0, 100]
[1, 0]
[5, 2, 10]
[5, 2]
[46, 99]
[5, 2, 10, 1, 0, 100, 46, 99]
*/
let array = [5,2,10,1,0,100,46,99]
let slice = array[3...5]
var arrayNew = Array(slice)
var array = [Int](0...20)
var arrayShu = array.shuffled()
print(array)
print(arrayShu)
/**
输出结果如下:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[17, 13, 2, 0, 1, 12, 15, 5, 10, 3, 11, 6, 18, 8, 16, 7, 19, 4, 14, 9, 20]
*/
var array = [Int](0...20)
print(array)
array.reverse()
print(array)
/**
输出结果如下:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
*/
var array = [Int](0...20)
var arrayShu = array.shuffled()
print(array)
let index = arrayShu.partition { (element) -> Bool
in
element > 10
}
print(arrayShu)
let partition1 = arrayShu[..
sort() 在原数组上将元素排序,只能作用于数组变量。
var array = [Int](0...20)
var arrayShu = array.shuffled()
print(array)
print(arrayShu)
print(arrayShu.sorted())
/**
输出结果如下:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[3, 19, 12, 13, 1, 10, 20, 16, 18, 9, 11, 17, 5, 0, 14, 8, 2, 6, 7, 4, 15]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
*/
var array = [Int](0...20)
print(array)
array.swapAt(array.startIndex, array.endIndex - 1)
print(array)
/**
输出结果如下:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[20, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0]
*/
var array = ["welcome","shenzhen"]
print(array.joined())
print(array.joined(separator: ","))
/**
输出结果如下:
welcomeshenzhen
welcome,shenzhen
*/
let ranges = [0...3,8...15,18...26]
for range in ranges{
print(range)
}
/**
输出结果如下:
0...3
8...15
18...26
*/
let array = [[1...3,5...6],[8...9,19...23],[10...15,28...30,35...40]]
let joined = array.joined(separator: [-1...0])
print(Array(joined))
/**
输出结果如下:
[ClosedRange(1...3), ClosedRange(5...6), ClosedRange(-1...0), ClosedRange(8...9), ClosedRange(19...23), ClosedRange(-1...0), ClosedRange(10...15), ClosedRange(28...30), ClosedRange(35...40)]
*/
for i in ranges.joined(){
print(i)
}
/**
输出结果如下:
0
1
2
3
8
9
10
11
12
13
14
15
18
19
20
21
22
23
24
25
26
*/
暂时还没没写总结。