# Indexing for (datatype element : array) loops

**URL:** <https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406>\
**Category:** Beginners\
**Created:** [October 7, 2020, 10:13am UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406 "2020-10-07T10:13:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RFullum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rfullum/32/10454_2.png) [@RFullum](https://discourse.processing.org/u/RFullum)\
**Post date:** [October 7, 2020, 10:13am UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406/1 "2020-10-07T10:13:20Z")

</div>

I’ve been trying to figure out if there’s a simple way to extract the index of the iteration through a for-loop when using the _for(elements in an array)_ style iteration.

It’s simple enough using the _for(init; test; update)_ iteration:

```auto
for (int i = 0; i < limit; i++)
{
   someArray[i]; // *just use i to get the item in that array index*
}

```

But if we look at the [Bouncy Bubbles Example](https://processing.org/examples/bouncybubbles.html) we iterate through the elements of the array of Balls:

```auto
  Ball[] balls = new Ball[numBalls];
.
.
.
  for (Ball ball : balls)
  {
    someArray[???]; // *how would we get the index the for-loop has iterated through here?*
    ball.collide();
    ball.move();
    ball.display();  
  }

```

Of course, in the Bouncy Bubbles example, the Ball class has an int variable called `id` that gets indexed as it’s constructed, so we could do something like:

```auto
  for (Ball ball : balls)
  {
     someArray[ball.id];
  }

```

But what if you’re working with an array of objects that don’t have the `id` variable? Is there a way to get the iteration count from the for-loop? Or would it make more sense to switch to either an init/test/increment for loop, or implement an ID variable to the class’s objects?

Thank you?

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [October 7, 2020, 10:59am UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406/2 "2020-10-07T10:59:56Z")

</div>

Hi @RFullum

The for-each loop that not provide a counter by itself, but you can provide your own counter.  
The reason for this is that the for-each loop internally does not _have_ a counter; it is based on the [Iterable](http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Iterable.html) interface, i.e. it uses an `Iterator` to loop through the “collection” - which may not be a collection at all, and may in fact be something not at all based on indexes (such as a linked list).

So, the solution is to either have your own counter, like:

```auto
int index = 0;
for(Ball ball : balls) {
    System.out.println("Current index is: " + (index++));
}

```

or make the class possess the id.

Best regards

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 7, 2020, 11:11am UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406/3 "2020-10-07T11:11:16Z")

</div>

There is an example here:  
_[https://processing.org/reference/for.html](https://processing.org/reference/for.html)_  
_[https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)_  
_[https://processing.org/reference/ArrayList.html](https://processing.org/reference/ArrayList.html)_ It is called an _enhanced for loop_ if you want to search for references to this.

`:)`

---

<div class="post-metadata">

**Author:** ![RFullum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rfullum/32/10454_2.png) [@RFullum](https://discourse.processing.org/u/RFullum)\
**Post date:** [October 7, 2020, 1:06pm UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406/4 "2020-10-07T13:06:25Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 7, 2020, 6:47pm UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406/5 "2020-10-07T18:47:12Z")

</div>

Rather than some _id_ field in class Ball you could instead use a vanilla `for ( ; ; )` loop and cache each index of array _balls[]_ in a variable:

```auto
for (int len = balls.length, i = 0; i < len; ++i) {
  final Ball ball = balls[i];

  ball.resize(fftDiameter[i]);
  ball.collide();
  ball.move(spectrum[i] * 10 + 1);
  ball.display();
}

```

---

<div class="post-metadata">

**Author:** ![RFullum](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rfullum/32/10454_2.png) [@RFullum](https://discourse.processing.org/u/RFullum)\
**Post date:** [October 8, 2020, 2:34pm UTC](https://discourse.processing.org/t/indexing-for-datatype-element-array-loops/24406/6 "2020-10-08T14:34:43Z")

</div>

@GoToLoop Yah. For this example, the _id_ variable is also used for other things within the class so it’s a handy identifier. And I was just having fun modifying Processing’s Bouncy Ball example. But in the future, yeah, if i need some sort of iterator i’ll just use the vanilla `for ( ; ; )` loop. I usually do. Was mostly curious if there was some sort of counter within the _enhanced for loop_.

Cheers!
