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().

Kotlin相关问答推荐

Microronaut Data 4和JDbi

T和T有什么区别:任何>

TzdbZoneRulesProvider 在 java.time 中不工作

Kotlin 中的 maxOf() 和 max() 方法有什么区别?

在 kotlin 原始字符串中转义三重引号

在 Kotlin 中 import 如何找到文件路径?

Kotlin 插件之间的区别

为空数组添加值

从字符串列表构建字符串

使用 Kotlin 协程时 Room dao 类出错

如何使用 Hilt 注入应用程序:ViewModel 中的上下文?

AIDL 中的 Parcelize 注释:Incompatible types: Object cannot be converted to MyCustomObject

无法创建类 ViewModel kotlin 的实例

androidx.core:core-ktx:1.0.0 小部件包丢失

将ExpectedException与Kotlin一起使用

使用Dagger 2提供函数依赖性

Kotlin中的Memoization功能

如何将 CameraView 与 Jetpack Compose 一起使用?

旋转 kotlin 数组

从另一个列表创建一个列表