Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
Iterator in Scala is a way to access the elements of a collection one by one. The two basic operations on an iterator are next
(to access the next item and put the pointer to next to next item) and hasNext
(to check whether an iterator has more items or not).
A simple example of declaring and looping over iterator is given below:
val it = Iterator("You", "are", "next")
while (it.hasNext){
println(it.next())
}
An iterator is a collection of items which can be traversed using hasNext() and next() to get the next item. An iterator can be traversed only once after which items are lost.
Declare an Iterator named myIter
, having below items.
"I"
"am"
"going"
Note you only have to declare. You don't have to iterate in a loop for this excercise.
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...