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


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


splitAt()方法属于类List的值成员。它用于在给定位置将给定列表拆分为前缀/后缀对。

函数定义: def splitAt(n: Int): (List[A], List[A])

Where, n is the position at which we need to split.



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

范例1:

// Scala program of splitAt() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating a List  
        val list = List("a", "b", "c", "d", "e", "f") 
          
        // Applying splitAt method  
        val result = list.splitAt(3) 
          
        // Displays output 
        println(result) 
      
    } 
}
输出:
(List(a, b, c), List(d, e, f))

范例2:

// Scala program of splitAt() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating a List  
        val list = List(1, 2, 3, 4, 5, 6, 7) 
          
        // Applying splitAt method  
        val result = list.splitAt(4) 
          
        // Displays output 
        println(result) 
      
    } 
}
输出:
(List(1, 2, 3, 4), List(5, 6, 7))


相关用法


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