拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 快速算法中的ChunksOfCountCollection

快速算法中的ChunksOfCountCollection

白鹭 - 2022-02-11 1970 0 0

在快速算法中,如何获取分块集合的第二项?

let a = ["a", "b", "c", "d", "e"]
let chunked = a.chunks(ofCount: 2) //["a", "b"] ["c", "d"] ["e"]

现在我想得到 ["c", "d"]

我可以使用

chunked.index(chunked.startIndex, offsetBy: 1) //Index(baseRange: Range(2..<4))

但在那之后,我不知道如何得到确切的结果。

请帮忙!

uj5u.com热心网友回复:

你已经得到了索引。只需使用下标语法来访问集合:

chunked[chunked.index(chunked.startIndex, offsetBy: 1)]

这会给你一个ArraySlice<String>. 打印这个会给你["c", "d"]

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *