带有示例的 Scala 列表 dropWhile() 方法
dropWhile()方法用于删除满足规定条件的元素的最长前缀。
Method Definition: def dropWhile(p: (A) => Boolean): List[A]
Return Type: It returns all the elements of the list except the dropped ones.
示例 #1:
// Scala program of dropWhile()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 3, 5, 4, 2)
// Applying dropWhile method
val res = m1.dropWhile(x=>{x % 2 != 0})
// Displays output
println(res)
}
}
输出:
List(4, 2)
示例 #2:
// Scala program of dropWhile()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(3, 6, 5, 4, 2)
// Applying dropWhile method
val res = m1.dropWhile(x=>{x % 3 == 0})
// Displays output
println(res)
}
}
输出:
List(5, 4, 2)