# Convert into arraylist to add elements

**URL:** <https://discourse.processing.org/t/convert-into-arraylist-to-add-elements/17317>\
**Category:** Coding Questions\
**Created:** [January 24, 2020, 6:24pm UTC](https://discourse.processing.org/t/convert-into-arraylist-to-add-elements/17317 "2020-01-24T18:24:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![floflopm](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@floflopm](https://discourse.processing.org/u/floflopm)\
**Post date:** [January 24, 2020, 6:24pm UTC](https://discourse.processing.org/t/convert-into-arraylist-to-add-elements/17317/1 "2020-01-24T18:24:00Z")

</div>

Hi,

I am working with a code where there is a defined number of elements N\_ATOMS proportionate to the screen size. I would like to add elements using MousePressed. However, there is no ArrayList in the code and I am not sure how to convert it. How can I increase N\_ATOMS? I don’t mind losing the number being proportionate to the screen, I would just set a fix size and adjust the amount of elements consequently.

Thank you very much!

```auto
Atom[] atoms; 
int N_ATOMS; //number of atoms
final int WATER = 0; //-0
final int HYDROPHOBIC = 1; //adjust HC value -1
final int R = 10; // rayon d'action de la Strong Force (buffer perimeter around each atom) -6

//------------------------------------------------------------------------------
class Atom 
{
  public PVector position,velocity,acceleration;
  public int type;
  public Atom(int t){
    position = new PVector(random(width),random(height));
    velocity = new PVector(random(-3,3),random(-3,3));
    acceleration = new PVector();
    type = t;
  }
  
  public void drawAtom()
  {
    switch(type) {
      case WATER: fill(color(0)); break; //Water color
      case HYDROPHOBIC: fill(color(255)); break; //oil color
      default: fill(color(255)); break;
    }
    ellipse(position.x,position.y,max(4,R),max(4,R)); // change shape
  }
  
}
//------setup-----------------------------------------------------------------------------------
void setup()
{
  size(1000,1000,P2D); // change the size of the world here
  smooth();
  
  N_ATOMS = width * height / ( R * 100 ); // proportion number of atoms to window -70
  atoms = new Atom[N_ATOMS]; //create atoms
  for(int i=0;i<N_ATOMS;i++) //call all atoms
  {
    switch(i%3) { // water/oil ratio (more % = less oil) -%5
      default: atoms[i] = new Atom(WATER); break; 
      case 0: atoms[i] = new Atom(HYDROPHOBIC); break;
    }
  }
}

//-----------------------------------------------------------------------------------------------------------------

void draw(){
  
  background(198,216,230);
  
  for(int i=0;i<N_ATOMS;i++) // call all atoms
    atoms[i].drawAtom(); //display them
    
  // draw the mouse-controlled water atom
  fill(color(255)); //its color
  ellipse(mouseX,mouseY,max(4,R),max(4,R)); //position and size
}

```

---

<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:** [January 24, 2020, 6:31pm UTC](https://discourse.processing.org/t/convert-into-arraylist-to-add-elements/17317/2 "2020-01-24T18:31:34Z")

</div>

> [@floflopm](#):
>
> However, there is no ArrayList in the code and I am not sure how to convert it.

- [Why use ArrayList instead of array with append()? - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/8080/why-use-arraylist-instead-of-array-with-append)
