Kotlin有很多速记和有趣的功能.所以,我想知道是否有一些快捷的方法可以将字符串数组转换为整数array.类似于Python中的以下代码:
results = [int(i) for i in results]
Kotlin有很多速记和有趣的功能.所以,我想知道是否有一些快捷的方法可以将字符串数组转换为整数array.类似于Python中的以下代码:
results = [int(i) for i in results]
You can use .map { ... }
with .toInt()
or .toIntOrNull()
:
val result = strings.map { it.toInt() }
Only the result is not an array but a list. It is preferable to use lists over arrays in non-performance-critical code, see the differences.
If you need an array, add .toTypedArray()
or .toIntArray()
.