looping with index


/ Published in: Groovy
Save to your folder(s)

how to loop an array / collection / list but with index. instead of using each{} closure, you can use eachWithIndex{} closure


Copy this code and paste it in your HTML
  1. // index started with 0
  2.  
  3. def x= [1,2,3,445,6];
  4. x.eachWithIndex{ obj, index ->
  5. println("${index}. ${obj}");
  6. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.