Groovy, how to iterate a list with an index

You can use eachWithIndex:

list.eachWithIndex { item, index ->
    println item
    println index
}

With Groovy 2.4 and newer, you can also use the indexed() method. This can be handy to access the index with methods like collect:

def result = list.indexed().collect { index, item ->
    "$index: $item"
}
println result

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)