📜  带有示例的 Scala List dropRight() 方法

📅  最后修改于: 2022-05-13 01:55:47.772000             🧑  作者: Mango

带有示例的 Scala List dropRight() 方法

dropRight()方法用于查找列表中除最后 n 个元素之外的所有元素。

示例 #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()