3 回答

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個贊
您可以使用串聯(lián)數(shù)組+,建立一個新數(shù)組
let c = a + b
print(c) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
或使用+=(或append)將一個數(shù)組附加到另一個數(shù)組:
a += b
// Or:
a.append(contentsOf: b) // Swift 3
a.appendContentsOf(b) // Swift 2
a.extend(b) // Swift 1.2
print(a) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個贊
使用Swift 5,您可以根據(jù)需要選擇以下六種方法之一來串聯(lián)/合并兩個數(shù)組。
#1 使用Array的+(_:_:)通用運(yùn)算符將兩個數(shù)組合并為一個新數(shù)組
Array有一個+(_:_:)通用運(yùn)算符。+(_:_:)具有以下聲明:
通過將集合的元素和序列連接起來,創(chuàng)建一個新的集合。
static func + <Other>(lhs: Array<Element>, rhs: Other) -> Array<Element> where Other : Sequence, Self.Element == Other.Element
以下Playground示例代碼顯示了如何[Int]使用+(_:_:)通用運(yùn)算符將兩個類型的數(shù)組合并為一個新數(shù)組:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenArray = array1 + array2
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
#2。使用Array的+=(_:_:)通用運(yùn)算符將數(shù)組的元素追加到現(xiàn)有數(shù)組中
Array有一個+=(_:_:)通用運(yùn)算符。+=(_:_:)具有以下聲明:
將序列的元素追加到范圍可替換的集合中。
static func += <Other>(lhs: inout Array<Element>, rhs: Other) where Other : Sequence, Self.Element == Other.Element
以下Playground示例代碼顯示了如何[Int]使用+=(_:_:)泛型運(yùn)算符將類型數(shù)組的元素附加到現(xiàn)有數(shù)組中:
var array1 = [1, 2, 3]
let array2 = [4, 5, 6]
array1 += array2
print(array1) // prints [1, 2, 3, 4, 5, 6]
#3。使用Array的append(contentsOf:)方法將一個數(shù)組附加到另一個數(shù)組
Swift Array有一個append(contentsOf:)方法。append(contentsOf:)具有以下聲明:
將序列或集合的元素添加到此集合的末尾。
mutating func append<S>(contentsOf newElements: S) where S : Sequence, Self.Element == S.Element
以下Playground示例代碼展示了如何[Int]使用append(contentsOf:)方法將一個數(shù)組追加到另一個類型的數(shù)組:
var array1 = [1, 2, 3]
let array2 = [4, 5, 6]
array1.append(contentsOf: array2)
print(array1) // prints [1, 2, 3, 4, 5, 6]
#4。使用Sequence的flatMap(_:)方法將兩個數(shù)組合并為一個新數(shù)組
Swift為flatMap(_:)符合Sequence協(xié)議(包括Array)的所有類型提供了一種方法。flatMap(_:)具有以下聲明:
返回一個包含此序列的每個元素的調(diào)用給定轉(zhuǎn)換的串聯(lián)結(jié)果的數(shù)組。
func flatMap<SegmentOfResult>(_ transform: (Self.Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence
以下Playground示例代碼顯示了如何[Int]使用flatMap(_:)方法將兩個類型的數(shù)組合并為一個新數(shù)組:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenArray = [array1, array2].flatMap({ (element: [Int]) -> [Int] in
return element
})
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
#5。使用Sequence的joined()方法和初始化器將兩個數(shù)組合并成一個新的數(shù)組Array。init(_:)
Swift為joined()符合Sequence協(xié)議(包括Array)的所有類型提供了一種方法。joined()具有以下聲明:
返回此序列序列的元素,串聯(lián)在一起。
func joined() -> FlattenSequence<Self>
此外,Swift Array有一個init(_:)初始化程序。init(_:)具有以下聲明:
創(chuàng)建一個包含序列元素的數(shù)組。
init<S>(_ s: S) where Element == S.Element, S : Sequence
因此,下面的Playground示例代碼展示了如何[Int]使用joined()method和init(_:)initializer 將兩個類型的數(shù)組合并為一個新數(shù)組:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenCollection = [array1, array2].joined() // type: FlattenBidirectionalCollection<[Array<Int>]>
let flattenArray = Array(flattenCollection)
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]
#6。使用Array的reduce(_:_:)方法將兩個數(shù)組合并為一個新數(shù)組
雨燕Array有一種reduce(_:_:)方法。reduce(_:_:)具有以下聲明:
返回使用給定的閉包組合序列元素的結(jié)果。
func reduce<Result>(_ initialResult: Result, _ nextPartialResult: (Result, Element) throws -> Result) rethrows -> Result
以下Playground代碼顯示了如何[Int]使用reduce(_:_:)方法將兩個類型的數(shù)組合并為一個新數(shù)組:
let array1 = [1, 2, 3]
let array2 = [4, 5, 6]
let flattenArray = [array1, array2].reduce([], { (result: [Int], element: [Int]) -> [Int] in
return result + element
})
print(flattenArray) // prints [1, 2, 3, 4, 5, 6]

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個贊
如果您不是運(yùn)算符重載的忠實(shí)擁護(hù)者,或者不是更多的功能類型:
// use flatMap
let result = [
["merge", "me"],
["We", "shall", "unite"],
["magic"]
].flatMap { $0 }
// Output: ["merge", "me", "We", "shall", "unite", "magic"]
// ... or reduce
[[1],[2],[3]].reduce([], +)
// Output: [1, 2, 3]
- 3 回答
- 0 關(guān)注
- 3388 瀏覽
添加回答
舉報(bào)