英文:
Kotlin basic math on each item of a list
问题
I'm not really understanding how to use Lists in Kotlin correctly, for even simple things.
Say I've got a list of Doubles, and I want a list that is 2 times as big. So for [1.0, 2.0, 3.0] I want [2.0, 4.0, 6.0] as the output.
What's the "correct" way to do that?
我不太明白如何正确在Kotlin中使用列表,即使是简单的事情。
假设我有一个Double类型的列表,我想要一个大小是原列表两倍的列表。所以对于[1.0, 2.0, 3.0],我希望输出是[2.0, 4.0, 6.0]。
什么是“正确”的做法?
I can figure out a way to go stepwise through the List and add each part to a new mutable list like so:
fun main() {
val list1 = listOf<Double>(1.0, 2.0, 3.0, 4.0)
val list2 = mutableListOf<Double>()
list1.forEach{
list2.add(it * 2.0)
}
print(list1)
print(list2)
}
https://pl.kotl.in/WORk6Wdqu for that code in a playground.
But it seems like this is "not the right way to do it," as in it would go very slow on big lists, or just takes a lot of lines compared to the right way, or that we now have two lists (in the end, I will just be shoving the values back into my "list1")
我可以想出一种逐步遍历列表并将每个部分添加到新的可变列表的方法,如下所示:
fun main() {
val list1 = listOf<Double>(1.0, 2.0, 3.0, 4.0)
val list2 = mutableListOf<Double>()
list1.forEach{
list2.add(it * 2.0)
}
print(list1)
print(list2)
}
您可以在https://pl.kotl.in/WORk6Wdqu 看到这段代码在Playground中的演示。
但是似乎这不是“正确的做法”,因为在大型列表上运行速度会很慢,或者与正确的方法相比需要很多行代码,或者最终我们现在有两个列表(最终,我只会将值放回到我的“list1”中)。
英文:
I'm not really understanding how to use Lists in Kotlin correctly, for even simple things.
Say I've got a list of Doubles, and I want a list that is 2 times as big. So for [1.0, 2.0, 3.0] I want [2.0, 4.0, 6.0] as the output.
What's the "correct" way to do that?
I can figure out a way to go stepwise through the List and add each part to a new mutable list like so:
fun main() {
val list1 = listOf<Double>(1.0,2.0,3.0,4.0)
val list2 = mutableListOf<Double>()
list1.forEach{
list2.add(it*2.0)
}
print(list1)
print(list2)
}
https://pl.kotl.in/WORk6Wdqu for that code in a playground.
But it seems like this is "not the right way to do it", as in it would go very slow on big lists, or just takes a lot of line compared to the right way, or that we now have two lists (in the end, I will just be shoving the values back into my "list1")
答案1
得分: 4
map
将表达式应用于每个项,并从结果生成新列表。
val list1 = listOf<Double>(1.0, 2.0, 3.0, 4.0)
val list2 = list1.map { it * 2 }
> 或者现在我们有两个列表(最后,我将把值塞回我的 "list1")
如果你真的有非常大的列表,你甚至可能不想要其中之一,可以考虑使用 sequences
。但我认为你可能过于急躁,担心过多。
尽量将事物保持为不可变对象是一个好习惯,但这意味着有时需要创建新列表。
英文:
map
applies the expression to each item and makes a new list from the result.
val list1 = listOf<Double>(1.0,2.0,3.0,4.0)
val list2 = list1.map { it * 2 }
> or that we now have two lists (in the end, I will just be shoving the values back into my "list1")
If you really have very large lists, you don't even want one of them and might want to look at sequences
. But I think you're jumping the gun and worrying too much.
Keeping things in immutable objects as much as possible is good practice, but that means making new lists sometimes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论