I have a scala Iterator[A]. How can I reference the nth element of it?
For instance:
val myIter: Iterator[Seq[String]] = { .... }
//get the size of the Iterator
val s = myIter.size
//get 3rd element (base 0)
val third:Seq[String] = myIter.get(2) <---- Something like this?
I maybe misreading the docs but can't find a function to do this easily. Thanks for help
To get an iterator starting from the n'th item, the idea is to construct an iterator pointing to the beginning of the input vector and call the standard algorithm std::advance to advance the iterator by specified positions.
An element can be removed from a Collection using the Iterator method remove(). This method removes the current element in the Collection. If the remove() method is not preceded by the next() method, then the exception IllegalStateException is thrown.
Approach 1: Using Iterator The Iterator object is used to iterate over the elements of the list using the hasNext() and next() methods. An if the condition is used within the while loop and when the condition is satisfied, the particular element is removed using the remove() method.
If you want to live dangerously,
myIter.drop(2).next
or if you'd rather be safe
myIter.drop(2).take(1).toList.headOption
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With