带有示例的 Scala 迭代器 toArray() 方法
toArray()方法属于抽象迭代器类的具体值成员。它用于将指定的集合转换为数组。
Method Definition: def toArray: Array[A]
Return Type: It returns an array from the elements of the iterator.
示例 #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