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
极客教程