Scala 不可变 TreeSet init() 方法
在 Scala 不可变TreeSet class
中,使用init()方法返回一个新的 TreeSet,它由除最后一个元素之外的所有元素组成。
Method Definition: def init: TreeSet[A]
Return Type: It returns a new TreeSet that consists of all the elements except the last one.
示例 #1:
// Scala program of init()
// method
// Import TreeSet
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(1, 3, 2, 7, 6, 5)
// Print the TreeSet
println(t1)
// Applying init() method
val result = t1.init
// Displays output
println("Elements of the TreeSet except the last one: " + result)
}
}
输出:
TreeSet(1, 2, 3, 5, 6, 7)
Elements of the TreeSet except the last one: TreeSet(1, 2, 3, 5, 6)
示例 #2:
// Scala program of init()
// method
// Import TreeSet
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(1, 3, 12, 7, 16, 5)
// Print the TreeSet
println(t1)
// Applying init() method
val result = t1.init
// Displays output
println("Elements of the TreeSet except the last one: " + result)
}
}
输出:
TreeSet(1, 3, 5, 7, 12, 16)
Elements of the TreeSet except the last one: TreeSet(1, 3, 5, 7, 12)