groovy Ways of Iteration in Groovy Each and EachWithIndex


Example

each and eachWithIndex are methods to iterate over collections.

each have it(default iterator) and eachWithIndex have it,index(default iterator, default index).

We can also change the default iterator/index. Please see below examples.

def list = [1,2,5,7]
list.each{
    println it
}

list.each{val->
    println val
}

list.eachWithIndex{it,index->
    println "value " + it + " at index " +index
}