How do I find the index of an element in a Scala list.
val ls = List("Mary", "had", "a", "little", "lamb")
I need to get 3 if I ask for the index of "little"
ArrayList. indexOf(). This method returns the index of the first occurance of the element that is specified. If the element is not available in the ArrayList, then this method returns -1.
FindIndex Method: int index = myList. FindIndex(a => a. Prop == oProp);
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.
contains() function in Scala is used to check if a list contains the specific element sent as a parameter. list. contains() returns true if the list contains that element. Otherwise, it returns false .
scala> List("Mary", "had", "a", "little", "lamb").indexOf("little") res0: Int = 3
You might try reading the scaladoc for List next time. ;)
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