Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List Kotlin replace isEmpty()&;集合中的last()和lastOrNull()_List_Arraylist_Collections_Kotlin - Fatal编程技术网

List Kotlin replace isEmpty()&;集合中的last()和lastOrNull()

List Kotlin replace isEmpty()&;集合中的last()和lastOrNull(),list,arraylist,collections,kotlin,List,Arraylist,Collections,Kotlin,我想使用类似(下面的代码)的东西,但我认为使用lastOrNull()必须有更好的解决方案,而不是使用isEmpty和last() var条目:MutableList=ArrayList() 如果(有些){ 条目。添加(条目(100f、200f) } val foo=(if(entries.isEmpty())0f else entries.last().y)+100f 是否有更好的方法,如entries.lastOrNull()?.y如果空0f?您可以使用Kotlin?:,例如: //

我想使用类似(下面的代码)的东西,但我认为使用
lastOrNull()
必须有更好的解决方案,而不是使用
isEmpty
last()


var条目:MutableList=ArrayList()
如果(有些){
条目。添加(条目(100f、200f)
}
val foo=(if(entries.isEmpty())0f else entries.last().y)+100f
是否有更好的方法,如
entries.lastOrNull()?.y如果空0f

您可以使用Kotlin
?:
,例如:

//   if the expression `entries.lastOrNull()?.y` is null then return `0f` 
//                                  v              
val lastY = entries.lastOrNull()?.y ?: 0f
//val foo = if (entries.isEmpty()) 0f else entries.last().y + 100f else 100f

//             if no Entry in List return `0F`  ---v
val foo = entries.lastOrNull()?.run { y + 100 } ?: 0F 
//                            ^--- otherwise make the last.y + 100  
对于上面代码中的表达式,您可以使用
?。让
/
?。运行
使代码更清晰,例如:

//   if the expression `entries.lastOrNull()?.y` is null then return `0f` 
//                                  v              
val lastY = entries.lastOrNull()?.y ?: 0f
//val foo = if (entries.isEmpty()) 0f else entries.last().y + 100f else 100f

//             if no Entry in List return `0F`  ---v
val foo = entries.lastOrNull()?.run { y + 100 } ?: 0F 
//                            ^--- otherwise make the last.y + 100  

如果我能正确理解你的意思,这就可以了:

val foo = if (entries.isEmpty()) 0f else entries.lastOrNull()?.y ?: 0f + 100f

我的问题是,为什么要使用run而不是let呢?@ligi hi,
run
取a,
let
取接收器作为参数。因此,如果使用
let
,则必须将代码更改为
?。让{it.y+100}
it
是参数的一个隐式变量。您可以选择您喜欢的一个。谢谢-类似情况下有这么多选项;-)运行/let/with/apply-仍在努力使用它-只是想让它读起来更漂亮/更自然-谢谢您的支持feedback@ligi是 啊但它们适用于不同的情况
apply
将返回
receiver
而不是lambda body中的结果<代码>with在这里不合适。是的,这里的问题只在run和let之间-我想我还是更喜欢let-就像它读起来更自然的“就这样吧”-run在这里并没有真正对我说话-但仍然试图找出最佳实践
条目。isEmpty()
表达式是不必要的。如果它不是空的,则总是返回
last.y+100
,否则返回
0f
。而且
列表
包含
条目
而不是
条目
,这里不需要使用
last?.y
。这句话很好。我忽略了列表包含不可为空的条目这一事实。谢谢。很抱歉我迟到了,我的网络不好。一点也不。我只想让你知道。