带有示例的 Scala List dropRight() 方法
dropRight()方法用于查找列表中除最后 n 个元素之外的所有元素。
Method Definition: def dropRight(n: Int): List[A]
Return Type: It returns all the elements of the list except the last n elements.
示例 #1:
// Scala program of dropRight()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 1, 3, 3, 3, 5, 4, 5, 2)
// Applying dropRight method
val res = m1.dropRight(3)
// Displays output
println(res)
}
}
输出:
List(1, 1, 3, 3, 3, 5)
示例 #2:
// Scala program of dropRight()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a list
val m1 = List(1, 1, 3, 3, 3, 5, 4, 5, 2)
// Applying dropRight method
val res = m1.dropRight(10)
// Displays output
println(res)
}
}
输出:
List()