Repeating array in Swift

后端 未结 4 414
滥情空心
滥情空心 2021-01-12 12:11

In Python I can create a repeating list like this:

>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

Is there a concise way to do this in

4条回答
  •  日久生厌
    2021-01-12 13:07

    Solution 1:

    func multiplerArray(array: [Int], time: Int) -> [Int] {
        var result = [Int]()
        for _ in 0..

    Call this

    print(multiplerArray([1,2,3], time: 3)) // [1, 2, 3, 1, 2, 3, 1, 2, 3]
    

    Solution 2:

    let arrays = Array(count:3, repeatedValue: [1,2,3])
    // [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
    var result = [Int]()
    for array in arrays {
        result += array
    }
    print(result) //[1, 2, 3, 1, 2, 3, 1, 2, 3]
    

提交回复
热议问题