當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Scala immutable 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.immutable._
  
// 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.immutable._
  
// 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:()



相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Scala immutable TreeSet clear() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。