带有示例的 Scala TreeSet clear() 方法
在 Scala TreeSet class
中,使用clear()方法删除 TreeSet 的所有元素。
Method Definition: def clear(): Unit
Return Type: It returns an empty TreeSet.
示例 #1:
// Scala program of clear()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(2, 1, 3, 4, 5)
// Print the TreeSet
println("Before clear method: " + t1)
// Applying clear() method
val result = t1.clear
// Displays output
println("After clear method: " + result)
}
}
输出:
Before clear method: TreeSet(1, 2, 3, 4, 5)
After clear method: ()
示例 #2:
// Scala program of clear()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet("a", "e", "i", "o", "u")
// Print the TreeSet
println("Before clear method: " + t1)
// Applying clear() method
val result = t1.clear
// Displays output
println("After clear method: " + result)
}
}
输出:
Before clear method: TreeSet(a, e, i, o, u)
After clear method: ()