# How to loop through objects?

**URL:** <https://discourse.processing.org/t/how-to-loop-through-objects/17990>\
**Category:** Beginners\
**Created:** [February 22, 2020, 1:11am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990 "2020-02-22T01:11:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 22, 2020, 1:11am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990/1 "2020-02-22T01:11:43Z")

</div>

Let’s say that we have a simple class in use here:

```auto
boolean wheatClicked = false;
boolean carrotClicked = false;
boolean potatoClicked = false;

class Crop
{
  int quantity;
  
  Crop(int cropQuantity)
  { 
    quantity = cropQuantity;
  }
  
  void IncrementData()
  {
    quantity += 1;
  }

  wheatClicked = false;
  carrotClicked = false;
  potatoClicked = false;
}

Crop wheat;
Crop carrot;
Crop potato;

void setup()
{ 
  wheat = new Crop(1);
  carrot = new Crop(5);
  potato = new Crop(10);
}

void draw()
{ 
  if (wheatClicked)
    wheat.IncrementData();
  if (carrotClicked)
    carrot.IncrementData(); 
  if (potatoClicked)
    potato.IncrementData();
}

void mousePressed()
{ 
  if (mouseX > 100 && mouseX < 150)
  {
    if (mouseY > 250 && mouseY < 300)
      wheatClicked = true;
    if (mouseY > 350 && mouseY < 400)
      carrotClicked = true;
    if (mouseY > 450 && mouseY < 500)
      potatoClicked = true;
  }
}

```

However, there won’t be just 3 crops, there will be dozens. So do I have to physically write out dozens of dozens of lines of repetitive code? Or can I loop these in perhaps a `for loop` or something?

And if so, how would I do so?

Thanks

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [February 22, 2020, 1:32am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990/2 "2020-02-22T01:32:17Z")

</div>

Yes. Instead of having one object for each of your crops, put all your crops in an array. It’s like a list of them.

> [@anon94203042](#):
>
> Crop wheat;  
> Crop carrot;  
> Crop potato;

Becomes just:

```auto
Crop[] crops = new Crop[12];

```

Now you suddenly have space for a dozen Crop objects! Next, use a loop to create them. Since this only happens once, do it in setup():

```auto
for( int i = 0; i < crops.length; i++){
  crops[i] = new Crop(0);
}

```

Now you can loop over them in draw() too, to draw them.

BONUS THOUGHT: Can you write a function in the Crop class that detects if a mouse was clicked on that crop? The Crop class will need to know a few things about itself… like its position…

See also: [Arrays / Processing.org](https://processing.org/tutorials/arrays/)

---

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 22, 2020, 2:37am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990/3 "2020-02-22T02:37:58Z")

</div>

Cool! Thanks!

Oh and yes, I can do stuff with `mousePressed` and all that. This was just a short snippet I created.

---

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 22, 2020, 2:42am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990/4 "2020-02-22T02:42:35Z")

</div>

Oh, and a quick question… so after I do this:

```auto
for(int i = 0; i < crops.length; ++i)
  crops[i] = new Crop(5);

```

Then I can do this?

```auto
crops[0].IncrementData();

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [February 22, 2020, 2:47am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990/5 "2020-02-22T02:47:32Z")

</div>

Yes, exactly. You pick one of the crops out of the list by using the square brackets [] with an index, and then you call the function on that crop object.

---

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 22, 2020, 2:50am UTC](https://discourse.processing.org/t/how-to-loop-through-objects/17990/6 "2020-02-22T02:50:21Z")

</div>

Cool, thanks! 👌
