带有示例的 Scala SortedMap copyToArray() 方法
copyToArray()方法用于将 SortedMap 的键对复制到数组中。
Method Definition: def copyToArray(xs: Array[(A, B)]): Unit
Return Type: It returns the keys of the SortedMap to an array.
示例 #1:
// Scala program of copyToArray()
// method
import scala.collection.immutable.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "cs" -> 2)
// Creating Array
val x: Array[Any] = Array(0, 0, 0, 0, 0)
// using 'copyToArray' method
m1.copyToArray(x)
// Displays keys copied in
// the Array
for(m2 <-x)
println(m2)
}
}
输出:
(cs, 2)
(for, 3)
(geeks, 5)
0
0
所以,这里的键被复制到数组中。
示例 #2:
// Scala program of copyToArray()
// method
import scala.collection.immutable.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3, "geeks" -> 5)
// Creating Array
val x: Array[Any] = Array(0, 0, 0, 0, 0)
// using 'copyToArray' method
m1.copyToArray(x)
// Displays keys copied in
// the Array
for(m2 <-x)
println(m2)
}
}
输出:
(for, 3)
(geeks, 5)
0
0
0
在这里,相同的键被删除。