Scala Iterator toArray()方法与实例
toArray()方法属于抽象迭代器类的具体值成员。它被用来将所述集合转换为数组。
方法定义:def toArray:Array[A]
返回类型。它从迭代器的元素中返回一个数组。
例子 #1:
// Scala program of toArray()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a Iterator
val iter = Iterator(2, 3, 5, 7, 8, 9)
// Applying toArray method
val result = iter.toArray
// Displays Output
println(result)
}
}
输出。
[I@506e1b77
例子#2。
// Scala program of toArray()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a Iterator
val iter = Iterator(0, 1, 10)
// Applying toArray method
val result = iter.toArray
// Displays Output
println(result)
}
}
输出。
[I@3535dee8
极客教程