Wednesday 23 January 2019

Groovy: List: indexOf: Return the index of an element in the list


Groovy provides ‘indexof’ method, it returns the index of an element in the list. If no element found, then indexOf method return -1.

HelloWorld.groovy

chars = ['a', 'e', 'i', 'o', 'u', 'u', 'o', 'i', 'e', 'a']

int index1 = chars.indexOf('i')
int index2 = chars.indexOf('z')

println "Index of 'i' is $index1"
println "Index of 'z' is $index2"

Output
Index of 'i' is 2
Index of 'z' is -1


Previous                                                 Next                                                 Home

No comments:

Post a Comment