Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the index of an element in a list scala

Tags:

list

scala

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"

like image 277
yAsH Avatar asked Jul 26 '13 05:07

yAsH


People also ask

How do I find the index of a specific element?

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.

How do you get the index of an element in a list in C?

FindIndex Method: int index = myList. FindIndex(a => a. Prop == oProp);

What is index in Scala?

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.

How do you check if a value is in a list Scala?

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 .


1 Answers

scala> List("Mary", "had", "a", "little", "lamb").indexOf("little") res0: Int = 3 

You might try reading the scaladoc for List next time. ;)

like image 151
DaoWen Avatar answered Oct 13 '22 04:10

DaoWen