joh*_*crq 6 ternary-operator kotlin
所以在java中我们有三元运算符(?),它有时很容易通过if-else内联计算一些值.例如:
myAdapter.setAdapterItems(
textToSearch.length == 0
? noteList
: noteList.sublist(0, length-5)
)
Run Code Online (Sandbox Code Playgroud)
我知道kotlin中的等价物是:
myAdapter.setAdapterItems(
if(textToSearch.length == 0)
noteList
else
noteList.sublist(0, length-5)
)
Run Code Online (Sandbox Code Playgroud)
但我曾经习惯于喜欢Java中的三元运算符,用于短表达式条件,以及将值传递给方法时.有没有Kotlin等价物?
Eri*_*ran 15
Kotlin没有三元运营商.
https://kotlinlang.org/docs/reference/control-flow.html
在Kotlin中,if是一个表达式,即它返回一个值.因此没有三元运算符(condition?then:else),因为普通的if在这个角色中工作正常.
归档时间: |
|
查看次数: |
8064 次 |
最近记录: |