Wednesday, 23 January 2019

Groovy: List: lastIndexOf: Return the index of an element from the end of list


Groovy provides ‘lastIndexof’ method, it returns the index of an element in the list from the end. If no element found, then lastIndexOf method return -1.

HelloWorld.groovy

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

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

println "Index of 'i' is $index1"
println "Index of 'i' from the end of list is $index2"


Output
Index of 'i' is 2
Index of 'i' from the end of list is 7



Previous                                                 Next                                                 Home

No comments:

Post a Comment