📜  Scala Iterator next() 方法与示例

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

Scala Iterator next() 方法与示例

next()方法属于 AbstractIterator 类的抽象值成员。它在Iterator类中定义。它给出了指定迭代器的下一个元素并推进迭代器。

示例 #1:

// Scala program of next()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an Iterator 
        val iter = Iterator(5, 8, 10, 13, 15)
          
        // Applying next method 
        val result = iter.next
          
        // Applying next method 
        // again 
        val res = iter.next
          
        // Displays output
        println(result)
        println(res)
      
    }
}
输出:
5
8

示例 #2:

// Scala program of next()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an Iterator 
        val iter = Iterator(1.1, 2.2)
          
        // Applying next method 
        val result = iter.next
          
        // Applying next method 
        // again 
        val res = iter.next
          
        // Displays output
        println(result)
        println(res)
      
    }
}
输出:
1.1
2.2