Scala 设置 splitAt() 方法与示例
splitAt()方法用于将给定的集合拆分为指定位置的前缀/后缀对。
Method Definition: def splitAt(n: Int): (Set[A], Set[A])
Where, n is the position at which we need to split.
Return Type: It returns a pair of sets consisting of the first n elements of this set, and the other elements.
示例 #1:
// Scala program of splitAt()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(4, 12, 2, 31)
// Applying splitAt method
val result = s1.splitAt(2)
// Display output
println(result)
}
}
输出:
(Set(4, 12), Set(2, 31))
示例 #2:
// Scala program of splitAt()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(1, 2, 3, 4)
// Applying splitAt method
val result = s1.splitAt(2)
// Display output
println(result)
}
}
输出:
(Set(1, 2),Set(3, 4))