Open In App

Scala Iterator indexOf() method with example

Last Updated : 28 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Method Definition:

    def indexOf(elem: B): Int
    

    Where, elem is the element to be searched.

  • Return Type:
    It returns the index of the first occurrence of the element elem in the stated Scala iterator.

Example :




// Scala program of indexOf()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(2, 3, 4, 9, 6)
          
        // Applying indexOf method
        val result = iter.indexOf(9)
          
        // Displays output
        println(result)
  
    }


Output:

3

Here, the value 9 in the indexOf method is present in the third position of the iterator so, it returns three.
Example :




// Scala program of indexOf()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Declaring an iterator
        val iter = Iterator(2, 3, 4, 9, 6)
          
        // Applying indexOf method
        val result = iter.indexOf(7)
          
        // Displays output
        println(result)
  
    }


Output:

-1

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.



Similar Reads

Scala String indexOf(String str) method with example
The indexOf(String str) method is utilized to return the index of the sub-string which occurs first in the string stated. Method Definition: indexOf(String str) Return Type: It returns the index of the sub-string which is specified in the argument of the method. Example #1: // Scala program of int indexOf() // method // Creating object object GfG {
1 min read
Scala List indexOf() method with example
The indexOf() method is utilized to check the index of the element from the stated list present in the method as argument. Method Definition: def indexOf(elem: A, from: Int): Int Return Type: It returns the index of the element present in the argument. Example #1: // Scala program of indexOf() // method // Creating object object GfG { // Main metho
1 min read
Scala String indexOf() method with example
The indexOf() method is utilized to find the index of the first appearance of the character in the string and the character is present in the method as argument. Method Definition: int indexOf(int ch) Return Type: It returns the index of the character stated in the argument. Example #1: // Scala program of int indexOf() // method // Creating object
1 min read
Scala String indexOf(int ch, int fromIndex) method with example
The indexOf(int ch, int fromIndex) method is same as indexOf() method but here this method starts finding the character from the index we specify. Method Definition: int indexOf(int ch, int fromIndex) Return Type: It returns the index of the character specified in the argument. Example #1: // Scala program of int indexOf() // method // Creating obj
1 min read
Scala String indexOf(String str, int fromIndex) method with example
The indexOf(String str, int fromIndex) method is utilized to return the index of the sub-string which occurs first from the index we specify in the string stated. Method Definition: int indexOf(String str, int fromIndex) Return Type: It returns the index of the sub-string from the index specified in the argument. Example #1: // Scala program of int
1 min read
Scala Iterator addString() method with example
The addString() method belongs to the concrete value members of the class AbstractIterator. It is defined in the class IterableOnceOps. It is utilized to append the elements of the Scala Iterator to a String Builder. Method Definition : def addString(b: StringBuilder): StringBuilder Return Type : It returns the String Builder to which the elements
1 min read
Scala Iterator slice() method with example
The slice() method belongs to the concrete value members of the class AbstractIterator. It is defined in the class Iterator. It creates a new iterator for the interval given in the slice. The first value present in the slice indicates the start of the element in the new iterator and the second value present in the slice indicates the end. Method De
2 min read
Scala Iterator size() method with example
The size() method belongs to the concrete value members of the class Abstract Iterator. It is defined in the classes IterableOnceOps. It is utilized to find the size of the stated collection. It will not terminate for the infinite sized collection. Method Definition : def size: Int Return Type :It returns the size of the stated collection.. Example
1 min read
Scala Iterator isTraversableAgain() method with example
The isTraversableAgain() method belongs to the concrete value member of the class iterator in Scala. It checks whether the iterator can be repeatedly traversed or not. Method Definition: def isTraversableAgain: Boolean Return Type: It returns true if the stated iterator can be traversed repeatedly and if it cannot be traversed repeatedly then it re
1 min read
Scala Iterator sameElements() method with example
The sameElements() method belongs to the concrete value members of the class iterator of Scala. It checks whether the two stated iterators produce the identical elements in alike order or not. This method won't terminate for infinite iterators. Method Definition: def sameElements(that: Iterator[_]): Boolean Where, that is the another iterator state
2 min read
Article Tags :