如何从 Kotlin 列表列表中的特定索引创建列表?

How to create a list from specific indexes in list of lists in Kotlin?

我想检索一个列表,其中包含 Kotlin 列表列表中特定索引处的所有元素,例如:

List1 = [a,b,c,e]
List2 = [1,2,3,4]
List3 = [!,@,#,$]

Input = [List1,List2,List3]
Index = 2

Desired output: [c,3,#]

如何在 Kotlin 中高效?有没有比遍历列表更好的方法? 谢谢

Input.map { it[Index] }

它遍历所有子列表并从每个子列表中获取 Index 个元素。

请注意,创建列表的列表会丢失有关每个子列表类型的信息。您需要重新设计数据结构以保留此信息。