gpt4 book ai didi

swift - 从数组中提取元素并将它们放回另一个数组。?

转载 作者:行者123 更新时间:2023-11-28 10:13:44 24 4
gpt4 key购买 nike

假设我有一个包含 10 个元素的数组。说,

var ArrayElemts : ["1","2","3","4","5","6","7","8","9","10","11"]

现在如何将 0 到 5 的元素保留在一个数组集中,而将 6 到 10 的元素保留到另一个数组集中?

最佳答案

使用 [0...5] 创建一个 ArraySlice,然后使用 Array 将其转换回数组:

var arrayElemts = ["1","2","3","4","5","6","7","8","9","10","11"]

let first = Array(arrayElemts[0...5])
let second = Array(arrayElemts[6...10])

print(first) // ["1", "2", "3", "4", "5", "6"]
print(second) // ["7", "8", "9", "10", "11"]

关于swift - 从数组中提取元素并将它们放回另一个数组。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44228356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com