Kotlin-检查数组是否包含值的惯用方式 [英] Kotlin - Idiomatic way to check array contains value
本文介绍了Kotlin-检查数组是否包含值的惯用方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查字符串数组是否包含Kotlin中的值的惯用方式是什么?就像红宝石的#include?
.
What's a idiomatic way to check if an array of strings contains a value in kotlin? Just like ruby's #include?
.
我大概是:
array.filter { it == "value" }.any()
有更好的方法吗?
推荐答案
The equivalent you are looking for is the contains operator.
array.contains("value")
Kotlin为此操作员提供了另一种缀标记:>
Kotlin offer an alternative infix notation for this operator:
"value" in array
与在后台调用的功能相同,但是由于缀标记在Java中找不到,我们可以说in
是最惯用的方式.
It's the same function called behind the scene, but since infix notation isn't found in Java we could say that in
is the most idiomatic way.
这篇关于Kotlin-检查数组是否包含值的惯用方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文