Indexing for (datatype element : array) loops

Thanks @MiguelSanches and @glv

Yah. If the for-each-loop (enhanced for-loop) doesn’t have an internal counter and i have to introduce the iterator, I might as well just structure the for-loop to use the init-test-update model or introduce an ID variable to the class. Seems cleaner than introducing the iterator.

A lot of what i’m doing is sending values of FFT bands to instances of classes within arrays of that object. My current implementation is:

for (Ball ball : balls)
  {
    float fftVelocity = spectrum[ball.id] * 10.0 + 1;   // THIS RIGHT HERE
    
    ball.resize(fftDiameter[ball.id]);
    ball.collide();
    ball.move(fftVelocity);
    ball.display();
  }

Alternately, i’ll just structure it with an init/test/update for-loop in the future.

Was mostly curious if the enhanced for-loop has that internal counter. But you two gave me the info i needed. Thanks again! Cheers!

3 Likes