前天我在对数组取最小值的时候,用错了minby的的写法,这次把数组的操作重新记录一下。
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) { 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, +)
//找出整形数组中的最⼤元素。
9let max = num.reduce(num[0], { 1 ? 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([], { [0 })
//翻转⼀个数组。
[9, 8, 7, 6, 5, 4, 3, 2, 1, 20]
let num2 = num.reduce([20]) { (x, y) -> [Int] in return [y] + x }
//起始位置的值(+)和String数组中元素连接⼀个字符串。 +abclet str = ["a","b","c"] let str2 = str.reduce("+", { 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: { 1 })num.sort { (x, y) -> Bool in return x < y } let num2 = num.sorted() let num2 = num.sorted(by: { 1 })
//降序 num.sort(by: { 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")]
//晸大値 let max = num.min by: < 1 ≥ let max = num.min { (x, y) -> Bool in return x > y } let max = num.maxby: { 1 }) let max = num.max { (x, y) -> Bool in return x < y }
1/最小值 let min = num.min(by: { 1 }) let min = num.min { (x, y) -> Bool in return x < y } let min = num.max(by: { 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.elements Equal(num2) let is True = num.elementsEqual(num2) { (x, y) -> Bool in return × * 20 < y }
/把所有元素分成多个数组。 [1,2,3115,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 < 5g)