我期待一个转换List<String>成List<Int>在科特林.
val stringArray = "1 2 3 4 5 6".split(" ")
val intArray = MutableList<Int>(0, {0})
for (item in stringArray) {
intArray.add(item.toInt())
}
Run Code Online (Sandbox Code Playgroud)
上面会这样做,但感觉好像有更好的方法这样做(可能与lambda和流?).
如果你的输入真的有分隔符,你可以split像已经建议的那样.之后map就是你需要的.这样就可以了:
val numbers = "1 2 3 4 5 6"
val result = numbers.split(" ").map(String::toInt)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
719 次 |
| 最近记录: |