Tuesday, 22 January 2019

Groovy: Lists: How to get the element at given position?


There are three ways to get an element from list.
a.   Using index notation
b.   Using getAt method
c.    Using get method

HelloWorld.groovy
evenNumbers = [0, 2, 4, 6]

println "evenNumbers[2] : ${evenNumbers[2]}"
println "evenNumbers.getAt(2) : ${evenNumbers.getAt(2)}"
println "evenNumbers.get(2) : ${evenNumbers.get(2)}"

Output
evenNumbers[2] : 4
evenNumbers.getAt(2) : 4
evenNumbers.get(2) : 4


Previous                                                 Next                                                 Home

No comments:

Post a Comment