object Test extends App{
println(List(1, 2, 3, 4, 5).partition(_ % 2 == 0))
//find返回Option(Some或None),因为find可能找不到
println(List(1, 2, 3, 4, 5).find(_ % 2 == 0))
println(List(1, 2, 3, 4, 5).find(_ <= 0))
//获取所有符合条件的元素
println(List(1, 2, 3, 4, 5).takeWhile(_ < 4))
//把符合条件的元素drop掉
println(List(1, 2, 3, 4, 5).dropWhile(_ < 4))
//将元素分成符合条件的和不符合条件的两部分
println(List(1, 2, 3, 4, 5).span(_ < 4))
println(List(1, 2, 3, 4, 5))
println(List(1, 2, 3, 4, 5))
//exists: m中至少存在一个元素满足条件f
//forall: 所有元素满足f条件
def hastotatallyZeroRow(m: List[List[Int]]) = m.exists(row => row.forall(_ == 0))
//矩阵
val m = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 0))
println(hastotatallyZeroRow(m))
}scala基础37-List高级方法
最新推荐文章于 2020-07-28 07:34:00 发布
本文介绍了Scala中List的各种操作方法,包括partition、find、takeWhile、dropWhile、span等,并通过具体示例展示了这些方法的应用场景。此外,还提供了一个判断矩阵是否存在全零行的函数实现。
183

被折叠的 条评论
为什么被折叠?



