# How should I organise this? 256 array with conditional content - Class?

**URL:** https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290
**Category:** Libraries
**Created:** [December 24, 2022, 1:36pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290 "2022-12-24T13:36:45Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [December 24, 2022, 1:36pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/1 "2022-12-24T13:36:45Z")

</div>

Hi All,  
Festive greetings. What’s the best way to do this? I will have up to 256 different sections of a live AV performance - each potentially featuring a different bit of cool codey Processing stuff. Should I be using a for loop (\<=255, i++?) How I then could I implement variable content depending upon a current pattern? See my idiot code below - while I think this would work, it seems far from optimal.

```auto
  if (PatternNewNumber!=-1){
    textSize(48);
    text(PatternNewNumber, 10, 100);
    
      if (PatternNewNumber == 0){
      // cool Processing Stuff 1..
      }
      
      if (PatternNewNumber == 1){
      // cool Processing Stuff 2..
      }
      
      if (PatternNewNumber == 2){
      // cool Processing Stuff 3..
      }
     
      ...&etc 
    
      if (PatternNewNumber == 256){
      // cool Processing Stuff 255
      }
}

```

---

<div class="post-metadata">

### Author: ![Mikey83](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mikey83/32/17956_2.png) [@Mikey83](https://discourse.processing.org/u/Mikey83)
#### Post date: [December 24, 2022, 2:17pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/2 "2022-12-24T14:17:54Z")

</div>

Hi, Try looking at switch statements, far more effective at things like this

> **[switch / Reference](https://processing.org/reference/switch.html)**
>
> Works like an if else structure, but switch is more convenient when you need to select between three or more alternatives. Program controls jumps to the case with the same value as the e…

---

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [December 24, 2022, 2:28pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/3 "2022-12-24T14:28:15Z")

</div>

Like this right? But should I use a loop somehow? Or classes or something?

```auto
 if (PatternNewNumber!=-1){
    textSize(48);
    text(PatternNewNumber 10, 100);
    
      switch (PatternNewNumber){
        
        case 1:
          // cool Processing Stuff 1..
        break;
        
        case 2:
         // cool Processing Stuff 2..
        break;
        
        case 3:
          // cool Processing Stuff 3..
        break;     

        case 4:
          // cool Processing Stuff 4..
        break;
    
       ..... etc   
    
        case 255:
         // cool Processing Stuff 255..
        break;  
}

```

---

<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: [December 24, 2022, 2:43pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/4 "2022-12-24T14:43:43Z")

</div>

If those cool stuffs can fit into a method you could create a base `interface` or `abstract class` w/ 1 `abstract` method **display()**:

```auto
@FunctionalInterface interface CoolStuff {
  void display();
}

```

```auto
abstract class CoolStuff {
  abstract void display();
}

```

Then create a CoolStuff[] array w/ _length_ 256:

```auto
final static int STUFFS = 256;
final CoolStuff[] stuffs = new CoolStuff[STUFFS];

```

And in a separate tab file have a function **createCoolStuffs()** where you anonymous-instantiate your 256 CoolStuff objects.

Each instance would have a different implementation of **display()**:

```auto
void createCoolStuffs() {
  stuffs[0] = new CoolStuff() {
    @Override public void display() {
      // Cool Processing Stuff 000...
    }
  };

  stuffs[1] = new CoolStuff() {
    @Override public void display() {
      // Cool Processing Stuff 001...
    }
  };

  stuffs[2] = new CoolStuff() {
    @Override public void display() {
      // Cool Processing Stuff 002...
    }
  };
}

```

---

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [December 24, 2022, 3:49pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/5 "2022-12-24T15:49:41Z")

</div>

Oh my brain hurts!! Excellent. thanks for the guidance. What’s the rational for approaching the problem in this way? What are the advantages over the simple switch example?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [December 24, 2022, 4:40pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/6 "2022-12-24T16:40:07Z")

</div>

@GoToLoop provides a nice solution

> [@JonnieNightTime](#):
>
> What are the advantages over the simple switch example?

It simplifies the creation and use of your coolstuff.

To create new coolstuff simply add extra stuffs in the createCoolStuffs() function then when you want to use it initialise it in setup and call it in draw like this

```auto
int currStuff = 0;

void setup(){

   createCoolStuffs();
}

void draw(){
  background(...);

  stuffs[currStuff].display();
}

```

You can change the stuff to display by simply changing `currStuff` to some other valid value in the array.

It avoids huge `if` or `switch` statements which would be difficult to maintain.

---

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [December 24, 2022, 6:35pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/7 "2022-12-24T18:35:07Z")

</div>

Hey quark and GoToLoop,  
This is making total sense - thanks to you both for your clarity - you guys are great - making complex things easy to understand - a real gift 🙂 Thank you and Nadolig Llawen (Welsh for Merry Christmas)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 25, 2022, 2:16pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/8 "2022-12-25T14:16:26Z")

</div>

That’s impressive and could also be used to make the states  
of a Sketch.

Am I right?

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [December 25, 2022, 4:45pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/9 "2022-12-25T16:45:50Z")

</div>

> [@Chrisir](#):
>
> Am I right?

Yes 😄

_Oh dear I need 20 characters LOL_

---

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [December 26, 2022, 6:38pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/10 "2022-12-26T18:38:41Z")

</div>

Hi Chrisir, An interesting question, and one beyond my understanding - could you expand on this a little?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 26, 2022, 11:12pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/11 "2022-12-26T23:12:01Z")

</div>

Sorry, it has nothing to do with your initial question

We often use states here in the forum to distinguish between different screens of a program, for example welcome screen, game and high score.

These states are often distinguished with switch() command but this approach here is better

---

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [January 2, 2023, 6:00pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/12 "2023-01-02T18:00:51Z")

</div>

Thanks Chrisir for the wider context. I’m interested in furthering my knowledge in all aspects, so the broader comments are always welcome! 🙂

---

<div class="post-metadata">

### Author: ![JonnieNightTime](https://avatars.discourse-cdn.com/v4/letter/j/ecb155/32.png) [@JonnieNightTime](https://discourse.processing.org/u/JonnieNightTime)
#### Post date: [January 3, 2023, 12:55pm UTC](https://discourse.processing.org/t/how-should-i-organise-this-256-array-with-conditional-content-class/40290/13 "2023-01-03T12:55:38Z")

</div>

As I am now trying to expand upon the CoolStuff abstract class, where would I incorporate, for example, movie playback. If I wanted to play a different video for each bankstuffs[i] where’s the best place for the following? Import the video library in the main sketch? If there are 16 different large video files (1GB+) will there be any potential memory issue with putting movie = new Movie(this, “BankA.mov”); x 16 in setup() as the example suggests? Do I put it in the CoolBanks class instead? and the movieEvent into the mainsketch too? What’s finally the best way to conditionally play each movie - bankstuffs[0], bankstuffs[1], bankstuffs[2] etc ? Any guidance would be awesome.

```auto
import processing.video.*; // this in main sketch?

Movie bankmovie;

void setup() {
  size(560, 406);
  background(0);
  bankmovie = new Movie(this, "BankA.mov"); /* there will be 16 of these - should this be an array
  any likely issue with 16 large video files here? */ 
  bankmovie.loop();
}

void movieEvent(Movie m) { // where should this go - main sketch or abstract class?
  m.read();
}

void draw() {
  if (bankmovie.available() == true) { 
  bankmovie.read(); 
  image(bankmovie, 0, 0, width, height); /* make bankmovie an array index? bankmovie[i]? called within bankstuffs[i]?
 */ 
}
}

```
