📜  Scala 设置 dropRight() 方法与示例

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

Scala 设置 dropRight() 方法与示例

dropRight()用于返回除最后“n”个元素之外的所有元素。

示例 #1:

// Scala program of dropRight()
// method
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating sets 
        val s1 = Set(1, 2, 3, 4, 5)
          
        // Applying dropRight method 
        val s2 = s1.dropRight(1) 
          
        // Displays output 
        for(elem <- s2)  
        println(elem) 
      
    } 
} 
输出:
5
1
2
3

示例 #2:

// Scala program of dropRight()
// method
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating sets 
        val s1 = Set(1, 2, 3, 4, 5)
          
        // Applying dropRight method 
        val s2 = s1.dropRight(2) 
          
        // Displays output 
        for(elem <- s2)  
        println(elem) 
      
    } 
} 
输出:
5
1
2