When using Groovy’s eachWithIndex method the index value starts at 0, I need it to start at 1. How can I do that?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The index will always start from
0Your options are:
1) Add an offset to the index:
2) Use something other than
eachWithIndex(ie: transpose a list of integers starting at 1 with your original list, and then loop through this)3) You can also use default parameters to hack this sort of thing in… If we pass
eachWithIndexa closure with 3 parameters (the two thateachWithIndexis expecting and a third with a default value ofindex + 1):We will give the output: