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


Scala Mutable SortedMap toArray()用法及代碼示例


toArray()方法用於顯示Scala SortedMap中的數組。

Method Definition: def toArray:Array[(A, B)]

Return Type: It returns an array from the stated SortedMap.

範例1:

// Scala program of toArray() 
// method 
import scala.collection.SortedMap 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating a SortedMap 
        val m1 = SortedMap(3 -> "geeks", 4 -> "for", 4 -> "for") 
          
        // Applying toArray method 
        val result = m1.toArray 
          
        for ( m5 <-result ) 
        { 
           println(m5 ) 
        }  
  
    } 
}
輸出:
(3, geeks)
(4, for)

範例2:

// Scala program of toArray() 
// method 
import scala.collection.SortedMap 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating a SortedMap 
        val m1 = SortedMap(3 -> "geeks", 4 -> "for", 2 -> "cs") 
          
        // Applying toArray method 
        val result = m1.toArray 
          
        for ( m5 <-result ) 
        { 
            // Display output 
            println(m5 ) 
        }  
          
    } 
}
輸出:
(2, cs)
(3, geeks)
(4, for)



相關用法


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