Wednesday 30 January 2019

Groovy: Print elements of list without using a counter and list size


In Groovy, non-empty list is evaluated to true, whereas empty list is evaluated to false. We can use the same to traverse the list.

HelloWorld.groovy
primes = [2, 3, 5, 7, 11]

while(primes){
 number = primes.remove(0)
 println "$number "
}


Output
2
3
5
7
11



Previous                                                 Next                                                 Home

No comments:

Post a Comment