Adding array to array list

So Im new to processing and I’m trying to port p5 code.

This is the original p5 code

  this.init = function(){
      var theta = 360/sides;
       for (var i=0;i<sides;i++){
            var thetab =  theta * i;
            var thetac =  theta * i+theta;

            this.walls.push(true);
            this.edge.push([this.x+w/2*sin(thetab),this.y+w/2*cos(thetab),this.x+w/2*sin(thetac),this.y+w/2*cos(thetac)]);
            this.vertex.push([this.x+w/2*sin(thetac),this.y+w/2*cos(thetac)]);
            this.length = dist(this.x+w/2*sin(thetab),this.y+w/2*cos(thetab),this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
  }

Ive created arraylists to replace the arrays as such in the class outside of the constructor

ArrayList<Boolean> walls =new ArrayList<Boolean>();
ArrayList<Float[]> edge =new ArrayList<Float[]>();
ArrayList<Float> vertex =new ArrayList<Float>();

and am trying to add to them in an amended function like this.

  void init(){
      float theta = 360/sides;
       for (int i=0;i<sides;i++){
            float thetab =  theta * i;
            float thetac =  theta * i+theta;
            float []a = {x+w/2*sin(thetab),y+w/2*cos(thetab),x+w/2*sin(thetac),y+w/2*cos(thetac)};
            walls.add(true);
            edge.add( a);
            vertex.add(x+w/2*sin(thetac),y+w/2*cos(thetac));
            length = dist(x+w/2*sin(thetab),y+w/2*cos(thetab),x+w/2*sin(thetac),y+w/2*cos(thetac));
  }

however I get this error.

The method add(int, Float) in the type ArrayList is not applicable for the arguments (float, float)

1 Like

There is a command vertex, therefore it’s confusing to name your ArrayList vertex

better would be vertexList.

You could say

before setup():

ArrayList<PVector> vertexList = new ArrayList();

in setup():

vertexList.add(new PVector(x+w/2*sin(thetac), 
  y+w/2*cos(theta*c)));

Chrisir

I will try that thanks, however its producing the error on the line before at edge.add

Do you mean walls.add(true); ?

replace

ArrayList walls =new ArrayList();

with

ArrayList walls =new ArrayList();

1 Like

No… you referenced vertex.add in your first reply, however on the line before vertex at edge.add, this is where im getting the error.

Can’t help you with that

1 Like

@paulgoux , please bring your project ( or MCVE ) to
https://editor.p5js.org/
so we can see the whole thing ( your source? ).
AND also can play.


and the JAVA code please post here again by
pasting it into the

</> code tag

! and test if that what you posted is running ( besides that one error? )

so we easy can copy and try it in processing 3.5.3 JAVA mode
to find your bug.


1 Like

Fixed its a simple error in this case. I’d created ArrayList<Float[]> edge =new ArrayList<Float[]>(); with a Float and not a float, so it then “a” needs to be Float a, and not float a.

2 Likes

In case you were wondering this was my project.

https://editor.p5js.org/paulgoux/sketches/NyWLGYBof

3 Likes