如何在Kotlin中返回多个值?

时间:2019-04-01 11:11:05

标签: android kotlin

我想返回4个值(notDev,lastIndex,lastIndexValue,firstIndexValue)

我尝试了return表达式并将fun输出更改为

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一样返回这些值或完全返回它们?

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

0 个答案:

没有答案