object D03_获取指定元素的索引 {
def main(args: Array[String]): Unit = {
val seq = Seq(1, 2, 4, 6, 4, 3, 2)
// 获取元素2在集合中第一次出现的索引
println(seq.indexOf(2))
// 获取元素2在集合中最后一次出现的索引
println(seq.lastIndexOf(2))
println("-" * 15)
// 获取集合中小于5的第一个偶数的索引
println(seq.indexWhere(x => x < 5 && x % 2 == 0))
// 从索引2位置开始查找集合中小于5的第一个偶数的索引
/**
* 参数1:查找规则
* 参数2:从哪个索引开始找
*/
println(seq.indexWhere(x => x < 5 && x % 2 == 0, 2))
// 获取集合中,小于5的最后一个偶数的索引
println(seq.lastIndexWhere(x => x < 5 && x % 2 == 0))
println("-" * 15)
// 获取子序列Seq(1, 2)在集合seq中,第一次出现的索引
println(seq.indexOfSlice(Seq(1, 2)))
// 从索引3开始查找子序列Seq(1, 2)在集合seq中,第一次出现的索引
println(seq.indexOfSlice(Seq(1, 2), 3))
}
}
11-15
1163
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)