Open In App

Scala Iterator toArray() method with example

Last Updated : 13 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The toArray() method belongs to the concrete value member of the class Abstract iterator. It is utilized to convert the stated collection to an array.

Method Definition: def toArray: Array[A]

Return Type: It returns an array from the elements of the iterator.

Example #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)
      
    }
}


Output:

[I@506e1b77

Example #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)
      
    }
}


Output:

[I@3535dee8


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads