Swift中的集合类之数组
好丟人,最近寫了一個摸不著東南西北的bug
說白了還是欠缺火候,下面總結(jié)一下Swift中的集合類。。。
空數(shù)組:
[類型]()
let array1 = [String]() ?
Array<類型>()
let array2 = Array<String>()
var array3 = Array<String>()
數(shù)組遍歷:
for-in:
forEach:
同時得到索引和值enumerated()
使用Interator遍歷數(shù)組
startIndex 返回第一個元素的位置,對于數(shù)組來說永遠(yuǎn)都是0
endIndex 返回最后一個元素索引+1的位置,對于數(shù)組來說等同于count
數(shù)組查找:
判斷是否包含指定元素
let array1 = [Int](1...8)
let isExist = array1.contains(5)
print("1\(isExist)")
let exist2 = array1.contains { (e) -> Bool in
? ? print("每個元素\(e)")
? ? return e > 2
}
print("2\(exist2)")
let exist3 = array1.contains { (e) -> Bool in
? ? e > 3
}
print("3\(exist3)")
let exist4 = array1.contains(where: {$0 > 4})
print("4\(exist4)")
判斷所有元素符合某個條件
let array1 = [Int](1...8)
let isExist = array1.allSatisfy { (e) -> Bool in
? ? return e > 0
}
print("是否都大于0:\(isExist)")
查找元素
let array1 = [Int](1...8)
let first = array1.first { (e) -> Bool in
? ? print("第一個大于3的數(shù)\(e)")
? ? return e > 3
}
let last = array1.last { (e) -> Bool in
? ? print("最后一個大于3的數(shù)\(e)")
? ? return e > 3
}
輸出:
第一個大于3的數(shù)1
第一個大于3的數(shù)2
第一個大于3的數(shù)3
第一個大于3的數(shù)4
最后一個大于3的數(shù)8
查找索引:
firstIndex(of)返回指定元素在數(shù)組中出現(xiàn)的第一個位置
lastIndex(of: 5)返回指定元素在數(shù)組中出現(xiàn)的最后一個位置
let firstIndex = array1.firstIndex(of: 0)
print("0出現(xiàn)的第一個位置\(firstIndex)")
let lastIndex = array1.lastIndex(of: 5)
print("5出現(xiàn)的最后一個位置\(lastIndex ?? -1)")
輸出:
0出現(xiàn)的第一個位置nil
5出現(xiàn)的最后一個位置4
?
let num3 = array1.firstIndex { (e) -> Bool in
? ? e > 4
}
print("大于4的第一個數(shù)字位置\(num3 ?? -1)")
let num4 = array1.lastIndex { (e) -> Bool in
? ? e > 4
}
print("大于4的最后一個數(shù)字位置\(String(describing: num4))")
輸出:
大于4的第一個數(shù)字位置4
大于4的最后一個數(shù)字位置Optional(7)
數(shù)組添加和刪除:
var numbers = [Int](arrayLiteral: 2,3,5,6,7)
在末尾添加一個元素
numbers.append(9)
在末尾添加多個元素
numbers.append(contentsOf: [4,6])
在指定位置插入一個元素
numbers.insert(1, at: 0)
在指定位置插入多個元素
numbers.insert(contentsOf: [4,6], at: 0)
移除并返回指定位置的一個元素
numbers.remove(at: 3)
移除并返回數(shù)組的第一個元素
let first =? numbers.removeFirst()
移除并返回數(shù)組的最后一個元素
let last = numbers.removeLast()
移除并返回數(shù)組的最后一個元素(Optional),如果數(shù)組為空返回nil
let last2 = numbers.popLast()
移除數(shù)組前面多個元素
numbers.removeFirst(3)
移除數(shù)組后面多個元素
numbers.removeLast(3)
移除數(shù)組中指定范圍的元素
numbers.removeSubrange(2...5)
移除數(shù)組中的所有元素
numbers.removeAll()?// 數(shù)組容量.capacity為0
移除數(shù)組中的所有元素并保持容量
numbers.removeAll(keepingCapacity: true)
數(shù)組片段ArraySlice
var numbers = [Int](arrayLiteral: 2,3,5,6,7,9,7,0)
numbers.sort()
let lastA = numbers.drop { (e) -> Bool in
? ? print("\(e)")
? ? if e < 5 {
? ? ? ? return true
? ? }else{
? ? ? ? return false
? ? }
}
print("numbers = \(numbers)")
print("lastA = \(lastA)")
輸出結(jié)果:
0
2
3
5
numbers = [0, 2, 3, 5, 6, 7, 7, 9]
lastA = [5, 6, 7, 7, 9]
drop(:) 移除原數(shù)組符合指定條件的元素得到一個ArraySlice
細(xì)解:跳過一個元素之后,其余元素生成一個新序列,原序列不變。如果該元素應(yīng)該被跳過,則閉包返回true,否則返回false。一旦返回false,它蔣不再被調(diào)用
除上面之外還有.prefix ?.suffix ?Range:?numbers[2...4]
數(shù)組的分組
var numbers = [Int](arrayLiteral: 2,3,5,6,7,9,7,0)
let index = numbers.partition { (e) -> Bool in
? ? return e > 3
}
let lastA = numbers[..<index]
let lastB = numbers[index...]
print("numbers = \(index) \(numbers)")
print("lastA = \(lastA)")
print("lastB = \(lastB)")
index是分組條件的開始位置,index前面的部分不滿足條件,index后面的部分滿足條件
數(shù)組的排序
numbers.sort()
numbers.sorted()
交換指定位置的兩個元素?numbers.swapAt(2, 4)
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Swift中的集合类之数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Swift中的模式分类
- 下一篇: swift中的@objc