下面的代码是一个更好的功能习惯替代品吗?即是否有一种更简洁的方法来获取值j而不必使用var?
<div class="txt">
This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text.
</div>
答案 0 :(得分:8)
val j = idxs.drop(i).indexWhere(_ != x) + i
或者,正如@kosii在评论中所建议的那样,使用indexWhere
重载来获取从哪里开始搜索的索引:
val j = idxs.indexWhere(_ != x, i)
修改强>
由于j
必须等于idxs
的长度,以防i
后的所有项都等于x
:
val index = idxs.indexWhere(_ != x, i)
val j = if(index < 0) idxs.length else index
// or
val j = if (idxs.drop(i).forall(_ == x)) idxs.length
else idxs.indexWhere(_ != x, i)
答案 1 :(得分:0)
也许使用溪流,例如:
a = True