Scala Iterator indexOf() method with example
Last Updated :
28 May, 2019
The indexOf() method belongs to the concrete value members of the class Abstract Iterator. It is helpful in searching the values and then indicating their positions in the stated iterator.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 2 , 3 , 4 , 9 , 6 )
val result = iter.indexOf( 9 )
println(result)
}
}
|
Here, the value 9 in the indexOf method is present in the third position of the iterator so, it returns three.
Example :
object GfG
{
def main(args : Array[String])
{
val iter = Iterator( 2 , 3 , 4 , 9 , 6 )
val result = iter.indexOf( 7 )
println(result)
}
}
|
Here, the value stated in the method indexOf is not present in the iterator so, it returns -1.
Note: If the value given in the indexOf method is not present in the stated iterator then this method will return -1.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...