# Is there a better way to write this piece of code?

**URL:** https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961
**Category:** Beginners
**Created:** [August 27, 2021, 12:40am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961 "2021-08-27T00:40:47Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)
#### Post date: [August 27, 2021, 12:40am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/1 "2021-08-27T00:40:47Z")

</div>

Is there a better way to write this piece of code? These belong to my DropDownList code. These lines are needed to bring the clicked list to front.  
Thank You.

```auto
 if (d1.isMousePressed()) {

    d1.bringToFront();
  }
  if (d2.isMousePressed()) {

    d2.bringToFront();
  }
  if (d3.isMousePressed()) {

    d3.bringToFront();
  }
  if (d4.isMousePressed()) {

    d4.bringToFront();
  }
  if (d5.isMousePressed()) {

    d5.bringToFront();
  }
  if (d6.isMousePressed()) {

    d6.bringToFront();
  }
  if (d7.isMousePressed()) {

    d7.bringToFront();
  }
  if (d8.isMousePressed()) {

    d8.bringToFront();
  }
   if (d9.isMousePressed()) {

    d9.bringToFront();
  }
  if (d10.isMousePressed()) {

    d10.bringToFront();
  }
  if (d11.isMousePressed()) {

    d11.bringToFront();
  }
  if (d12.isMousePressed()) {

    d12.bringToFront();
  }
  if (d13.isMousePressed()) {

    d13.bringToFront();
  }
  if (d14.isMousePressed()) {

    d14.bringToFront();
  }
  if (d15.isMousePressed()) {

    d15.bringToFront();
  }
  if (d16.isMousePressed()) {

    d16.bringToFront();
  }
  if (d17.isMousePressed()) {

    d17.bringToFront();
  }
  if (d18.isMousePressed()) {

    d18.bringToFront();
  }
   if (d19.isMousePressed()) {

    d19.bringToFront();
  }
  if (d20.isMousePressed()) {

    d20.bringToFront();
  }
  if (d21.isMousePressed()) {

    d21.bringToFront();
  }
  if (d22.isMousePressed()) {

    d22.bringToFront();
  }
  if (d23.isMousePressed()) {

    d23.bringToFront();
  }
  if (d24.isMousePressed()) {

    d24.bringToFront();
  }
  if (d25.isMousePressed()) {

    d25.bringToFront();
  }
  if (d26.isMousePressed()) {

    d26.bringToFront();
  }
  if (d27.isMousePressed()) {

    d27.bringToFront();
  }
  if (d28.isMousePressed()) {

    d28.bringToFront();
  }
   if (d29.isMousePressed()) {

    d29.bringToFront();
  }
  if (d30.isMousePressed()) {

    d30.bringToFront();
  }
  if (d31.isMousePressed()) {

    d31.bringToFront();
  }
  if (d32.isMousePressed()) {

    d32.bringToFront();
  }
  if (d33.isMousePressed()) {

    d33.bringToFront();
  }
  if (d34.isMousePressed()) {

    d34.bringToFront();
  }
  if (d35.isMousePressed()) {

    d35.bringToFront();
  }
  if (d36.isMousePressed()) {

    d36.bringToFront();
  }

```

---

<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: [August 27, 2021, 12:53am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/2 "2021-08-27T00:53:20Z")

</div>

[forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays.html](http://forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays.html)

---

<div class="post-metadata">

### Author: ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)
#### Post date: [August 27, 2021, 1:05am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/3 "2021-08-27T01:05:51Z")

</div>

Thank you. But I don’t understand how I am going to make an array with d1, d2, d3, etc. They are not “int”

---

<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: [August 27, 2021, 1:19am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/4 "2021-08-27T01:19:05Z")

</div>

> [@jhsa](#):
>
> They are not `int`.

Just replace `int` by the desired datatype: `final DataType[] dArray = new DataType[37];`

---

<div class="post-metadata">

### Author: ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)
#### Post date: [August 27, 2021, 1:37am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/5 "2021-08-27T01:37:18Z")

</div>

Ok, thanks…  
Do I have to do something like DataType[] = {d1,d2,d3…d36} ??  
Sorry, I am a real beginner with Processing. Arduino seems to be quite different in many aspects. I am a beginner with arduino as well…

---

<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: [August 27, 2021, 1:50am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/6 "2021-08-27T01:50:53Z")

</div>

> **[Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

[docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html](http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html)

---

<div class="post-metadata">

### Author: ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)
#### Post date: [August 27, 2021, 2:06am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/7 "2021-08-27T02:06:27Z")

</div>

Thank you. will read it and try to find examples… I just need to understand the logic of it.

---

<div class="post-metadata">

### Author: ![rapatski](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rapatski/32/5164_2.png) [@rapatski](https://discourse.processing.org/u/rapatski)
#### Post date: [August 30, 2021, 1:41am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/8 "2021-08-30T01:41:07Z")

</div>

Hi jhsa,

Indeed like GoToLoop is saying, an array is nothing but a list, and can be of any one datatype. But like you remarked, you do need to define which type they are. With the example above, I suspect these are controlp5 DropDownLists, though 36 dropdowns seems pretty mad!

In any case, how you’d rewrite those as an array would be something like:

```auto
int numLists = 36;
DropdownList[] crazyListOfDropdownLists = new DropdownList[numLists];

void setup() 
{
  for (int i = 0; i < crazyListOfDropdownLists.length; i++) {
    crazyListOfDropdownLists[i] = cp5.addDropdownList("myList-d"+(i+1)).setPosition(100, 100 + i*100);
  }
}

void draw()
{
  for (int i = 0; i < crazyListOfDropdownLists.length; i++) {
    if (crazyListOfDropdownLists[i].isMousePressed()) {
      crazyListOfDropdownLists[i].bringToFront();
    }
  }
  
  
}

```

---

<div class="post-metadata">

### Author: ![jhsa](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@jhsa](https://discourse.processing.org/u/jhsa)
#### Post date: [August 30, 2021, 1:49am UTC](https://discourse.processing.org/t/is-there-a-better-way-to-write-this-piece-of-code/31961/9 "2021-08-30T01:49:48Z")

</div>

Thank you, will definitely try your suggestion, I got the feeling that it is close to what i am looking for.  
You think 36 lists are crazy? I will probably need other 20 or so 😃😛 The “Bypass” tab has another two that aren’t visible on this one. I know I am crazy doing something like this as my first, well, second project. the first one was reading/saving the eeprom from/to my DIY guitar effects switcher.  
Have a look at what I currently have. 🙂

 ![iLeditor](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c7302a6cef50b8eed85ebf383a8aafed0436e66b.png)
