# Remove a object in the array of colors

**URL:** <https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521>\
**Category:** Coding Questions\
**Created:** [June 5, 2021, 11:36pm UTC](https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521 "2021-06-05T23:36:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylu](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@kylu](https://discourse.processing.org/u/kylu)\
**Post date:** [June 5, 2021, 11:36pm UTC](https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521/1 "2021-06-05T23:36:55Z")

</div>

Hi! i’m working with a code, and i need to remove the color once it has been used.  
is a walker who takes on a color each time he begins his path. I need to make sure that the color not to repeat  
This is the class Paleta, i just separate the cold colors of the warm colors, also an array with both of them.  
Thanks!

```auto

color [] colores = {#d25b17, #e7bc4b, #942720,#1e2744, #304e34, #68494f};
//ARRAY WARM
color [] calidos = {#d25b17, #e7bc4b, #942720};
//ARRAY COLD
color [] frios = {#1e2744, #304e34, #68494f};

//PALETA FONDO

color [] fondo = {#c63435, #ec603c, #ffc13c ,#4c6045 ,#264468 , #41415b};

class Paleta {

  PImage imagen;
  Paleta( ) {

  }
  
  color darColor() {
  return colores[int(random(6))];
}

  color darCalido() { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return calidos[int(random(3))];
  }
  color darFrio() { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return frios[int(random(3))];
  }
  
  color darFondo() {
  return fondo[int(random(6))];
}

```

---

<div class="post-metadata">

**Author:** ![Th3cG](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/th3cg/32/12780_2.png) [@Th3cG](https://discourse.processing.org/u/Th3cG)\
**Post date:** [June 6, 2021, 5:32pm UTC](https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521/2 "2021-06-06T17:32:44Z")

</div>

I suggest to create a couple of twin arrays, then checking if the new color is already in the list and, if so, discard it. Here’s my approach based on arrays of integers - just change _int_ to _color_ and it should work:

```auto
//an array of six numbers
int[] colores = {1, 2, 3, 4, 5, 6};

//a sub-array of three numbers
int[] calidos = {1, 2, 3};
//and its copy of the same lentgh
int[] calidosCopy = new int[calidos.length];

//a second sub-array of other three numbers
int[] frios = {4, 5, 6};
//and its copy
int[] friosCopy = new int[frios.length];

//another array of other six numbers
int[] fondo = {7, 8, 9, 10, 11, 12};

//a random number from zero to three(excluded)
int randomThree = int(random(3));

//a random number from zero to six(excluded)
int randomSix = int (random(6));

//one of the six numbers from the first array
int darColores = colores[randomSix];

//if one of the numbers of the first sub-array
//IS NOT EQUAL TO
//the one of the six numbers previously found,
//copy it to the first sub-array copy
for (int i = 0; i < calidos.length; i++) {
  if (calidos[i] != darColores) {
    calidosCopy[i] = calidos[i];
  }
}

//then extract a random number from the first sub-array copy
int darCalidos = calidosCopy[randomThree];

//if one of the numbers of the second sub-array
//IS NOT EQUAL TO
//the one of the six numbers previously found,
//copy it to the second sub-array copy
for (int i = 0; i < frios.length; i++) {
  if (frios[i] != darColores) {
    friosCopy[i] = frios[i];
  }
}

//then extract a random number from the second sub-array copy
int darFrios = friosCopy[randomThree];

int darFondo = fondo[randomSix];

println(darColores);
println(darCalidos);
println(darFrios);
println(darFondo);

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [June 6, 2021, 6:13pm UTC](https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521/3 "2021-06-06T18:13:07Z")

</div>

Hello!  
I’m not sure but …  
🤔  
If you use **arrayList** instead of **array** you have more flexibility with adding and removing items from an arrayList. And an arraList will resize dynamically.

**You can read about the arrayList here:**

[https://processing.org/reference/ArrayList.html](https://processing.org/reference/ArrayList.html)

**And a really good HOW-TO here:**

> **[ArrayLists](https://happycoding.io/tutorials/processing/arraylists)**
>
> Learn how to use ArrayLists in Processing.

Hopefully this helps.  
🤓

---

<div class="post-metadata">

**Author:** ![kylu](https://avatars.discourse-cdn.com/v4/letter/k/8491ac/32.png) [@kylu](https://discourse.processing.org/u/kylu)\
**Post date:** [June 7, 2021, 5:00pm UTC](https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521/4 "2021-06-07T17:00:16Z")

</div>

Hi everyone! Thank you all for your answer , I could solve it. I leave you the code! 😃

```auto
ArrayList<Integer> listC = new ArrayList();
ArrayList<Integer> listCa = new ArrayList();
ArrayList<Integer> listF = new ArrayList();
import java.util.ArrayList;
import java.util.Random;

//PALETA FONDO

color [] fondo = {#c63435, #ec603c, #ffc13c, #4c6045, #264468, #41415b};
Random randomGenerator;
class Paleta {

  PImage imagen;
  Paleta( ) {

// AÑADIMOS TODOS LOS COLORES
    listC.add(color (210, 91, 23)); // AMARILLO;
    listC.add(color (210, 91, 23)); // NARANJA
    listC.add(color (148, 39, 32)); // ROJO
    listC.add(color (30, 39, 68)); // AZUL
    listC.add(color (48, 78, 52)); // VERDE
    listC.add(color (104, 73, 99)); // MORADO

//AÑADIMOS COLORES CALIDOS
    listCa.add(color (210, 91, 23)); // AMARILLO;
    listCa.add(color (210, 91, 23)); // NARANJA
    listCa.add(color (148, 39, 32)); // ROJO

// AÑADIMOS COLORES FRIOS
    listF.add(color (30, 39, 68)); // AZUL
    listF.add(color (48, 78, 52)); // VERDE
    listF.add(color (104, 73, 99)); // MORADO
  }

  void eliminar (color col) {

    listC.remove((col));
  }
  
  
   void eliminarCalido (color col) {

    listCa.remove((col));
  }
  
  
   void eliminarFrio (color col) {

    listF.remove((col));
  }
  
  color darColor(int _id) {
    return (color) (Integer) listC.get(_id);
  }

  color darCalido(int _id) { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return (color) (Integer) listCa.get(_id);
  }
  
  
  color darFrio(int _id) { //RETORNA UN COLOR AL AZAR DEL ARREGLO
    return (color) (Integer) listF.get(_id);
  }

  color darFondo() {
    return fondo[int(random(6))];
  }
}

```

---

<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:** [June 7, 2021, 5:19pm UTC](https://discourse.processing.org/t/remove-a-object-in-the-array-of-colors/30521/5 "2021-06-07T17:19:31Z")

</div>

The efficient way is to just shuffle the list.

Then you can just use an index from 0 to n and increase it by 1 esch step. So you use every color without duplicates
