android - 如何在 kotlin 中返回多个值?
问题描述
我想返回 4 个值(notDev、lastIndex、lastIndexValue、firstIndexValue)
我尝试了返回表达式并将有趣的输出更改为
fun whenNotZero(): (Int, Int, Int, Int) -> Unit{//rest of the code
return{notDiv, lastIndex, lastIndexValue, firstIndexValue}
}
然后我试着这样做
fun whenNotZero(order:Int): Int {//rest of the code
if(order == 1){return notDiv}
if(order == 2){return lastIndex}
if(order == 3){return lastIndexValue}
if(order == 4){return firstIndexValue}
}
有什么方法可以像 python 一样返回这些值或完全返回它们?
蟒蛇代码
def foo():
# code here
return(notDiv, lastIndex, lastIndexValue, firstIndexValue)
values = foo()
print(values[0])
我当前的 kotlin 代码
fun whenNotZero() {
val seq = mutableListOf<Int>()
val inDevSeq = mutableListOf<Int>()
for (i in range1..range2) {
if (i % divisible_number == 0){inDevSeq.add(i);seq.add(i)}else{seq.add(i)}
}
val notDiv = seq.sum() - inDevSeq.sum()
val lastIndex = inDevSeq.count()
val lastIndexValue = inDevSeq.last()
val firstIndexValue = inDevSeq.first()
}
解决方案
推荐阅读
- javascript - 不允许表单提交,而是发送一个 xmlHttpRequest
- laravel - Summernote 图片上传出现 SQL 错误
- objective-c - 使用 NSTask 执行 shell 脚本文件 - Objective-C
- swift - 编辑并保存配置文件错误线程 1:致命错误:在展开可选值时意外发现 nil
- wordpress - 某些帖子和页面上未显示管理栏
- apache-spark - 严格流中数据集中列的不同计数
- java - WebFilter:如何使用西里尔字符指定 urlPattern?
- c# - 如何修复从右到左的绘制矩形
- angular - 如何在 Angular 4 的下拉菜单中正确绑定枚举?
- angular - 我可以在 get 方法中更改 Date 类型的属性吗?