0

I've been wondering if it is somehow possible to get the current index of a for each loop in Java without a seperate count variable.

In debug mode I can see the loop in the following structure:

i$ = {ArrayList$Itr@17838}
    cursor = 1
    lastRet = 0
    expectedModCount = 5
    this$0 = {ArrayList@17836}

So I guess cursor is the index. Is there a way to access the cursor?

Thanks for your help

So far

0x44656e6e795279616e
  • 891
  • 3
  • 11
  • 23
  • Look at this comment: http://stackoverflow.com/a/1491331/2574357 – mlewandowski Feb 24 '16 at 14:47
  • 1
    This answer - [How do you get the index of the current iteration of a foreach loop](http://stackoverflow.com/questions/43021/how-do-you-get-the-index-of-the-current-iteration-of-a-foreach-loop#answer-43029) – ThisClark Feb 24 '16 at 14:51
  • @HELOX it is not duplicate, as in the link you provides the question is more general. In this post I explicitly asked about how to access cursor of loop. – 0x44656e6e795279616e Feb 24 '16 at 15:04
  • I still think it is. As stated multiple times on the page, you can't. They even give some workarounds (but you are not interested in those). – HELOX Feb 24 '16 at 15:34

1 Answers1

2

Not to my knowledge. But even if there were a way, it would be counterproductive to access. The reason why one would want a for:each loop is because it is quick, and cycles through a whole array in a simple manor. If you want to have a cursor index to access, use a for loop and just access the incrementing variable.