📜  Scala List iterator() 方法与示例

📅  最后修改于: 2022-05-13 01:54:53.674000             🧑  作者: Mango

Scala List iterator() 方法与示例

迭代器方法用于给出一个迭代器。

示例 #1:

// Scala program of iterator
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a list
        val m1 = List(1, 2, 3, 4, 5)
          
        // Applying iterator method
        val result = m1.iterator
          
        // Displays output
        println(result)
          
    }
}
输出:
non-empty iterator

示例 #2:

// Scala program of iterator
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an empty list
        val m1 = List() 
          
        // Applying iterator method
        val result = m1.iterator
          
        // Displays output
        println(result)
      
    }
}
输出:
empty iterator