- Declare a variable before the loop, initialize to 0.
- Increment variable at the beginning of the body of for-each.
- Check variable at the end of the body of for-each.
You have to be careful with this technique because there could be other break/continue in the for-each body, but in your case there isn't, so this would work.