带有示例的 Scala 迭代器连接
Scala 迭代器的连接是通过使用运算符++完成的。它属于类AbstractIterator的具体值成员。它用于添加两个迭代器的元素。
它在Iterator类中定义。
Method Definition : def ++(that: => Iterator[A]): Iterator[A]
Return Type : It returns the concatenation of two iterators.
示例 #1:
// Scala program of concatenation
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying ++ operator
val result = Iterator(2,4).++(Iterator(6,7))
// Applying while loop
while(result.hasNext)
{
// Displays output
println(result.next())
}
}
}
输出:
2
4
6
7
因此,迭代器的元素都被添加了,在这里我们使用了hasNext和next方法,它们可以在 Scala 中的迭代器上调用。
示例 #2:
// Scala program of concatenation
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Applying ++ operator
val result = Iterator(0).++(Iterator(1))
// Applying while loop
while(result.hasNext)
{
// Displays output
println(result.next())
}
}
}
输出:
0
1