Scala SortedSet copyToArray()方法及示例
copyToArray() 方法是用来将排序集的元素复制到数组中。
方法定义: def copyToArray(xs: Array[A], start: Int, len: Int):Unit
参数:
xs: 表示复制元素的数组。
start: 表示复制发生的起始索引。默认值为0。
len: 表示要复制的元素的数量。其默认值是SortedSet的长度。
返回类型: 将SortedSet的元素返回到一个数组中。
例1 :
// Scala program of copyToArray()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedSets
val s1 = SortedSet(1, 2, 3)
val arr: Array[Int] = Array(0, 0, 0, 0, 0)
// Applying copyToArray method
s1.copyToArray(arr)
// Displays output
for(elem <- arr)
println(elem)
}
}
输出
1
2
3
0
0
例2 :
// Scala program of copyToArray()
// method
import scala.collection.immutable.SortedSet
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedSets
val s1 = SortedSet(1, 2, 3)
val arr: Array[Int] = Array(0, 0, 0, 0, 0)
// Applying copyToArray method
s1.copyToArray(arr, 1, 2)
// Displays output
for(elem <- arr)
println(elem)
}
}
输出
0
1
2
0
0
极客教程