Scala SortedSet dropRight()方法及示例

Scala SortedSet dropRight()方法及示例

dropRight() 是用来返回除最后’n’个元素以外的所有元素。

方法定义: def dropRight(n: Int):SortedSet[A]

返回类型。它返回一个包含除最后’n’元素以外的所有元素的排序集。

例子 #1:

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

输出。

1
2
3
4

例子#2。

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

输出。

1
2
3

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程