Scala List iterator() 方法与示例
迭代器方法用于给出一个迭代器。
Method Definition: def iterator: Iterator[A]
Return Type: It returns a non-empty iterator for non-empty list and returns an empty iterator for empty list.
示例 #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