带有示例的 Scala TreeSet splitAt() 方法
splitAt()方法用于在指定位置将给定的 TreeSet 拆分为 TreeSet 的前缀/后缀对。
Method Definition: def splitAt(n: Int): (TreeSet[A], TreeSet[A])
Where, n is the position at which we need to split.
Return Type: It returns a pair of TreeSet consisting of the first n elements of this TreeSet, and the other elements.
示例 #1:
// Scala program of splitAt()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(3, 1, 5, 2, 4)
// Print the TreeSet
println(t1)
// Applying splitAt method
val result = t1.splitAt(2)
// Displays output
print(result)
}
}
输出:
TreeSet(1, 2, 3, 4, 5)
(TreeSet(1, 2), TreeSet(3, 4, 5))
示例 #2:
// Scala program of splitAt()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(3, 1, 5, 2, 4)
// Print the TreeSet
println(t1)
// Applying splitAt method
val result = t1.splitAt(3)
// Displays output
print(result)
}
}
输出:
TreeSet(1, 2, 3, 4, 5)
(TreeSet(1, 2, 3), TreeSet(4, 5))