当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala SortedSet splitAt()用法及代码示例


splitAt()方法用于在给定位置将给定的SortedSet分为一个前缀/后缀对。

函数定义: def splitAt(n: Int): (SortedSet[A], SortedSet[A])
Where, n is the position at which we need to split.

返回类型: It returns a pair of TreeSets consisting of the first n elements of this SortedSet, and the other elements.



范例1:

// Scala program of splitAt()  
// method  
import scala.collection.immutable.SortedSet  
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a SortedSet  
        val s1 = SortedSet(2, 4, 11, 31)  
          
        // Applying splitAt method  
        val result = s1.splitAt(2) 
          
        // Display output 
        println(result) 
    }  
} 
输出:
(TreeSet(2, 4), TreeSet(12, 31))

范例2:

// Scala program of splitAt()  
// method  
import scala.collection.immutable.SortedSet  
  
// Creating object  
object GfG  
{  
  
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a SortedSet  
        val s1 = SortedSet(1, 2, 3, 4)  
          
        // Applying splitAt method  
        val result = s1.splitAt(2) 
          
        // Display output 
        println(result) 
    }  
} 
输出:
(TreeSet(1, 2), TreeSet(3, 4))


相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Scala SortedSet splitAt() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。