# Store values in array

**URL:** https://discourse.processing.org/t/store-values-in-array/5385
**Category:** Coding Questions
**Created:** [November 9, 2018, 3:56pm UTC](https://discourse.processing.org/t/store-values-in-array/5385 "2018-11-09T15:56:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![b\_and\_z](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@b\_and\_z](https://discourse.processing.org/u/b_and_z)
#### Post date: [November 9, 2018, 3:56pm UTC](https://discourse.processing.org/t/store-values-in-array/5385/1 "2018-11-09T15:56:12Z")

</div>

I already posted this on [reddit](https://www.reddit.com/r/processing/comments/9vl3mp/store_values_in_array/), so here goes:

For an installation, I need to create a code where one can press a key (r, g, b, y) which then creates a rgb code for an rgb lamp. There will be a fixed amount of lamps (e.g. 50 in my code), and after a second key is pressed, the first color switches to the next lamp, so if for example the first user pressed “r”, the first lamp glows red. When the second user presses “b”, the red “travels” from the first to the second lamp and the first lamp gets blue.

I made three arrays, one for the red value, one for the green value, one for the blue value. In theory, the arrays each should store 50 values for the 50 lamps, so that the values change as soon as a key is pressed. The new rgb value should always be on the 0th element of the array, and the old values travel to the next element.

Here is what I got so far:

```auto
int mySwitch = 'r';

//lamp count
int num = 50;
int[] r = new int[num];
int[] g = new int[num];
int[] b = new int[num];

int winR[] = {246, 83, 20};
int winG[] = {124, 187, 0};
int winB[] = {0, 161, 241};
int winY[] = {255, 187, 0};

void setup() { 
  size(800, 600); 
  rectMode(CENTER);
} 

void draw() { 
  noStroke();

  background(0);

  // Add the new values to the beginning of the array
  if (mySwitch == 'r') {
    r[0] = winR[0];
    g[0] = winR[1];
    b[0] = winR[2];
  } else if (mySwitch == 'g') {
    r[0] = winG[0];
    g[0] = winG[1];
    b[0] = winG[2];
  } else if (mySwitch == 'b') {
    r[0] = winB[0];
    g[0] = winB[1];
    b[0] = winB[2];
  } else if (mySwitch == 'y') {
    r[0] = winY[0];
    g[0] = winY[1];
    b[0] = winY[2];
  }

  for (int i = 0; i < num; i++) {
    float dist = width/num;
    fill(r[i], g[i], b[i]);
    rect(i*dist, height/2, dist, height);
  }
}
void keyPressed() { 

  if (keyCode == 82) { 
    mySwitch = 'r';
  } else if (keyCode == 71) { 
    mySwitch = 'g';
  } else if (keyCode == 66) { 
    mySwitch = 'b';
  } else if (keyCode == 89) { 
    mySwitch = 'y';
  } 

}

```

Unfortunately, this only works for one lamp at a time, as the other values aren’t stored in the array. Do you have any advice on how I could make the preceding rgb values be stored in the array?

I hope I could make myself clear, but if you have any questions, just ask! Thanks for any help!

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 9, 2018, 4:04pm UTC](https://discourse.processing.org/t/store-values-in-array/5385/2 "2018-11-09T16:04:28Z")

</div>

You could first of all replace int num = 50 with Color[] col = new Color[50]; and just take it‘s length. And then just set the respective Color value for each element. And then just iterate over the array and each Time a new Color is added you can just set each Element to be one higher.

You could also use Java.util.Queue. Might be a more convenient.

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 10, 2018, 10:05am UTC](https://discourse.processing.org/t/store-values-in-array/5385/3 "2018-11-10T10:05:19Z")

</div>

Hey @b_and_z, before you overwrite r[0], you need to shift all the values. Try to make a for loop which shifts all the values one higher, so you move r[0] to r[1], r[1] to r[2], etc. and the last element will be dropped. Hint: start at the end of the array.

Then values are shifted and you can write a new value to r[0].

---

<div class="post-metadata">

### Author: ![b\_and\_z](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@b\_and\_z](https://discourse.processing.org/u/b_and_z)
#### Post date: [November 11, 2018, 10:31am UTC](https://discourse.processing.org/t/store-values-in-array/5385/4 "2018-11-11T10:31:24Z")

</div>

Thanks for your answer! I tried this, but now the array is filled as long as the key is pressed

```auto
  if (keyPressed) {
    for (int i = num-1; i > 0; i--) {
      r[i] = r[i-1];
      g[i] = g[i-1];
      b[i] = b[i-1];
    }
  }

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 11, 2018, 10:51am UTC](https://discourse.processing.org/t/store-values-in-array/5385/5 "2018-11-11T10:51:22Z")

</div>

Your array goes the wrong way round. You should have the value i+1, not -1. And as for why it‘s filled only while mouse is pressed, you probably added in draw something that resets the array…

---

<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: [November 11, 2018, 12:39pm UTC](https://discourse.processing.org/t/store-values-in-array/5385/6 "2018-11-11T12:39:07Z")

</div>

Fixed-sized array cycling queue sketches: 🕶

1. [Studio.ProcessingTogether.com/sp/pad/export/ro.9GTDpA6dp4tH1](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9GTDpA6dp4tH1)
2. [Studio.ProcessingTogether.com/sp/pad/export/ro.9ldYvJUyiXGzi](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9ldYvJUyiXGzi)

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 11, 2018, 12:50pm UTC](https://discourse.processing.org/t/store-values-in-array/5385/7 "2018-11-11T12:50:43Z")

</div>

Good job @b_and_z, the code looks good!

Now if you want this to happen only once, you have to put the code inside `keyPressed()`. Otherwise it happens every frame. I think the code which sets `r[0]` should go there as well.

---

<div class="post-metadata">

### Author: ![b\_and\_z](https://avatars.discourse-cdn.com/v4/letter/b/b5e925/32.png) [@b\_and\_z](https://discourse.processing.org/u/b_and_z)
#### Post date: [November 28, 2018, 9:50am UTC](https://discourse.processing.org/t/store-values-in-array/5385/8 "2018-11-28T09:50:57Z")

</div>

Thanks all for your help, I found another way which I also understand 🙂

```auto

boolean initChange = false;

void draw() {
if (initChange) {
  for (int i = num-1; i > 0; i--) {
    r[i] = r[i-1];
    g[i] = g[i-1];
    b[i] = b[i-1];
  }

  initChange=false;
}
}
void mouseClicked() {
  initChange = true;
}

```
