Split large Array in Array of two elements

前端 未结 7 2113
借酒劲吻你
借酒劲吻你 2020-12-20 17:53

I have large list of objects and I need to split them in a group of two elements for UI propouse.

Example:

[0, 1, 2, 3, 4, 5, 6]

Becomes

7条回答
  •  没有蜡笔的小新
    2020-12-20 18:14

    Maybe not the most efficient solution, but the most direct solution:

    func toPairs(numbers:[Int])->[[Int]]
    {
        var pairs:[[Int]]=[]
        var pair:[Int]=[]
        for var index=0;index

    Output on my laptop:

    [[0, 1], [2, 3], [4, 5]]
    Program ended with exit code: 0
    

提交回复
热议问题