美文网首页Swift
swift 数组操作

swift 数组操作

作者: gaookey | 来源:发表于2020-08-21 11:06 被阅读0次
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]]

相关文章

  • 迟到的Swift入门 - 数组操作

    Swift数组操作 1. 数组的日常操作 1.0 声明数组 初始化空数组 初始化默认值的数组 2. 数组基本操作 ...

  • 关于swift数组

    今天在对swift数组进行操作的时候,发现swift数组中元素的拷贝竟是直接的地址拷贝!当时,我需要新建一个数组,...

  • swift 数组操作

    数组: *懒加载 *数组加载元素homeListArray.append(item)*数组遍历

  • swift 数组操作

    stride函数[https://www.jianshu.com/p/aa6d9d873b5a]

  • swift 数组操作

    1.便利数组 2.便利除了数组中第一个元素以外的元素,使用dropFirst 3.便利除了数组中后3个元素(根据需...

  • 2019-05-05: 五:Swift中数组的使用?

    一:Swift中数组的使用? 二:数组的介绍? 三:数组的初始化? 四:对数组的基本操作? 五:数组的遍历? 六;...

  • swift中数组操作

    数组的声明 可变关键字用var,不可变关键字用let,默认用一对[]表示 swift数组拼接操作 swift的值拷...

  • swift sorted 排序函数

    swift 提供了便捷的快速排序数组、字典的函数 sorted( )所有操作都在 swift 3.0 下完成 1....

  • Swift学习系列 数组

    一、数组的基本操作 1、数组的定义 Swift中的数组跟OC类似 分为可变数组和不可变数组。数据类型为Array。...

  • swift数组扩展

    swift数组移除元素 swift数组拷贝

网友评论

    本文标题:swift 数组操作

    本文链接:https://www.haomeiwen.com/subject/nghujktx.html