var num = [1,2,3,4,5,6,7,8,9]
//迭代数组
for x in num { }
//迭代除了最后两个元素以外的数组
for x in num.dropLast(2) { }
//列举数组中的元素和对应下标
for (index, element) in num.enumerated() { }
//寻找一个指定元素的位置
if let index = num.firstIndex(where: { $0 == 5 }) { }
if let index = num.firstIndex(where: { (num) -> Bool in
return num == 5
}) { }
//对数组中的所有元素进行变形
let num2 = num.map { (x) -> Int in return x * 5 }
let num2 = num.map { $0 * 5 }
let num2 = num.compactMap { $0 * 2 }
let num2 = num.compactMap { (x) -> Int in return x * 2 }
[1, 2, 3].map( { (i: Int) -> Int in return i * 2 } )
[1, 2, 3].map( { i in return i * 2 } )
[1, 2, 3].map( { i in i * 2 } )
[1, 2, 3].map( { $0 * 2 } )
[1, 2, 3].map() { $0 * 2 }
[1, 2, 3].map { $0 * 2 }
//生成两个0-100的随机数
(0..<2).map { _ in Int.random(in: 1..<100) }
//筛选出符合某个特定标准的元素
let num2 = num.filter { (x) -> Bool in return x > 5 }
let num2 = num.filter { return $0 > 5 }
//针对一个条件测试所有元素
let isTrue = num.allSatisfy { $0 < 5 }
let isTrue = num.allSatisfy { (x) -> Bool in return x < 5 }
//将元素聚合成一个值
//reduce(<#T##initialResult: Result##Result#>, <#T##nextPartialResult: (Result, Int) throws -> Result##(Result, Int) throws -> Result#>)
//把所有元素合并为一个新的单一的值。初始值(3),中间值(x),序列中的元素(y)
let num2 = num.reduce(3) { $0 + $1 }
let num2 = num.reduce(3) { (x, y) -> Int in return x + y }
let num2 = num.reduce(3, { x, y in x + y })
let num2 = num.reduce(3, +)
//找出整形数组中的最大元素。 9
let max = num.reduce(num[0], { $0 > $1 ? $0 : $1 })
let max = num.reduce(num[0]) { (x, y) -> Int in return x > y ? x : y }
//翻转一个数组。 [9, 8, 7, 6, 5, 4, 3, 2, 1]
let num2 = num.reduce([], { [$1] + $0 })
//翻转一个数组。 [9, 8, 7, 6, 5, 4, 3, 2, 1, 20]
let num2 = num.reduce([20]) { (x, y) -> [Int] in return [y] + x }
//起始位置的值(+)和String数组中元素连接一个字符串。 +abc
let str = ["a","b","c"]
let str2 = str.reduce("+", { $0 + $1 })
let str2 = str.reduce("+") { (x, y) -> String in return x + y }
//整数列表转换为一个字符串。 1, 2, 3, 4, 5, 6, 7, 8, 9,
let num2 = num.reduce("", {str, x in str + "\(x), "})
//访问每一个元素
num.forEach({ print($0) })
num.forEach { (x) in print(x) }
//升序
num.sort()
num.sort(by: { $0 < $1 })
num.sort { (x, y) -> Bool in return x < y }
let num2 = num.sorted()
let num2 = num.sorted(by: { $0 < $1 })
//降序
num.sort(by: { $0 > $1 })
num.sort { (x, y) -> Bool in return x > y }
var numberStrings = [(2, "two"), (1, "one"), (3, "three")]
numberStrings.sort(by: <) // [(1, "one"), (2, "two"), (3, "three")]
//是否存在
if let x = num.firstIndex(of: 5) { }
if let x = num.first(where: { $0 > 3}) { }
if let x = num.firstIndex(where: { $0 > 5 }) { }
if let x = num.lastIndex(of: 5) { }
if let x = num.last(where: { $0 > 3}) { }
if let x = num.firstIndex(where: { $0 > 5 }) { }
let isContains = num.contains(7)
//最大值
let max = num.min(by: { $0 > $1 })
let max = num.min { (x, y) -> Bool in return x > y }
let max = num.max(by: { $0 < $1 })
let max = num.max { (x, y) -> Bool in return x < y }
//最小值
let min = num.min(by: { $0 < $1 })
let min = num.min { (x, y) -> Bool in return x < y }
let min = num.max(by: { $0 > $1 })
let min = num.max { (x, y) -> Bool in return x > y }
//将元素与另一个数组进行比较
var num2 = [14,25,36,47,52,65,76,78,19]
let isTrue = num.elementsEqual(num2)
let isTrue = num.elementsEqual(num2) { (x, y) -> Bool in return x * 20 < y }
//把所有元素分成多个数组。 [1, 2, 3] [5, 6, 7, 8, 9]
let num2 = num.split(separator: 4)
//从头取元素直到条件不成立。 [1, 2, 3, 4]
let num2 = num.prefix(while: { $0 < 5 })
//当条件为真时,丢弃元素;一旦不为真,返回其余的元素 (和 prefix 类似,不过返回相反的集合)。 [1, 2, 3, 4]
let num2 = num.drop(while: { $0 < 5 })
//将不同数组里的元素进行合并,得到两个数组中元素的所有配对组合。 [("1", "5"), ("1", "6"), ("1", "7"), ("2", "5"), ("2", "6"), ("2", "7"), ("3", "5"), ("3", "6"), ("3", "7")]
let suits = ["1", "2", "3"]
let ranks = ["5", "6", "7"]
let result = suits.flatMap { suit in
ranks.map { rank in
(suit, rank)
}
}
//数组切片
let slice = num[2...]
slice // [3, 4, 5, 6, 7, 8, 9]
type(of: slice) // ArraySlice<Int>
stride函数
let array = Array(1...31)
let arr = stride(from: 0, to: array.count, by: 7).map { (index) -> [Int] in
if (index + 7) > array.count {
return Array(array[index...])
} else {
return Array(array[index..<index+7])
}
}
print(arr)
// [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31]]
let array = Array(1...31)
let arr = stride(from: 0, through: array.count, by: 7).map { (index) -> [Int] in
if (index + 7) > array.count {
return Array(array[index...])
} else {
return Array(array[index..<index+7])
}
}
print(arr)
// [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31]]
let array = Array(1...31)
let step = 7
let arr = stride(from: 0, to: array.count - (array.count % step), by: step).map {
Array(array[$0..<$0+step])
}
print(arr)
// [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28]]